.net - get cell value in gridview -
i want text literal inside gridview,
but when run program, exception through
unable cast object of type system.web.ui.literalcontrol type system.web.ui.databoundliteralcontrol.
.aspx code:
<asp:gridview id="gridview3" runat="server" onrowdatabound="rowdatabound" datakeynames="qno" autogeneratecolumns="false" showfooter="true" onrowcancelingedit="cancel" onrowcommand="create" onrowdeleting="delete" onrowediting="edit" onrowupdating="update"> <columns> <asp:templatefield headertext="selection"> <itemtemplate> <asp:checkbox id="check1" runat="server"/> </itemtemplate> </asp:templatefield> <asp:templatefield headertext="id" visible="true"> <itemtemplate> <asp:label id="id7" runat="server" text='<%#eval("assessid") %>' ></asp:label> </itemtemplate> </asp:templatefield> <asp:templatefield headertext="qno" visible="true"> <itemtemplate> <asp:dropdownlist appenddatabounditems="true" autopostback="true" id="dropdownlist1" runat="server"> <asp:listitem ></asp:listitem> </asp:dropdownlist> </itemtemplate> </asp:templatefield> <asp:templatefield headertext="description" visible="true"> <itemtemplate> <asp:literal id="id6" runat="server" text='<%#eval("description") %>' > </asp:literal> </itemtemplate> <edititemtemplate> <asp:textbox id="textbox1" text='<%#eval("description") %>' runat="server" ></asp:textbox> </edititemtemplate> <footertemplate> <asp:textbox id="textbox3" runat="server" ></asp:textbox> </footertemplate> </asp:templatefield> <asp:templatefield headertext="strongagree" visible="true"> <edititemtemplate> = <asp:templatefield headertext="action" visible="true"> <itemtemplate> <asp:linkbutton id="linkbutton5" text="edit" commandname="edit" runat="server"></asp:linkbutton> </itemtemplate> <edititemtemplate> <asp:linkbutton id="linkbutton1" text="update" commandname="update" runat="server"></asp:linkbutton> <asp:linkbutton id="linkbutton3" text="cancel" commandname="cancel" runat="server"></asp:linkbutton> </edititemtemplate> <footertemplate> <asp:linkbutton id="linkbutton7" text="deleteall" commandname="delete" runat="server"></asp:linkbutton> </footertemplate> </asp:templatefield> <asp:templatefield headertext="" visible="true"> <itemtemplate> <asp:linkbutton id="linkbutton23" text="delete" commandname="delete" runat="server"></asp:linkbutton> </itemtemplate> <footertemplate> <asp:button id="bdh" text="insert " commandname="insert" runat="server" /> </footertemplate> </asp:templatefield> </columns> <footerstyle backcolor="#990000" font-bold="true" forecolor="white" /> <headerstyle backcolor="#990000" font-bold="true" forecolor="white" /> <pagerstyle backcolor="#ffcc66" forecolor="#333333" horizontalalign="center" /> <rowstyle backcolor="#fffbd6" forecolor="#333333" /> <selectedrowstyle backcolor="#ffcc66" font-bold="true" forecolor="navy" /> <sortedascendingcellstyle backcolor="#fdf5ac" /> <sortedascendingheaderstyle backcolor="#4d0000" /> <sorteddescendingcellstyle backcolor="#fcf6c0" /> <sorteddescendingheaderstyle backcolor="#820000" /> </asp:gridview>
code behind
protected void rowdatabound(object sender, gridviewroweventargs e) { if (e.row.rowtype == datacontrolrowtype.datarow) { string percentage = ((literalcontrol)e.row.cells[2].controls[0]).text; } }
the literal control getting not 1 want (there literal matching whitespace). try id:
string percentage = ((literalcontrol)e.row.findcontrol("id6").text;
or perhaps try index:
string percentage = ((literalcontrol)e.row.cells[2].controls[1]).text;
Comments
Post a Comment