【问题标题】:Workarounds to access the Readonly Textbox value on server side when changed through client side script通过客户端脚本更改时访问服务器端只读文本框值的解决方法
【发布时间】:2011-10-29 16:16:48
【问题描述】:

我在网格视图的每一行中都有一个日期文本框。由于不允许用户在文本框中输入自己的值,我们将属性设置为 ReadOnly="true"。并提供了一个设置Textbox值的calender java-script插件。现在,当我尝试在保存单击时访问日期文本框时,文本框值不会保留。

我也知道这个问题和解决方案。我们需要在服务器端设置只读属性来解决这个问题。但我最关心的是,我已将文本框放在网格视图的模板字段中。所以我需要遍历网格视图的每一行,然后获取对日期文本框的引用,然后将只读属性设置为只读。看起来非常麻烦。任何人都可以建议其他替代方案或解决方法。

代码如下

<asp:GridView ID="gv_sample" runat="server" AutoGenerateColumns="false">
 <Columns>
   <asp:TemplateField>
     <ItemTemplate>
      <table cellpadding="0" cellspacing="0" border="0">
       <tr>
        <td>
         <asp:TextBox runat="server" ID="txtByWhen" ReadOnly="true" CssClass="inputbox" Text='<%#Eval("ByWhen") %>'></asp:TextBox>
       </td>
        <td style="padding-left: 2px;">
         <img src="../Images/dateico.gif" alt="Select a date" title="Select a date" 
         onclick="JavaScript:return showCalendar('<%#((GridViewRow)Container).FindControl("txtByWhen").ClientID %>','dd/mm/y');" />
      </td>
     </tr>
    </table>
   </ItemTemplate>
  </asp:TemplateField>
 </Columns>
</asp:GridView>

【问题讨论】:

    标签: javascript asp.net gridview textbox readonly


    【解决方案1】:

    尽量不要通过更改其属性将文本框直接设为只读。

    我们可以在代码隐藏中添加以下代码行并将文本框设为只读,但我们仍然可以通过回发在客户端保留其更改的值。

    TextBox1.Attributes.Add("readonly", "readonly");

    【讨论】:

      【解决方案2】:

      终于帮我解决了:)

      我所做的是从文本框中删除了只读属性并添加了一个 ForceReadOnly 类,其中我没有返回如下内容:

          $.fn.ForceReadOnly =
      function () {
          return this.each(function () {
              $(this).keydown(function (e) {
                  var key = e.charCode || e.keyCode || 0;
                  return (key == 9);
              });
          });
      };
      
      
      $(".ForceReadOnly").ForceReadOnly();

      所以我们从事件中获得了只读而不是属性

      【讨论】:

        【解决方案3】:

        得到了另一个很酷的解决方法。只需将 ASP 文本框替换为 HTML 输入类型文本并添加 runat="server" 属性,进一步添加 readonly 属性。示例代码如下

        <asp:GridView ID="gv_sample" runat="server" AutoGenerateColumns="false">
         <Columns>
           <asp:TemplateField>
             <ItemTemplate>
              <table cellpadding="0" cellspacing="0" border="0">
               <tr>
                <td>
                 <input type="text" runat="server" id="txtByWhen" readonly="readonly" class="inputbox"
                 style="width: 50px;" value='<%#Eval("ByWhen") %>' />
               </td>
                <td style="padding-left: 2px;">
                 <img src="../Images/dateico.gif" alt="Select a date" title="Select a date" 
                 onclick="JavaScript:return showCalendar('<%#((GridViewRow)Container).FindControl("txtByWhen").ClientID %>','dd/mm/y');" />
              </td>
             </tr>
            </table>
           </ItemTemplate>
          </asp:TemplateField>
         </Columns>
        </asp:GridView>
        

        在服务器端,我们只需添加以下代码即可获取值

        foreach (GridViewRow gvr in RSGV_ScoreCard.Rows)
        {
         using (HtmlInputText txtByWhen = (HtmlInputText)gvr.FindControl(STR_BYWHEN))
          {
            string date=txtByWhen.Value.Trim();
          }
        }
        

        希望这将节省大量编码。

        【讨论】:

          【解决方案4】:

          你是对的,解决方案是在 Codebehind 中设置属性。 (见this question

          您不必循环遍历 GridView,只需在 RowCreated-Event 中执行即可:

          protected void Page_Load(object sender, EventArgs e)
          {
              gv_sample.RowCreated += new GridViewRowEventHandler(gv_sample_RowCreated);
          }
          
          void gv_sample_RowCreated(object sender, GridViewRowEventArgs e)
          {
              if (e.Row.RowType == DataControlRowType.DataRow)
              {
                  TextBox txtByWhen = (TextBox)e.Row.FindControl("txtByWhen");
                  txtByWhen.Attributes.Add("readonly", "readonly");
              }
          }
          

          【讨论】:

            猜你喜欢
            • 2014-05-10
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多