尝试填充使用中继器的表 [英] Trying to populate a table using repeaters

查看:125
本文介绍了尝试填充使用中继器的表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用一个数据库来填充表。我一直在试图效仿的例子不胜枚举,但无济于事。我觉得我失去了一些东西,因为这应该是这个硬...谢谢根本。

 <表>
        < ASP:直放站ID =RPT=服务器>
            <&HeaderTemplate中GT;
                &所述; TR>
                    百分位>< /第i
                < / TR>
            < / HeaderTemplate中>
            <&ItemTemplate中GT;
                &所述; TR>
                    &所述; TD>
                        < ASP:标签ID =数据=服务器文本='<%=的String.Format({0},My_Variable)%GT;' />
                    < / TD>
                < / TR>
            < / ItemTemplate中>
        < / ASP:直放站>
    < /表>

C#

 保护无效的Page_Load(对象发件人,EventArgs的发送)
    {
        康涅狄格州的SqlConnection =新的SqlConnection(ConfigurationManager.ConnectionStrings [_的connectionString]的ConnectionString);
        SqlDataAdapter的适配器=新的SqlDataAdapter(SELECT * FROM客户,康恩);
        数据表表=新的DataTable();
        adapter.Fill(表);
        rpt.DataSource =表;
        rpt.DataBind();
    }    私人无效rpt_ItemDataBound(对象发件人,RepeaterItemEventArgs E)
    {
        //如果(e.Item.ItemType ==
        标签L =(标签)e.Item.FindControl(数据);
        l.Text = e.Item.DataItem.ToString();
    }


解决方案

您应该不需要进行简单变量rpt_ItemDataBound。在你的.aspx文件,请尝试的eval()函数:

 <&ItemTemplate中GT;
    &所述; TR>
        &所述; TD>
            < ASP:标签ID =数据=服务器文本='<%#的eval(MyColumnName)%>' />
        < / TD>
    < / TR>
< / ItemTemplate中>

的eval()只能在模板,数据绑定控件的情况下。这需要你specfie​​d数据源,发现目前正由中继器呈现的项目,并让您对该项目访问成员属性。

您只需要在您需要渲染额外的东西,是不是在数据源的的ItemDataBound 事件,而是需要需要code逻辑图如何渲染。

I'm trying to populate a table using a database. I've been trying to follow numerous examples but to no avail. I feel like I'm missing something fundamental as this should be this hard... Thanks.

    <table>
        <asp:Repeater ID="rpt" runat="server">
            <HeaderTemplate>
                <tr>
                    <th></th>
                </tr>
            </HeaderTemplate>
            <ItemTemplate>
                <tr>
                    <td>
                        <asp:Label ID="data" runat="server" Text='<%= string.Format("{0}", My_Variable) %>' />
                    </td>
                </tr>
            </ItemTemplate>
        </asp:Repeater>
    </table>

C#

   protected void Page_Load(object sender, EventArgs e)
    {
        SqlConnection Conn = new SqlConnection(ConfigurationManager.ConnectionStrings["_connectionString"].ConnectionString);
        SqlDataAdapter adapter = new SqlDataAdapter("SELECT * FROM Customer", Conn);
        DataTable table = new DataTable();
        adapter.Fill(table);
        rpt.DataSource = table;
        rpt.DataBind();
    }

    private void rpt_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        //if (e.Item.ItemType == 
        Label l = (Label)e.Item.FindControl("data");
        l.Text = e.Item.DataItem.ToString();
    }

解决方案

You shouldn't need the rpt_ItemDataBound for most simple variables. In your .aspx file, try the Eval() function instead:

<ItemTemplate>
    <tr>
        <td>
            <asp:Label ID="data" runat="server" Text='<%#Eval("MyColumnName") %>' />
        </td>
    </tr>
</ItemTemplate>

Eval() only works in the context of templated, databound controls. It takes the data source you specfied, finds the item currently being rendered by the repeater, and lets you access member properties on that item.

You only need to use the ItemDataBound event when you need to render extra stuff that isn't in the data source, but requires need code logic to figure out how to render.

这篇关于尝试填充使用中继器的表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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