Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Apr 24th, 2012  |  syntax: None  |  size: 1.46 KB  |  hits: 11  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. How to select a row from grid view and edit the details in a new webform using ASP.NET
  2. <asp:HyperLinkField Text="View Details" DataNavigateUrlFields="YourId" DataNavigateUrlFormatString="details.aspx?id={0}" />
  3.        
  4. if(Request.QueryString["id"])!=null)
  5.   {
  6.       // Fetch respective item from database here & populate form fields  
  7.         // Open connection(); execute datareader;populate form fields
  8.    }
  9.        
  10. <asp:GridView ID="GV_YourGridView" runat="server" OnRowDataBound="gdView_RowDataBound" >
  11. </asp:GridView>
  12.        
  13. gdView.RowDataBound += new GridViewRowEventHandler(gdView_RowDataBound);
  14.        
  15. protected void gdView_RowDataBound(object sender, GridViewRowEventArgs e)
  16.     {
  17.         try
  18.         {
  19.             string value = e.Row.Cells[0].Text; //gets your Primary Key from cell on Databind
  20.             e.Row.Cells[0].Controls.Clear(); //Clears existing controls. (not necessary)
  21.             e.Row.Cells[0].Text = "";//Clears the innertext of the cell
  22.  
  23.             LinkButton LB = new LinkButton();//creates link button
  24.             LB.Text = value;//names it with your primarykey
  25.             LB.Attributes.Add("href", "view-edit.aspx?id=" + LB.Text);// links it to your edit page aswell as passing the primary key
  26.             LB.Attributes.Add("target", "_blank");//this makes your edit window open on new tab
  27.  
  28.             e.Row.Cells[0].Controls.Add(LB);
  29.         }
  30.         catch { }
  31.     }
  32.        
  33. if(Request.QueryString["id"])!=null)
  34. {      
  35.     string ID = Request.QueryString["id"];
  36. }