【问题标题】:Creating server controls dynamically at runtime inside a grid item template在运行时在网格项模板内动态创建服务器控件
【发布时间】:2010-08-06 08:27:23
【问题描述】:

我在网格中有一个项目模板,其中有一个<asp:LinkButton/>。我将链接按钮的文本分配为

<%# Convert.ToString(Eval("Tags"))%>

标签可以有一个字符串,其中包含多个用空格分隔的标签。例如。 “sports”、“sports cricket”、“sports cricket sachin”是一些可能的标签示例。

我想为字符串中的每个标签创建一个按钮。如何在运行时在网格项模板内动态创建控件(服务器控件 - 链接按钮)?

谢谢

【问题讨论】:

    标签: c# asp.net grid dynamic


    【解决方案1】:

    我假设您所指的网格是数据网格?

    This tutorial 有一个很好的例子。总结一下:

    在您的网格视图中添加一个模板列来代替链接按钮列:

    <asp:datagrid id="dataGrid1" runat="server" Width="792px" 
    
        AutoGenerateColumns="False" 
        CellPadding="0" >
    
        <Columns>
    
    
            <asp:BoundColumn DataField="id" HeaderText="ID"> 
                <HeaderStyle Width="190px" HorizontalAlign="Center" >
                </HeaderStyle> 
            </asp:BoundColumn> 
    
    
            <asp:TemplateColumn HeaderText="Tags" 
                         HeaderStyle-HorizontalAlign="Center"> 
                <ItemStyle HorizontalAlign="Left" Wrap="True"></ItemStyle> 
                <ItemTemplate> 
                    <asp:Repeater ID="rptChild" runat="server" DataSource='<%# DataBinder.Eval(Container.DataItem, "tags").ToString().Split(tagSplitChars) %>'> 
                        <ItemTemplate> 
                            <asp:LinkButton ID="linkChild" 
                            runat="server" 
                                CommandArgument="<%# Container.DataItem%>"
                                > 
                                <%# Container.DataItem%> 
                            </asp:LinkButton> 
                        </ItemTemplate> 
                    </asp:Repeater> 
                </ItemTemplate> 
            </asp:TemplateColumn> 
        </Columns> 
        <PagerStyle PageButtonCount="20" Mode="NumericPages"></PagerStyle> 
    </asp:datagrid>
    

    请注意,tagSplitChars 应在您的代码中定义为:

     protected char[] tagSplitChars  = new char[] { ' '};
    

    显然,您可以根据需要向链接按钮添加“onclick”处理程序。

    我已经用后面的代码对此进行了测试,它运行良好:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    namespace Demo
    {
        public partial class WebForm1 : System.Web.UI.Page
        {
            public char[] splitChars = new char[] { ' '};
            protected void Page_Load(object sender, EventArgs e)
            {
                dataGrid1.DataSource = new List<dynamic>() { new { id = 1, names = "one single" }, new { id = 2, names = "two double" } };
                dataGrid1.DataBind();
            }
    
        }
    }
    

    更新

    如果您只是尝试添加按钮列表,并且没有其他列可以显示在您的网格中,您可以显着简化解决方案:

    <asp:Repeater ID="rptChild" runat="server" > 
        <ItemTemplate> 
            <asp:LinkButton ID="linkChild" runat="server"> 
                 <%# Container.DataItem%> 
            </asp:LinkButton> 
        </ItemTemplate> 
    </asp:Repeater> 
    

    然后在后面的代码中

            protected void Page_Load(object sender, EventArgs e)
            {
                dataGrid1.DataSource = myTagsString.split(splitChars);
                dataGrid1.DataBind();
            }
    

    显然,您必须手动访问数据表中的数据才能提取字符串,有一段时间没有这样做了,但从内存中这很简单。

    【讨论】:

    • 如何将标签作为数据源分配给整个网格?我还有其他专栏吗?标签只是该数据表上分配给网格的列(它实际上是 Telerik RadGrid)。从该列中,我需要获取“标签字符串”并需要将其拆分以获取实际标签。有什么想法吗?
    • 如果将标签列表分配给数据网格,则可以将 DataBinder.Eval(Container.DataItem, "tags").ToString().Split(tagSplitChars) 替换为 Container.DataItem.ToString ().Split(tagSplitChars)。但是,如果您希望数据网格的 1 列包含标记链接按钮列表,则答案中的代码有效。
    • 当我包含转发器控件时出现以下错误 - 服务器标签格式不正确。在 System.Web.UI.TemplateParser.ProcessError(String message) 在 System.Web.UI.TemplateParser.DetectSpecialServerTagError(String text, Int32 textPos) 在 System.Web.UI.TemplateParser.ParseStringInternal(String text, Encoding fileEncoding) 在 System .Web.UI.TemplateParser.ParseString(String text, VirtualPath virtualPath, Encoding fileEncoding) 有什么想法吗?
    • 当我移除中继器控制时,错误消失。我找不到报价不匹配之类的东西。
    • 好吧,我明白了。错过了一个结束标签。
    【解决方案2】:

    如果标签位于单个数据表列中,则构建一个 select Sql 查询,该查询从中获取标签值并将结果数据表或读取器分配为网格源。如果某些列值包含多个用逗号分隔的标签,请按照 Giles 的建议在运行时将它们拆分。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-04-20
      • 2011-02-23
      • 1970-01-01
      • 2011-04-29
      • 1970-01-01
      • 2013-10-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多