0

I have added a button for Print in my gridview table as follows

<asp:GridView ID="GridView1" runat="server" AllowPaging="True" 
                    AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="ID" 
                    DataSourceID="SqlDataSource2" 
                    onselectedindexchanged="GridView1_SelectedIndexChanged" Width="522px">
                    <Columns>
                        <asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False" 
                            ReadOnly="True" SortExpression="ID" />
                        <asp:BoundField DataField="Story_number" HeaderText="Story_number" 
                            SortExpression="Story_number" />
                        <asp:BoundField DataField="Date" HeaderText="Date" SortExpression="Date" />
                        <asp:BoundField DataField="Memory_card" HeaderText="Memory_card" 
                            SortExpression="Memory_card" />
                        <asp:BoundField DataField="Story_Name" HeaderText="Story_Name" 
                            SortExpression="Story_Name" />
                        <asp:ButtonField ButtonType="Button" Text="print" />
                    </Columns>
                </asp:GridView>

Please help me with the c# code for this button. When the button is pressed I need it to redirect to a page (print.aspx). I have been trying the following code but it does not work .Thanks in advance for your help.

Session["id"] = GridView1.SelectedRow.Cells[0].Text;
Response.Redirect("Print.aspx");
5
  • Question title and body is completely different - title says adding column, body says how to get 1st cell text? Commented Apr 25, 2015 at 15:15
  • also where are you trying the last two lines of your code? And what happens when you click the print button, does it refresh the page or something else ... Commented Apr 25, 2015 at 15:19
  • Sorry for the confusion. What I want is to add a print button to Gridview that when clicked creates a session Variable of the ID of the selected row and redirects to a new web page Commented Apr 25, 2015 at 15:21
  • I am not sure were to use it as I usually use On SelectedIndexChanged when I use the built in Select button. I would like your help as I am new to this Commented Apr 25, 2015 at 15:23
  • You can have select index change event for gridview, then set the value of selected row to session and on button click just redirect. Commented Apr 25, 2015 at 15:35

1 Answer 1

3

Instead of buttonField, use template field

<asp:TemplateField>
    <ItemTemplate>
        <asp:ImageButton ID="ButtonPrint" runat="server" CssClass="yourCssClassIsNeedIt" OnClick="printRegFunction"
            CommandArgument='<%# Bind("id") %>' ImageUrl="images/button.png"/>
    </ItemTemplate>
</asp:TemplateField>

your server code or behind code here:

protected void printRegFunction(object sender, ImageClickEventArgs e)
    {
        Session["id"] = ((ImageButton)sender).CommandArgument;
        Response.Redirect("Print.aspx");

    }
Sign up to request clarification or add additional context in comments.

1 Comment

Thank You Exactly what I wanted.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.