簡體   English   中英

從aspx.cs到aspx頁面的實現邏輯

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

我試圖讓所有 TextBox 控件在我的 .aspx 頁面中只讀。 我在我的 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);
}

顯然,我在 .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>

顯然它不會使我的文本框只讀。 我不認為邏輯有任何問題,這讓我相信它沒有被 .aspx 頁面解釋。

原始代碼片段中存在錯誤:

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

這里:

SetReadOnly(c, readOnly); 

應該是原來的方法:

SetTextBoxReadOnly(c, readOnly); 

正如大衛在評論中所說在頁面上調用這個方法(在它加載時)在 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