c# - How add control programmatically in gridview template? -
i want add control label in gridview, possible add datatable? here code:
<asp:gridview id="reportscheduledetailsgridview" runat="server" autogeneratecolumns="false"> </asp:gridview>
i try use tag html span, not render:
string querystring = @"select * [table1]"; sqlcommand cmd = new sqlcommand(querystring, connokto); using (sqldatareader sdrmaster = cmd.executereader()) { while (sdrmaster.read()) { datarow rows = datatable.newrow(); rows[0] = sdrmaster["name"].tostring(); (var x = 1; x < maxcol; x++) { querystring = @"select * table2"; cmd = new sqlcommand(querystring, connokto); using (sqldatareader sdrrev = cmd.executereader()) { while (sdrrev.read()) { blok = "<span></span>"; no = (int)int16.parse(sdrrev["no"].tostring()); } } rows[x] = blok; if (no > 1) { no--; } else { blok = ""; } } datatable.rows.add(rows); } }
i don't know, how can add control asp in gridview, label. please help, thanks.
one way add controls using onrowdatabound
event of gridview. add placeholder say, inside <itemtemplate>
of grid view.
<asp:gridview id="empgridview" onrowdatabound="empgridview_rowdatabound" <itemtemplate> <asp:placeholder id="placeholder1" runat="server"></asp:placeholder> </itemtemplate> ...></asp:gridview>
ang code behind file have:
protected void empgridview_rowdatabound(object sender, gridviewroweventargs e) { if (e.row.rowtype == datacontrolrowtype.datarow) { // create label control label lbl = new label(); lbl.text="mydynamic label"; lbl.id="lbl1"; // use id values prefer // lets create 1 more control example linkbutton btnlink = new linkbutton(); btnlink.text = "delete"; btnlink.id = "btndelete"; linkb.click += new eventhandler(btnlink_click); // add controls placeholder inside <itemtemplate> placeholder phld = e.row.findcontrol("placeholder1") placeholder; phld.controls.add(btnlink); phld.controls.add(lbl); //code add control specific column/ cell e.row.cells[1].controls.add(btnlink); // adding 2nd column // adding last column.. e.row.cells[empgridview.columns.count - 1].controls.add(btnlink); } }
hope various ways of adding controls templates in cell of gridview helps you.
Comments
Post a Comment