c# - Get Gridview RowIndex outside of the gridview -


my button1 inside panel, want access rowindex hide imagebutton. when enter debug mode, gridview1.selectedindex has null value. pleaase help!

  protected void button1_click1(object sender, eventargs e)     {          foreach (gridviewrow row in gridview1.rows)         {             if (row.rowtype == datacontrolrowtype.datarow)             {                 if (row.rowindex == convert.toint32(gridview1.selectedindex))                 {                     imagebutton stopbutton = (imagebutton)row.findcontrol("stopimagebutton");                     imagebutton startbutton = (imagebutton)row.findcontrol("startimagebutton");                     stopbutton.visible = true;                     startbutton.visible = false;                           }             }         }          this.stoptimenotespanel_modalpopupextender.hide();     } 

you said button inside panel. able handle gridview events effectively, use button inside gridview itself.

or if still want use button in panel, then,

1.) first add select button inside gridview. select row using select button ,

2.) click button in panel.

gridview.selectedindex set when have selected row of grid view. 2 ways possible:

1.) set autogenerateselectbutton property true.

<asp:gridview id="customersgridview"         datasourceid="customerssource"         autogenerateselectbutton="true"        runat="server"> 

2.) add buttonfield inside section of gridview as:

<asp:gridview id="customersgridview"          datasourceid="customerssqldatasource"          autogeneratecolumns="false"         runat="server">        <columns>           <asp:buttonfield buttontype="button"              commandname="select"             headertext="select customer"              text="select"/>           <asp:boundfield datafield="companyname"              headertext="company name"/>        </columns> </asp:gridview> 

now after row selected, 2 events of gridview fired: selectedindexchanging & selectedindexchanged.

only when required, step below selectedrow in selctedindexchangedevent

void customersgridview_selectedindexchanged(object sender, eventargs e)   {      // selected row using selectedrow property.     gridviewrow row = customersgridview.selectedrow;     messagelabel.text = "you selected " + row.cells[2].text; // display    } 

now, inside button click event , selected index:

protected void button1_click1(object sender, eventargs e) {      int = customersgridview.selectedindex; } 

Comments

Popular posts from this blog

html5 - What is breaking my page when printing? -

html - Unable to style the color of bullets in a list -

c# - must be a non-abstract type with a public parameterless constructor in redis -