将JavaScript变量发送到asp.net webforms代码隐藏总是空的

时间:2013-09-27 16:36:42

标签: javascript asp.net webforms

我使用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:control。

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

1 个答案:

答案 0 :(得分:2)

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

修复HTML问题后,您可以摆脱该JavaScript,只需使用inAction的值。

W3C Markup Validation Service

你提到你有很奇怪的关闭表格标签问题。表单不能放在表单中,因此可能是您问题的根源。