c# - 带有链接按钮的 ASP.net Visual Studio Repeater

标签 c# asp.net visual-studio-2010 repeater linkbutton

在加载页面时,我想通过界面访问名称列表,并为该列表中的每个名称创建链接按钮。单击任何链接按钮也会进入另一个页面。

我是 ASP.net 新手,所以我不确定应该在哪里创建链接按钮。起初我想在 .aspx 文件中创建它,但是转发器如何知道在列表中创建尽可能多的按钮,所以我在页面加载函数中完成了它并将名称绑定(bind)到按钮。但这不起作用:

 public void Repeater1_ItemDataBound(object sender, EventArgs e)
    {
        LinkButton lb = (LinkButton)this.FindControl("lb");

        IComparisonDataService ds = new ComparisonDataService();
        IList<string> apps = ds.GetApplicationList();
        foreach (var app in apps)
            lb.Text = app;
    }

对于 .aspx,我只有一个带有链接按钮的转发器对象:

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
  <link href="Styles/Layout.css" rel="stylesheet" type="text/css" />
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
  <div align="center" class="submitButton">
    <asp:Repeater ID="Repeater1" runat="server"  OnItemDataBound="Repeater1_ItemDataBound">
        <ItemTemplate>
            <asp:LinkButton ID="lb" runat="server" />
        </ItemTemplate>
    </asp:Repeater>
       </div>

最佳答案

您必须将 lb 控件(我认为是 Label)放入您的 Repeater1 控件中,然后在您的中继器控件上创建一个事件 OnItemDataBound 并将代码放在那里排除此:

protected void Page_Load(object sender, EventArgs e)
    {
        List<string> str = new List<string>{"I", "You", "They"};
        Repeater1.DataSource = str;
        Repeater1.DataBind();
    }

    protected void Repeater1_OnItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            LinkButton lb = (LinkButton)e.Item.FindControl("lb");
            string str = (string) e.Item.DataItem;
            lb.Text = str;
        }

    }

编辑: 您的 .aspx 应该如下所示:

<asp:Repeater ID="Repeater1" runat="server" OnItemDataBound="Repeater1_ItemDataBound">
     <%-- here you can also add some <HeaderTemplate> if you need a headers --%>
     <ItemTemplate>
         <%-- here you can put your controls --%>
         <asp:LinkButton ID="lb" runat="server"/>
     </ItemTemplate>
</asp:Repeater>

希望这有帮助:)

关于c# - 带有链接按钮的 ASP.net Visual Studio Repeater,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11792658/

相关文章:

c# - 在asp.net中同时执行代码行

c# - Image.Save() 抛出异常 "Value cannot be null./r/nParameter name: encoder"

c# - asp.net:如何检测 iOS/Android?

visual-studio-2010 - 如何完整发布Visual C#项目?

vb.net - 如何在 Visual Basic 2010 中读取 CSV 文件并在网格中显示结果?

c# - 从 IDesignTimeDbContextFactory<T> 访问 App.config

c# - 如何在 C# 中将 Dictionary<string, object> 转换为 Dictionary<string, string>

asp.net - 负载测试和分析 ASP.NET Web 应用程序的当前最佳实践是什么?

c# - 给 Paypal 订阅7天的宽限期

.net - 从哪里可以获得更多 VS2010 主题?