C# .NET - How to find control of gridview in button event

Asked By abinav shankar on 18-Jan-12 01:05 AM
Hi

I want to find control of the gridview itemtemplate I used the code below and i am getting the value but because of using the
for loop i am getting only the last value for instance if i need the value of the first row I am getting only the value of second row i am pasting my code here pl help me how to do it

<asp:TemplateField HeaderText="Associate ID">
        <ItemTemplate>
        <asp:LinkButton ID="lnkAssId" Text='<%# Eval("associate_id") %>' runat="server" 
          onclick="lnkAssId_Click">LinkButton</asp:LinkButton>
        </ItemTemplate>
      </asp:TemplateField>
  
  
protected void lnkAssId_Click(object sender, EventArgs e)
  {
  for (int i = 0; i < grdApproval.Rows.Count; i++)
  {
    LinkButton lblAssId = (LinkButton)grdApproval.Rows[i].FindControl("lnkAssId");
    Session["AssId"] = lblAssId.Text;
  }
  
  }
D Company replied to abinav shankar on 18-Jan-12 01:10 AM
Hello Abinav,

You can try write this in grid row databound evens

for e.g

void grvValidCourses_RowDataBound(object sender, GridViewRowEventArgs e)
 {  
if (e.Row.RowType == DataControlRowType.DataRow)  
{  
 
// here fetch each row and call some function
   }
 }

Hope this will help you

Regards
D
Sandeep Mittal replied to abinav shankar on 18-Jan-12 01:24 AM
protected void lnkAssId_Click(object sender, EventArgs e)
{
  Session["AssId"] = ((LinkButton)sender).Text;     
}
dipa ahuja replied to abinav shankar on 18-Jan-12 03:29 AM
void bind()
{
  foreach (GridViewRow row in GridView1.Rows)
  {
    string name = ((Label)row.FindControl("label1")).Text.ToString();
    string gender = ((DropDownList)row.FindControl("dropdown1")).SelectedValue.ToString();
    string contact = ((Label)row.FindControl("lblcontact")).Text.ToString();
 
    //code to write
  }
}