简体   繁体   English

从aspx.cs到aspx页面的实现逻辑

[英]Implementing logic from aspx.cs to aspx page

I am trying to make all TextBox controls read only in my .aspx page.我试图让所有 TextBox 控件在我的 .aspx 页面中只读。 I've written this in my aspx.cs page:我在我的 aspx.cs 页面中写了这个:

private void SetTextBoxReadOnly(Control parent, bool readOnly)
{
  // Get all TextBoxes and set the value of the ReadOnly property.
  foreach (var tb in parent.Controls.OfType<TextBox>())
    tb.ReadOnly = readOnly;

  // Recurse through all Controls
  foreach(Control c in parent.Controls)
    SetReadOnly(c, readOnly);
}

Obviously I have a ton of TextBoxes in the .aspx page such as this one:显然,我在 .aspx 页面中有大量的 TextBoxes,例如这个:

                <div class="form-group">
                    <asp:Label runat="server" AssociatedControlID="txtTimePointsExplained" CssClass="col-md-2 control-label">Explain why these time points:</asp:Label>
                    <div class="col-md-10">
                        <asp:TextBox ID="txtTimePointsExplained" TextMode="MultiLine" Rows="8" runat="server" CssClass="form-control"></asp:TextBox></div>
                </div>

Obviously it's not making my textboxes readOnly.显然它不会使我的文本框只读。 I don't think there is anything wrong with the logic, which leads me to believe it's not being interpreted by the .aspx page.我不认为逻辑有任何问题,这让我相信它没有被 .aspx 页面解释。

There was an error within the original code snippet:原始代码片段中存在错误:

private void SetTextBoxReadOnly(Control parent, bool readOnly)
{
  // Get all TextBoxes and set the value of the ReadOnly property.
  foreach (var tb in parent.Controls.OfType<TextBox>())
    tb.ReadOnly = readOnly;

  // Recurse through all Controls
  foreach(Control c in parent.Controls)
    SetReadOnly(c, readOnly);
}

Here:这里:

SetReadOnly(c, readOnly); 

should be the original method:应该是原来的方法:

SetTextBoxReadOnly(c, readOnly); 

As David said in the comments to invoke this method on the page (at the time it loads) Call this method in the Page_Load method:正如大卫在评论中所说在页面上调用这个方法(在它加载时)在 Page_Load 方法中调用这个方法:

protected void Page_Load(object sender, EventArgs e)
    {
        SetTextBoxReadOnly(this, true);
    }

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

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