Wednesday, 2 May 2012

Get the Selected Row Index for the TextBox in the GridView

This is the Example for select the current TextBox in the gridview .

  • Asp Page

<asp:TemplateField HeaderText="Name">
 <ItemTemplate>
  <center>
    <asp:Label runat="server" ID="Name" Text='<%#Eval("name") %>'>       </asp:Label>
   </center>
  </ItemTemplate>
 <HeaderStyle BackColor="#6699CC" />
</asp:TemplateField>

 
Point to note: Text='<%# Eval("name") %>' This line is used to get the value from the database to the selected textbox 

  •  C# Sharp code select the Textbox in the gridview on click the Save button
  protected void btnSave_Click(object sender, EventArgs e)
    {
        GridViewRow grvrow = (GridViewRow)(((Control)sender).NamingContainer);
        TextBox txtName = (TextBox)grvrow.FindControl("txtName");
        if (txtName.Text == "")
        {
                    ((System.Web.UI.Page)HttpContext.Current.Handler).RegisterStartupScript("alert", "<script language='javascript'> alert('Please Enter the Name ')</script>");
        }
        else
         {
((System.Web.UI.Page)HttpContext.Current.Handler).RegisterStartupScript("alert", "<script language='javascript'> alert('Welcome ')</script>");
 
         }
 
 Point to note:grvrow is the object for Gridview Row and get the object for the textboxin the gridview.