gpt4 book ai didi

javascript - 通过客户端脚本更改时在服务器端访问只读文本框值的解决方法

转载 作者:行者123 更新时间:2023-11-29 22:30:38 25 4
gpt4 key购买 nike

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

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

代码如下

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

最佳答案

你是对的,解决方案是在Codebehind中设置Attribute。 (参见 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");
}
}

关于javascript - 通过客户端脚本更改时在服务器端访问只读文本框值的解决方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7106000/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com