【问题标题】:Changing Readonly attribute of Textbox within a repeater在转发器中更改文本框的只读属性
【发布时间】:2012-11-05 22:30:52
【问题描述】:

在我的 aspx 页面上,我有一个带有 5 个文本框和 1 个图像按钮的中继器来编辑行 这些文本框是只读的,要编辑它们,我需要它们不是只读的..

在我使用的后面代码中:

protected void EditRecipeInfo(object sender, CommandEventArgs e)
{
    ImageButton ib = sender as ImageButton;

    TextBox titleTXT = (TextBox)ib.FindControl("titleRepeat");
    TextBox qtyTXT = (TextBox)ib.FindControl("qtyRepeat");
    TextBox uomTXT = (TextBox)ib.FindControl("uomRepeat");
    TextBox prepTXT = (TextBox)ib.FindControl("prepRepeat");
    TextBox orTXT = (TextBox)ib.FindControl("orRepeat");

    titleTXT.ReadOnly = false;
    qtyTXT.ReadOnly = false;
    uomTXT.ReadOnly = false;
    prepTXT.ReadOnly = false;
    orTXT.ReadOnly = false;
    ////
}

但是当我触发这个事件时,断点显示该属性被设置为 false,但是当我单击删除文本框中的任何值时,它仍然像只读的一样

更新:

  <asp:Repeater ID="ingredRepeater" runat="server">
                <HeaderTemplate>
                    <table style="width: 100%">
                        <tr>
                            <th></th>
                            <th></th>
                            <th><h2>Title</h2></th>
                            <th><h2>Qty.</h2></th>
                            <th><h2>UoM</h2></th>
                            <th><h2>Prep.</h2></th>
                            <th><h2>Alternate</h2></th>
                        </tr>
                </HeaderTemplate>
                <ItemTemplate>
                    <tr>
                        <td>
                            <asp:ImageButton Style="height: 25px; width: 25px;" ImageUrl="/img/edit.png" Visible="true"
                                ID="editRecipeInfo" AutoPostBack="true" runat="server" OnCommand="EditRecipeInfo" CommandName='<%# DataBinder.Eval(Container, "DataItem.DetailID") %>' />
                        </td>
                        <td>
                            <asp:ImageButton ImageUrl="/img/RedX.png" ID="button2" runat="server" Height="20"
                                Width="20" CommandName='<%# DataBinder.Eval(Container, "DataItem.DetailID") %>'
                                OnCommand="deleteRecipeView" />
                        </td>
                        <td>
                            <asp:TextBox AutoPostBack="true" ReadOnly="true" ID="titleRepeat" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Name") %>'
                                size="45" />
                        </td>
                        <td>
                            <asp:TextBox  AutoPostBack="true" ReadOnly='true' ID="qtyRepeat" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Quantity") %>'
                                size="10" />
                        </td>
                        <td>
                            <asp:TextBox AutoPostBack="true"  ReadOnly='true' ID="uomRepeat" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.UnitsOfMeasure") %>'
                                size="10" />
                        </td>
                        <td>
                            <asp:TextBox AutoPostBack="true"  ReadOnly='true' ID="prepRepeat" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Prep") %>'
                                size="10" />
                        </td>
                        <td>
                            <asp:TextBox AutoPostBack="true"  ReadOnly='true' ID="orRepeat" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.AlternativeIngredients") %>'
                                size="20" />
                        </td>
                    </tr>
                </ItemTemplate>
                <FooterTemplate>
                    </table>
                </FooterTemplate>
            </asp:Repeater>

【问题讨论】:

  • 能否发布您的标记?
  • 点击图片中的按钮,触发事件

标签: c# asp.net textbox


【解决方案1】:

确保在将 ReadOnly 属性设置为 true 后没有重新绑定中继器。

【讨论】:

    【解决方案2】:

    我同意@Tariqulazam,标记会有所帮助。

    假设您的代码来自 ItemCommand 事件处理程序,我很惊讶地看到 FindControl 应用于 ImageButton。

    我猜你的代码应该是这样的:

    void rpAcces_ItemCommand(object source, RepeaterCommandEventArgs e)
      {
      //...
      ImageButton ib = sender as ImageButton;
    
      TextBox titleTXT = (TextBox)e.Item.FindControl("titleRepeat");
      TextBox qtyTXT = (TextBox)e.Item.FindControl("qtyRepeat");
      TextBox uomTXT = (TextBox)e.Item.FindControl("uomRepeat");
      TextBox prepTXT = (TextBox)e.Item.FindControl("prepRepeat");
      TextBox orTXT = (TextBox)e.Item.FindControl("orRepeat");
    
      titleTXT.ReadOnly = false;
      qtyTXT.ReadOnly = false;
      uomTXT.ReadOnly = false;
      prepTXT.ReadOnly = false;
      orTXT.ReadOnly = false;
      //...
     }
    

    另外,请注意,您无法在页面生命周期的后期重新绑定中继器而不会丢失这些更改。 并注意您的 TextBoxes 上设置的任何 Enabled 属性

    再一次,没有完整的代码很难回答。

    【讨论】:

    • 已修复。我的 Page_load,没有正确配置,我正在重新绑定转发器,它正在覆盖它。
    猜你喜欢
    • 2013-11-09
    • 1970-01-01
    • 2018-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-05
    相关资源
    最近更新 更多