c# - cannot find controls in gridview and get an error(Index was out of range) -


actually have problem finding linkbutton control in gridview . have 2 gridviews 1 of them inside 1 problem cannot value of linkbutton of second gridview,

here code

<asp:gridview id="gridview1" runat="server" autogeneratecolumns="false"  borderstyle="none" datasourceid="sqldatasource2" gridlines="none"> <columns>     <asp:templatefield showheader="false">         <itemtemplate>                     <table align="center" class="table3">                         <tr>                             <td >                             <asp:label id="lblid" runat="server" visible="false" text='<%# eval("food_id") %>'></asp:label>                                <b> <%#eval("title")%></b>                             </td>                         </tr>                         <tr>                             <td align="center" class="imgkidfood">                                 <asp:image id="img"  width="680px" height="145px" imageurl='<%#eval("pictures") %>' runat="server" />                             </td>                         </tr>                         <tr>                             <td style="direction:rtl; text-align:right;">                                    <asp:gridview id="showfoodmenu2" runat="server" autogeneratecolumns="false"                                      borderstyle="none" gridlines="none" showheader="false"                                      datasourceid="sqldatasource1" width="100%"                                       >                                     <columns>                                         <asp:templatefield>                                             <itemtemplate>                                              <table align="center" class="tablelistmenu">                                             <tr>                                             <td  class="add">                                             <asp:linkbutton id="linkbutton2" commandargument="<%# container.dataitemindex %>"                                                      runat="server" causesvalidation="false"                                                      commandname="select" text="select"                                                       onclick="linkbutton2_click" ></asp:linkbutton>                                                    </td>                                                  <td class="tocenter">                                                        <b><%#eval("title_pr") %>                                                        </b>                                                    <asp:linkbutton id="linkbutton12" runat="server" commandargument="<%# container.dataitemindex %>"  commandname="link2" text='<%# eval("menu_id") %>'></asp:linkbutton>                                                 </td>                                                   <td class="priceleft">                                                        <%#eval("price") %>                                                 </td>                                              </tr>                                         </table>                                         <hr />                                             </itemtemplate>                                         </asp:templatefield>                                      </columns>                                 </asp:gridview>                                  <br /><br /><br />                                  </td>                         </tr>                     </table>           </itemtemplate>     </asp:templatefield> </columns> 

as u can see second gidview named id showfoodmenu2 , it's inside first girdview id giridview1 .

and have 2 linkbuttons inside second gridview 1 of them keep value(which linkbutton12) , one(linkbutton2) when clicked add record in database .

but when clicked on linkbutton(linkbutton2) show value of linkbutton12 , ann error

here error

http://hidelion.com/images/error.png

and here my.cs code

  protected void linkbutton2_click(object sender, eventargs e)     {          gridview g = new gridview();         g.findcontrol("showfoodmenu2");         system.threading.thread.sleep(1000);         linkbutton m = (linkbutton)sender;         int = int32.parse(m.commandargument);         linkbutton lblmososid = (linkbutton)g.rows[i].findcontrol("linkbutton2");        // linkbutton lblmososid2 = (linkbutton)g.rows[i].findcontrol("linkbutton12");            label1.text = lblmososid.text;     } 

so how can solved problem ??????

gridview g = new gridview(); create new instance of gridview class, , not find gridview automatically, messing up, go this, linkbutton find corresponding row, row fetch control wish manipulate, this:-

protected void linkbutton2_click(object sender, eventargs e)     {         linkbutton linkbutton2 = sender linkbutton;         gridviewrow grdrow = (gridviewrow)linkbutton2.namingcontainer;         linkbutton linkbutton12 = (linkbutton)grdrow.findcontrol("linkbutton12 ");         label1.text = linkbutton12.text;     } 

Comments

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -