问题
First of all the ASPcode, the 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, then in the CodeBehind I have a Select from my database, where i select the ID_Contact, the Name, the Companydetails, which can be 1 per Row only. On the RowCreated event I get the UserID of the actual User, and I select all E-Mails the user has, can be 0-10 per row. Now my problem is: How can i insert Linkbuttons with the onClick-event in the description into this part of my code? Like 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 i want to add those controlls with code into THIS TemplateField. Is this possible ?
Thoughts i allready had: This.GridViewContacs.Controlls.AddAt(index,Linkbutton) But also no clue here how it should work.
Thanks in advance,
me
回答1:
Easiest is to add a placeholder control to the ItemTemplate, as ItemTemplate has no ID.
<asp:TemplateField>
<ItemTemplate>
<asp:PlaceHolder ID="emails" runat="server"></asp:PlaceHolder>
</ItemTemplate>
</asp:TemplateField>
and then in RowDataBound event
if (e.Row.RowType == DataControlRowType.DataRow)
{
PlaceHolder emails = e.Row.FindControl("emails") as PlaceHolder;
if (emails != null)
{
LinkButton lbEmail = new LinkButton();
lbEmail.Text = "your text";
lbEmail.Click += new EventHandler(SendEmail);
emails.Controls.Add(lbEmail);
}
}
Of course, the example is simplified. You can easily extend it to your needs.
来源:https://stackoverflow.com/questions/16398555/how-to-add-controlls-on-runtime-to-a-gridview