rahul ahuja

rahul ahuja

  • NA
  • 80
  • 34.2k

Click Event in the Asp.net gridview

Jul 11 2014 6:18 AM
Hi All,
I need help for click event in asp.net grid view.
On Row click of the grid view i want to populate data in the another grid by passing the id of the selected row.  
I go through the this link http://aspsnippets.com/demos/533/ but it  i need optimized code want to avoid lopping through the grid rows.
protected void OnRowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes["onclick"] = Page.ClientScript.GetPostBackClientHyperlink(GridView1, "Select$" + e.Row.RowIndex);
e.Row.ToolTip = "Click to select this row.";
}
}
protected void OnSelectedIndexChanged(object sender, EventArgs e)
{
foreach (GridViewRow row in GridView1.Rows)
{
if (row.RowIndex == GridView1.SelectedIndex)
{
row.BackColor = ColorTranslator.FromHtml("#A1DCF2");
string a= row.Cells[0].Text;
row.ToolTip = string.Empty;
}
else
{
row.BackColor = ColorTranslator.FromHtml("#FFFFFF");
row.ToolTip = "Click to select this row.";
}
}
this code contains loop for checking the selected row which i don't want to do is there any other way to do it.
 Thanks in advance.
 

Answers (1)