【问题标题】:How do I set controls' values of a repeater control regardless to their data无论数据如何,如何设置转发器控件的控件值
【发布时间】:2009-06-06 20:16:45
【问题描述】:

这是中继器:

<asp:Repeater ID="rptrReports" runat="server">
            <ItemTemplate>
                <div style="margin: 2">
                    <asp:Label ID="lblAccount" runat="server" Text='<%#Eval("Account").FullName%>'  />&nbsp;
                    <asp:TextBox ID="txtDescription" runat="server" MaxLength="256" Text='<%#Eval("Description")%>'
                        ReadOnly="<%# Not Account.IsAdmin %>" BackColor="<%# If(Account.IsAdmin,Color.White, Color.Transparent)  %>"
                        BorderStyle="<%# If(admin, BorderStyle.NotSet, BorderStyle.None) %>" 
                        />&nbsp;
                    <asp:TextBox ID="txtNote" runat="server" MaxLength="1024" Text='<%#Eval("Note")%>'
                        ReadOnly="<%# Not Account.IsAdmin %>" BackColor="<%# If(Account.IsAdmin,Color.White, Color.Transparent)  %>"
                        BorderStyle="<%# If(admin, BorderStyle.NotSet, BorderStyle.None) %>" />&nbsp;
                    <!-- Here I have many more controls which I want to apply same rules !-->
                </div>
     </ItemTemplate>
</asp:Repeater>

我想在代码中动态设置 itemtemplate 的这些控件,这样 asp.net 代码不应该看起来那么难看:

Private Sub HandleTextBoxes()
    Dim admin = Account.IsAdmin
    For Each tb As TextBox In _
                        From c In rptrReports.Controls  _
                        Where TypeOf c Is TextBox 'ItemTemplate doesn't expose the properties :(
        With tb
            .ReadOnly = Not admin
            .BackColor = If(admin, Color.White, Color.Transparent)
            .BorderStyle = If(admin, BorderStyle.NotSet, BorderStyle.None)
        End With
    Next
End Sub

另一方面,我不想单独设置它或每个绑定的 ItemTemplate,我想通过父控件(中继器)本身来设置它。

C# 中的答案也将受到欢迎!

【问题讨论】:

    标签: asp.net .net-3.5 repeater web-controls


    【解决方案1】:

    我能想到的唯一方法是连接到ItemDataBoundRepeater

    protected void rptrReports_ItemDataBound(object sender, RepeaterItemEventArgs e) {
      var admin = Account.IsAdmin;
      var txtDescription = (TextBox) e.Item.FindControl("txtDescription");
      var txtNote = (TextBox) e.Item.FindControl("txtNote");
      txtDescription.ReadOnly = admin;
      txtDescription.BackColor = admin ? Color.White : Color.Transparent;
      //...
    }
    

    如果你想选择所有的文本框,你可以这样做:

    var textBoxes = e.Item.Controls.OfType<TextBox>();
    foreach (TextBox textBox in textBoxes) {
      // do stuff with the textBox...
    }
    

    如果不想使用ItemDataBound事件,可以把这段代码放在Page_PreRender方法中:

    protected void Page_PreRender(object sender, EventArgs e) {
      for (int i = 0 ; i < rInterlocuteurs.Items.Count ; i++) {
        var textBoxes = rInterlocuteurs.Items[i].Controls.OfType<TextBox>();
        foreach (TextBox textBox in textBoxes) {
          // do stuff with the textBox...
        }
      }
    }
    

    【讨论】:

    • 有没有办法找到中继器中的所有控件? Control[] 控制 = rpeater.FindAllControls();
    • 是的,您可以使用 e.Item.Controls 集合访问所有控件
    • 不,我不想访问 e 我想在项目绑定之前设置它。
    • 您无法访问中继器的 ItemTemplate 中的控件,因为最后,Repeater 只是 RepeaterItem 的集合,影响 ItemTemplte 不会对集合本身产生任何影响...
    【解决方案2】:

    VB:

    Private Shared Sub HandleTextBoxes(ByVal controls As ControlCollection)
        Dim admin = Account.IsAdmin
    
        If controls.Count > 0 Then
            For Each Control In controls
                HandleTextBoxes(Control.Controls)
            Next
        End If
    
        For Each tb As TextBox In _
                            From c In controls _
                            Where TypeOf c Is TextBox
            With tb
                .ReadOnly = Not admin
                .BackColor = If(admin, Color.White, Color.Transparent)
                .BorderStyle = If(admin, BorderStyle.NotSet, BorderStyle.None)
            End With
        Next
    End Sub
    

    C#:

    private static void HandleTextBoxes(ControlCollection controls)
    {
        var admin = Account.IsAdmin;
    
        if (controls.Count > 0) 
            foreach (var Control in controls)
                HandleTextBoxes(Control.Controls);        
    
        foreach (TextBox tb in controls) {        
            tb.ReadOnly = !admin;
            tb.BackColor = admin ? Color.White : Color.Transparent;
            tb.BorderStyle = admin ? BorderStyle.NotSet : BorderStyle.None;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2011-08-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-15
      相关资源
      最近更新 更多