通过客户端脚本更改时访问服务器端只读文本框值的解决方法 [英] Workarounds to access the Readonly Textbox value on server side when changed through client side script

查看:10
本文介绍了通过客户端脚本更改时访问服务器端只读文本框值的解决方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a Date Textbox in each row of a Grid View. As the users must not be allowed to put their own values in the text box, we have set the property as ReadOnly="true". And provided a calender java-script plug-in which sets the Textbox value. Now when I am trying to access the date Textbox on save click, the Textbox value is not persisted.

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

I knew about this issue and resolution for this too. We need to set the readonly attribute on server side to resolve this issue. But my biggest concern here is, I have placed the Textbox inside the Template field of Grid view. So I need to loop through each row of the Grid view, then get the reference to the date Textbox and then set the readonly attribute to readonly. Looks very cumbersome. Could anybody suggest other alternatives or work arounds.

代码如下

<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>

推荐答案

又找到了一个很酷的解决方案.只需用 HTML 输入类型文本替换 ASP 文本框并添加 runat="server" 属性,进一步添加只读属性.示例代码如下

Got another cool work around. Just replace the ASP Text box with HTML input type text and adding runat="server" attribute, further adding readonly attribute. Sample code as below

<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>

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

On the server side we can just add the below code for getting the value

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

希望这将节省大量编码.

Hope this will save lot of coding.

这篇关于通过客户端脚本更改时访问服务器端只读文本框值的解决方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆