简体   繁体   English

传递 Javascript 更新的值<input type="button">到 ASP.NET 代码后面

[英]Passing a Javascript-updated value from <input type="button"> to ASP.NET code behind

I've got a set of buttons in my ASP.NET webpage that look like this:我的 ASP.NET 网页中有一组按钮,如下所示:

<input id="B1" type="button" onclick="return change(this)" value="A" runat="server" />

The Javascript function would allow the button value to change between a set of values when clicked. Javascript 函数将允许按钮值在单击时在一组值之间更改。

function change(elem)
    {
        if (elem.value === "A")
            elem.value = "B";
        else if (elem.value === "B")
            elem.value = "C";
        else
            elem.value = "A";
    }

In the code behind, the button value is passed into a SQL query:在后面的代码中,按钮值被传递到 SQL 查询中:

cmd.Parameters.AddWithValue("@b1", B1.Value);

Ideally, when the entire webpage form is submitted, the current button values should be passed to the code behind.理想情况下,当整个网页表单提交时,当前按钮值应该传递给后面的代码。 When inspecting the change through inspecting the element, I can verify that the value does update to the next option, and so on.通过检查元素检查更改时,我可以验证该值是否更新到下一个选项,依此类推。 But it seems to only pass the hardcoded value found in the input tag, ie.但它似乎只传递在输入标签中找到的硬编码值,即。 value "A" is sent to the code behind, regardless of whether the user changed the value to the other options.值“A”被发送到后面的代码,无论用户是否将值更改为其他选项。

There are no issues with the SQL query, as the "default" button values are inserted into the database. SQL 查询没有问题,因为“默认”按钮值已插入到数据库中。

Any insights would help.任何见解都会有所帮助。 Thanks in advance.提前致谢。

when you use type="button" changed value can't be captured on post back, may be you could try using hidden input elements to achieve.当您使用 type="button" 时,无法在回发时捕获更改的值,可能您可以尝试使用隐藏的输入元素来实现。

<script>
    function change(elem) {

        if (elem.value === "A")
            elem.value = "B";
        else if (elem.value === "B")
            elem.value = "C";
        else
            elem.value = "A";
        document.getElementById("<%=B1.ClientID%>").value = elem.value;

    }
</script>

HTML HTML

 <input type="button" onclick="return change(this)" value="A" runat="server" />
<input id="B1" type="hidden" value="A" runat="server" />
<script

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM