【问题标题】:Send JavaScript variable to asp.net webforms code-behind is always empty将 JavaScript 变量发送到 asp.net webforms 代码隐藏始终为空
【发布时间】:2013-09-27 16:36:42
【问题描述】:

我使用this SO answer 作为向我的服务器端发送 JavaScript 变量的参考。但是,当我实施该解决方案时,JS alert() 中的所有内容都会正确显示,但是当我点击服务器时,我的隐藏字段的值始终为空。

JavaScript 和 HTML:

<script>
    function rblSelectionChange()
    {
        var selection = $('#inAction input:checked').val();
        var stuff = $('#<%= clientSelection.ClientID %>').val(selection);
        alert(stuff.val());
    }
</script>
<asp:HiddenField ID="clientSelection" runat="server" />
<div class="row-fluid">
    <asp:RadioButtonList runat="server" ID="inAction" ClientIDMode="Static">
        <asp:ListItem onClick="rblSelectionChange();" Value="RuEp" Text="I remember my <b>username</b>. Please email me a new <b>password</b>." />
        <asp:ListItem onClick="rblSelectionChange();" Value="ReEu" Text="I remember my <b>email</b>. Please email me my <b>username</b>." />
        <asp:ListItem onClick="rblSelectionChange();" Value="ReEup" Text="I remember my <b>email</b>. Please email me my <b>username</b> and a new <b>password</b>." />
    </asp:RadioButtonList>
</div>

在页面的提交事件中,我尝试抓取该值,但它是空的:

protected void btnActionSelect_Click(object sender, EventArgs e)
{
    string selection = clientSelection.Value;
    ...snip...
}

知道我错过了什么吗?

更新

我尝试将隐藏字段更改为纯 html,而不是 asp: 控件。

<input type="hidden" id="clientSelection" name="clientSelection" value="" />

我已将代码隐藏修改如下:

private string _selection = "";
protected void Page_Load(object sender, EventArgs e)
{
    //_selection = clientSelection.Value.ToString();
    if (IsPostBack)
        _selection = Request.Form["clientSelection"];
}

对于值 Request.Form["clientSelection"],我仍然一无所获。但重要的是它适用于 Chrome、FF 和 IE10。我试图让它工作的浏览器是 IE 7 8 和 9。我完全被难住了。

更新 2

根据请求,这是我在 IE10 中检查时页面的来源(浏览器和文档模式设置为 IE7)

<DIV id=ctl00_cphBodyWithForm_htmActionSelect class=row-fluid>
    <P class=lead>Can't access your account? Please select from the following options: </P>
    <SCRIPT>
                function rblSelectionChange()
                {
                    var selection = $('#inAction input:checked').val();
                    var stuff = $('#ctl00_cphBodyWithForm_clientSelection').val(selection);
                    alert(stuff.val());
                }
    </SCRIPT>
    <INPUT id=ctl00_cphBodyWithForm_clientSelection type=hidden name=ctl00$cphBodyWithForm$clientSelection jQuery191034119593101524303="7"> 
    <DIV class=row-fluid>
        <TABLE id=inAction border=0>
            <TBODY>
            <TR>
             <TD><INPUT onclick=rblSelectionChange(); id=inAction_0 type=radio value=RuEp name=ctl00$cphBodyWithForm$inAction jQuery191034119593101524303="8"><LABEL for=inAction_0>I remember my <B>username</B>. Please email me a new <B>password</B>.</LABEL></TD>
             </TR>
             ...snip...
            </TBODY>
        </TABLE>
     </DIV>
     <DIV class=span12>
        <A class="submitButton roundedBR" href="javascript:__doPostBack('ctl00$cphBodyWithForm$ctl00','')">Continue &gt; </A>
     </DIV>
</DIV>

【问题讨论】:

  • 你不能在代码隐藏文件中使用inAction.SelectedValue吗?
  • 出于某种原因,对于 IE 7 8 和 9,inAction.SelectedValue 总是为 null。这是我尝试一个笨拙的解决方案的根本原因。
  • 尝试 Request.Form["clientSelection"] 代替。
  • 尝试使用UniqueID 而不是ClientID
  • 试过 Request.Form["clientSelection"] 和 UniqueID,都不起作用。

标签: javascript asp.net webforms


【解决方案1】:

通过验证器运行您的 HTML。如果您的表单值未在某些浏览器中发布,则听起来您的 HTML 无效,这就是问题的根源。

一旦您修复了 HTML 问题,您就可以摆脱该 JavaScript 并简单地使用 inAction 的值。

W3C Markup Validation Service

你提到你有奇怪的结束表单标签问题。表单不能放在表单中,所以这可能是您问题的根源。

【讨论】:

  • 是的,我在主 webforms-form 中有一个用于搜索框的表单。感谢您的帮助,顺便说一句,MVC ftw。
  • 啊,我要等 23 小时才能获得赏金。
猜你喜欢
  • 2016-05-11
  • 2016-11-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多