How to get rowindex in GridView when OnCommand argument used in the field
<asp:LinkButton ID="lblViewDetail" runat="server" style="text-decoration:underline;" OnCommand="ViewSummary" CommandArgument='<%#Eval("ShowlinkDetail") %>' Text='View Detail' />
I have one Menthod in .cs file and this method name call in the datafield
OnCommand="ViewSummary"
private void ViewSummary()
{
//code
}
when I click in the grid view on the开发者_Python百科 lable control then I want to get the Rowindex.Please help.
The OnCommand for your linkbutton would be something like this
protected void lnk_Command(object sender, CommandEventArgs e)
{
// The Namingcontainer would be Gridrow
// You can get the rowindex in this fashion
((sender as LinkButton).NamingContainer as GridViewRow).RowIndex
}
it will return your current row. you don't need to row index..
if(e.CommandName == "")
{
GridViewRow row = (GridViewRow)(((ImageButton)e.CommandSource).NamingContainer);
}
For LinkButton:
CommandArgument='<%#((GridViewRow)Container).RowIndex%>'
in .cs
:
protected void gv_CommDetails_RowCommand(object sender, GridViewCommandEventArgs e)
{
try
{
int index = Convert.ToInt32(e.CommandArgument); //Here you can get the index.
if (e.CommandName == "ViewSummary")
{
drp_Comm.Items.FindByValue(((HiddenField)gv_CommDetails.Rows[index].Cells[0].FindControl("hdn_CommCode")).Value).Selected = true;
txt_CommDescription.Text =gv_CommDetails.Rows[index].Cells[2].Text;
}
}
catch (Exception ee)
{
string message = ee.Message;
}
}
精彩评论