ASP.net的Visual Studio中继器linkbuttons [英] ASP.net Visual Studio Repeater with linkbuttons

查看:162
本文介绍了ASP.net的Visual Studio中继器linkbuttons的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个页面的加载,我要通过接口访问的名称列表,并创建链接按钮在该列表中的每个名称。也可以点击任何链接按钮导致另一页。

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.

我是新来ASP.net所以我不知道应该在哪里创建的链接按钮。起初我以为在.aspx文件中创建,但如何中继知道在列表中创建任意多个按钮,这样的话,我做到了在页面加载功能,并绑定名称的按钮。但是,这并不工作:​​

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

和为.aspx我只是有链接按钮中继对象:

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>

推荐答案

您必须把我以为是控制标签 Repeater1 控制则让你Repeater控件 OnItemDataBound 事件,并把你的code有排除此:

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

    }

编辑:
您的.aspx应该看起来是这样的:

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

这篇关于ASP.net的Visual Studio中继器linkbuttons的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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