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

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

问题描述

我有一个网格视图中的每一行日期文本框。由于用户绝不允许把自己的价值观在文本框中,我们设置属性为只读=真。并提供了日历的java脚本插件,它设置文本框的值。现在,当我试图访问保存单击日期文本框,文本框的值不会保留。

我知道这个问题和解决方案这一点。我们需要设置服务器端的只读属性来解决此问题。但我在这里最关心的是,我已经把网格视图的模板字段中的文本框。于是我通过网格视图中的每一行需要循环,然后拿到参考日期文本框,然后只读属性只读设置。看起来很繁琐。任何人可以提出其他方案或变通。

code如下

 < ASP:GridView控件ID =gv_sample=服务器的AutoGenerateColumns =false的>
 <柱体和GT;
   < ASP:的TemplateField>
     <&ItemTemplate中GT;
      <表格的cellpadding =0CELLSPACING =0BORDER =0>
       &所述; TR>
        &所述; TD>
         < ASP:文本框=服务器ID =txtByWhen只读=真的CssClass =输入框文本='<%#的eval(ByWhen)%>'>< / ASP:文本框&GT ;
       < / TD>
        < TD风格=填充左:2px的;>
         < IMG SRC =../图像/ dateico.gifALT =选择日期称号=选择日期
         的onclick =JavaScript的:返回showCalendar('<%#((GridViewRow)集装箱).FindControl(txtByWhen)的ClientID%GT;','DD / MM / Y'); />
      < / TD>
     < / TR>
    < /表>
   < / ItemTemplate中>
  < / ASP:的TemplateField>
 < /专栏>
< / ASP:GridView的>


解决方案

得到各地的另一个很酷的工作。刚替换HTML输入文本类型和添加=服务器属性的ASP文本框,从而进一步增加只读属性。示例code如下

 < ASP:GridView控件ID =gv_sample=服务器的AutoGenerateColumns =false的>
 <柱体和GT;
   < ASP:的TemplateField>
     <&ItemTemplate中GT;
      <表格的cellpadding =0CELLSPACING =0BORDER =0>
       &所述; TR>
        &所述; TD>
         <输入类型=文本=服务器ID =txtByWhen只读=只读级=输入框
         风格=宽度:50像素;值='<%#的eval(ByWhen)%>' />
       < / TD>
        < TD风格=填充左:2px的;>
         < IMG SRC =../图像/ dateico.gifALT =选择日期称号=选择日期
         的onclick =JavaScript的:返回showCalendar('<%#((GridViewRow)集装箱).FindControl(txtByWhen)的ClientID%GT;','DD / MM / Y'); />
      < / TD>
     < / TR>
    < /表>
   < / ItemTemplate中>
  < / ASP:的TemplateField>
 < /专栏>
< / ASP:GridView的>

在服务器端,我们可以只添加以下code为获取值

 的foreach(在RSGV_ScoreCard.Rows GridViewRow GVR)
{
 使用(的HtmlInputText txtByWhen =(的HtmlInputText)gvr.FindControl(STR_BYWHEN))
  {
    字符串日期= txtByWhen.Value.Trim();
  }
}

希望这将节省编码不少。

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.

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.

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

解决方案

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天全站免登陆