回发时不保留表单字段

发布于 2024-09-12 10:21:50 字数 1184 浏览 10 评论 0原文

我正在使用 EPiServer 开发一个网站。我有一个自我提交的表格。 提交时,我检查是否缺少任何字段。如果是,则显示错误消息。

问题是我的字段在提交时被重置。

我可以使用 jQuery 来检查这一点,但我没有。我正在从后面的代码中检查这一点。

我尝试过设置 EnableViewState=true 几个地方,但没有运气。

这是我的代码的一部分:

<asp:Content ID="Content3" ContentPlaceHolderID="RightContentPlaceHolder" runat="server">
  <asp:Panel ID="panelComplaint" runat="server">
    <li>
        <h3>Postnummer*</h3>
        <input id="senderPostCode" name="postCode" type="text" size="20" enableviewstate="true" />
        <asp:Label runat="server" id="lblPostCode" CssClass="missingField" Text="Mangler tekst" Visible="false" />
    </li>
    <li>
        <h3>Post sted*</h3>
        <input id="senderCity" name="city" type="text" size="100" />
        <asp:Label runat="server" id="lblCity" CssClass="missingField" Text="Mangler tekst" Visible="false" />
    </li>
    <li>
      <div class="spacer10px"></div>
      <button type="submit" name="Send" >Send me</button>
    </li>
  </asp:Panel> 
</asp:Content>  

为了保留表单字段,我需要做什么?

I'm developing a website using EPiServer. I have a form which submits to itself.
On submit, I check if there are any fields missing. If yes, then error message is shown.

The problem is that my fields are reset when submitted.

I could check this using jQuery, but I'm not. I'm cheking this from code behind.

I've tried setting EnableViewState=true sevceral places, but no luck.

Here's part of my code:

<asp:Content ID="Content3" ContentPlaceHolderID="RightContentPlaceHolder" runat="server">
  <asp:Panel ID="panelComplaint" runat="server">
    <li>
        <h3>Postnummer*</h3>
        <input id="senderPostCode" name="postCode" type="text" size="20" enableviewstate="true" />
        <asp:Label runat="server" id="lblPostCode" CssClass="missingField" Text="Mangler tekst" Visible="false" />
    </li>
    <li>
        <h3>Post sted*</h3>
        <input id="senderCity" name="city" type="text" size="100" />
        <asp:Label runat="server" id="lblCity" CssClass="missingField" Text="Mangler tekst" Visible="false" />
    </li>
    <li>
      <div class="spacer10px"></div>
      <button type="submit" name="Send" >Send me</button>
    </li>
  </asp:Panel> 
</asp:Content>  

What do I need to do, in order to retain form fields?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

时光是把杀猪刀 2024-09-19 10:21:50

您是否尝试过将 runat="server" 添加到输入字段?据我所知,这是确保 asp.net 正确往返数据所必需的。我不确定 EPiServer 是否有任何特定的东西会使情况有所不同。

enableviewstate 属性,除非它特定于 EPiServer,否则实际上不会执行任何操作。

如果 ASP.net 混合 ID 存在问题,请考虑升级到 .NET 4.0(如果可以的话),因为这为您提供了控制 ID 修改方式的方法。如果没有,您可以尝试在页面中放置一个友好的翻译,然后您可以通过 JQuery 访问该页面,例如:

<script language="javascript" type="text/javascript">
  function getSenderPostCode() { return eval('<%=senderPostCode.ClientID%>'); }

  var senderPostCodeField = JQuery('#getsenderPostCode');

  alert(senderPostCodeField.length());
</script>

也就是说,引用“getSenderPostCode()”意味着您可以跳过使用 JQuery 获取引用的步骤到它,所以以下内容将起作用:

alert(getSenderPostCode().length();

通过将 runat="server" 添加到您的控件,您就不需要使用 Request.Form["senderPostCode"].ToString()< /code> 要访问该字段的内容,您可以直接访问它:

var contentOfSenderPostCode = senderPostCode.Value.ToString();

Have you tried adding runat="server" to the input fields? As far as I'm aware, this is needed to ensure that asp.net round-trips data properly. I'm not sure if there's anything specific to EPiServer that would make that differ.

The enableviewstate attribute, unless it's specific to EPiServer, won't actually be doing anything.

If having the ID mashed up by ASP.net is a problem, consider upgrading to .NET 4.0, if you can, as this provides ways for you to control how the ID's are modified. If not, you could try placing a friendly translation into the page that you can then access via JQuery, for example:

<script language="javascript" type="text/javascript">
  function getSenderPostCode() { return eval('<%=senderPostCode.ClientID%>'); }

  var senderPostCodeField = JQuery('#getsenderPostCode');

  alert(senderPostCodeField.length());
</script>

That said, having "getSenderPostCode()" to reference would mean that you could probably skip the step with JQuery of getting a reference to it, so the following would work:

alert(getSenderPostCode().length();

By adding runat="server" to your controls you then wouldn't need to use Request.Form["senderPostCode"].ToString() to access the content of that field, you could instead access it directly:

var contentOfSenderPostCode = senderPostCode.Value.ToString();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文