简体   繁体   English

从转发器中的后面代码引用控件ID

[英]referencing control IDs from code behind that is in a repeater

This week I had a problem of trying to determine what control to use to be able to display data from multiple data sources in a non-grid looking way. 本周,我遇到了一个问题,即尝试确定要使用哪种控件才能以非网格方式显示多个数据源中的数据。 Thanks to this forum, I have learned that the best way will be to use a Repeater with different controls such as labels and tables inside the repeater. 多亏了这个论坛,我才知道最好的方法就是使用具有不同控件(例如,转发器中的标签和表格)的转发器。 Not quite sure exactly how I will get it all implemented yet but the first thing I need to learn is how to change the text from the code behind of a table and a Label that I will have inside the repeater. 不太确切地知道如何实现所有功能,但是我需要学习的第一件事是如何从转发器内部的表和标签后面的代码中更改文本。 I have read up and found that the following should work. 我已经阅读并发现以下方法应该可行。 but it doesnt. 但事实并非如此。

for (int i = 0; i <= Repeater1.Items.Count - 1; i++)
            {
                Label labelCustomerID = (Label)Repeater1.Items[i].FindControl("labelCustomerID");
                labelCustomerID.Text = "from code Behind";

            }

my declarative syntax is this: 我的声明式语法是这样的:

<asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDataSource1">
    <ItemTemplate>
            <hr/>
            <asp:Label runat="server" ID="labelCustomerId"  />
            <div style="padding-left:150px">
                <asp:Label runat="server" ID="labelCustomerName" />
                <asp:Label runat="server" ID="labelCustomerAddress" />
                <asp:Label runat="server" ID="labelCityState" />    
            </div>
            <asp:GridView runat="server" ID="gridViewRecordData">

            </asp:GridView>
            <hr/>
        </ItemTemplate>
 </asp:Repeater>

Thanks to EdB I have gotten this far. 多亏了EdB,我到此为止了。 Not still sure how I am gonna fill the gridview with one record of data but first things first... I need to be able to change a label's text. 仍不确定我将如何用一条数据记录填充gridview,但首先要首先...我需要能够更改标签的文本。 If i can get that done I am sure the rest will fall into place. 如果我能做到这一点,我相信其余的将就位。

Please help me! 请帮我! :) dell :)戴尔

Try with this code : 尝试使用以下代码:

for (int i = 0; i <= Repeater1.Items.Count - 1; i++) 
        {
            if(Repeater1.Items[i].ItemType == ListItemType.Item || Repeater1.Items[i].ItemType == ListItemType.AlternatingItem)
            {
                Label labelCustomerID = (Label)Repeater1.Items[i].FindControl("labelCustomerID"); 
                labelCustomerID.Text = "from code Behind"; 
            }

        } 

我认为您在填充中继器之前就已对其进行了引用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM