Click here to Skip to main content
15,896,726 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have added a button control in gridview and I want to redirect to the another page on button's click event.

Also, I want to access the values of selected row in a gridview and show those values on other page on button click event which is inside the gridview.

Can anybody please help me out in this .
Posted
Updated 16-Apr-11 3:40am
v2
Comments
Dalek Dave 16-Apr-11 9:40am    
Edited for Grammar, Syntax and Readability.

You can use the PostbackUrl property of the button and set it to the page you want to post. Now at your current page expose the selected row as a public property. Declare the previous page type in in your other page like <%@ PreviousPageType VirtualPath="~/Default.aspx" %>. All set. Now you can access the public property in your page 1 to page 2 by use the object PreviousPage which is actually an instance of your page1 and accessible in page2.

Good luck

For example at the grid view mark up or in the code behind define handlers for RowBound and RowCommand events

XML
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
    DataKeyNames="ProductId" DataSourceID="SqlDataSource1"
        OnRowCommand="GV_RowCommand"   OnRowDataBound="GV_RowBound"
        >


Have template field with button, set its CommandName and PostBackUrl

XML
<asp:TemplateField>
     <ItemTemplate>
         <asp:Button CommandName="Redirect"  ID="Redirect" runat="server" PostBackUrl="~/Default2.aspx" Text="Redirect" />
     </ItemTemplate>
 </asp:TemplateField>




At the event handlers in code behind..

C#
//GridView Row Exposed through a public property
 public GridViewRow SelectedRow { get; set; }
 protected void GV_RowBound(object sender, GridViewRowEventArgs e)
 {
     //Assign the row index as command argument for the row command event
     if (e.Row.RowType == DataControlRowType.DataRow)
     {
         Button btn = e.Row.FindControl("Redirect") as Button;
         btn.CommandArgument = e.Row.RowIndex.ToString();
     }
 }
 protected void GV_RowCommand(object sender, GridViewCommandEventArgs e)
 {
     GridViewRow row = GridView1.Rows[Convert.ToInt32(e.CommandArgument)];
     //set the selected row.
     SelectedRow = row;
 }


At page 2 define the previous page type

XML
<%@ PreviousPageType VirtualPath="~/Default.aspx" %>


Then from the code behind of the page2...

C#
protected void Page_Load(object sender, EventArgs e)
{
    GridViewRow row = this.PreviousPage.SelectedRow;
    
}
 
Share this answer
 
v3
Comments
Dalek Dave 16-Apr-11 9:40am    
Good Answer.
Albin Abel 16-Apr-11 9:43am    
Thanks Dalek Dave
Use the OnRowCommand event handler of the GridView. This[^] will get you startd.
 
Share this answer
 
Comments
Dalek Dave 16-Apr-11 9:40am    
Good Link

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900