如何在运行时添加Controlls到GridView? [英] How to add Controlls on runtime to a Gridview?

查看:219
本文介绍了如何在运行时添加Controlls到GridView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所有的ASP code首先,下方problemdescription。

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>

好吧,然后在codeBehind我已经从我的数据库,在那里我选择ID_Contact,名称,Companydetails,可以是1仅占A行选择。
在RowCreated事件中,我得到实际的用户的用户ID,而我选择所有电子邮件的用户,可以是0-10每行。
现在我的问题是:
我怎么可以插入在描述onClick的事件Linkbuttons到我的code的这一部分?
像这样的:

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>

所以我想用code那些controlls加入到这个模板列。
这可能吗?

So i want to add those controlls with code into THIS TemplateField. Is this possible ?

我思考了媒体链接:
This.GridViewContacs.Controlls.AddAt(指数的LinkBut​​ton)
但这里也没有线索应该如何工作。

Thoughts i allready had: This.GridViewContacs.Controlls.AddAt(index,Linkbutton) But also no clue here how it should work.

由于提前,

推荐答案

最简单的是一个占位符控件添加到ItemTemplate中,作为ItemTemplate中没有ID。

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> 

,然后在RowDataBound事件

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.

这篇关于如何在运行时添加Controlls到GridView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆