ASP.net Visual Studio Repeater with linkbuttons

时间秒杀一切 提交于 2019-12-11 02:12:46

问题


On the loading of a page, I want to access a list of names via an interface and create link buttons for each name in that list. Also clicking any of the link buttons leads to another page.

I'm new to ASP.net so I'm not sure where the link buttons should be created. At first I thought to create it in the .aspx file but how does the repeater know to create as many buttons in the list so then I did it in the page load function and bind the names to the button. But this doesn't work:

 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;
    }

And for the .aspx I just have a repeater object with link button:

<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>


回答1:


You have to put lb control which I assume is Label into your Repeater1 control then make an event on you repeater control OnItemDataBound and put your code there exclude this:

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;
        }

    }

Edit: Your .aspx should seems like this:

<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>

Hope this helps :)




回答2:


Don't databind to a list of controls, databind to the actual data, in this case your list of strings stored in the variable apps. Then add a databinding expression to the template to set the properties of the LinkButton control:

<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" Height="200px" Width="150px" BackColor="#33CC33"
    BorderColor="Black" Font-Underline="False" Text='<%# Container.DataItem #>'></asp:LinkButton>
</ItemTemplate>
</asp:Repeater>


来源:https://stackoverflow.com/questions/11792658/asp-net-visual-studio-repeater-with-linkbuttons

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!