c# - How to add Controlls on runtime to a Gridview? -


first of aspcode, problemdescription below.

<asp:gridview id="gridviewcontacts" runat="server" forecolor="#333333" datakeynames="l_id_contact" allowpaging="true" allowsorting="true"                  onpageindexchanging="gridviewcontacts_pageindexchanging" pagesize="25" autogeneratecolumns="false" onrowcommand="gv_contacts_rowcommand" >                     <footerstyle backcolor="#5d7b9d" font-bold="true" forecolor="white" />                     <rowstyle backcolor="#f7f6f3" forecolor="#333333" />                     <columns>                         <asp:templatefield headertext="edit">                             <itemtemplate>                                 <asp:linkbutton id="linkbuttonedit" runat="server" commandargument="edit" commandname="edit" text="edit">edit</asp:linkbutton>                             </itemtemplate>                         </asp:templatefield>                         <asp:templatefield headertext="view">                             <itemtemplate>                                 <asp:linkbutton id="linkbuttonview" runat="server" commandargument="view" commandname="view" text="view">view</asp:linkbutton>                             </itemtemplate>                         </asp:templatefield>                         <asp:templatefield headertext="name">                             <itemtemplate>                                 <asp:label id="l_name" runat="server" text='<%# eval("l_name") %>'></asp:label>                             </itemtemplate>                         </asp:templatefield>                         <asp:templatefield headertext="companydetails">                             <itemtemplate>                                 <asp:label id="l_companydetails" runat="server" text='<%# eval("l_companydetails") %>'></asp:label>                             </itemtemplate>                         </asp:templatefield>                         <asp:templatefield headertext="email">                             <itemtemplate>                              </itemtemplate>                         </asp:templatefield>                          <asp:templatefield visible="false" headertext="id_contact" >                             <itemtemplate>                                 <asp:label visible="false" id="l_id_contact" runat="server" text='<%# eval("l_id_contact") %>' />                             </itemtemplate>                         </asp:templatefield>                                                 </columns>                                             <%--  //stylesettings here--%>                 </asp:gridview> 

okay, in codebehind have select database, select id_contact, name, companydetails, can 1 per row only. on rowcreated event userid of actual user, , select e-mails user has, can 0-10 per row. problem is: how can insert linkbuttons onclick-event in description part of code? this:

                        <asp:templatefield headertext="email">                             <itemtemplate>                                 <asp:linkbutton[i] runat="server" onclick="sendemail">                                 </asp:linkbutton[i]>                                  <asp:linkbutton[i] runat="server" onclick="sendemail">                                 </asp:linkbutton[i]>                               </itemtemplate>                         </asp:templatefield> 

so want add controlls code templatefield. possible ?

thoughts allready had: this.gridviewcontacs.controlls.addat(index,linkbutton) no clue here how should work.

thanks in advance,

me

easiest add placeholder control itemtemplate, itemtemplate has no id.

<asp:templatefield>     <itemtemplate>         <asp:placeholder id="emails" runat="server"></asp:placeholder>     </itemtemplate> </asp:templatefield>  

and in rowdatabound event

if (e.row.rowtype == datacontrolrowtype.datarow) {     placeholder emails = e.row.findcontrol("emails") placeholder;      if (emails != null)     {         linkbutton lbemail = new linkbutton();         lbemail.text = "your text";         lbemail.click += new eventhandler(sendemail);          emails.controls.add(lbemail);     } } 

of course, example simplified. can extend needs.


Comments