开发者

ASP.NET/HTML input button value on postback

According to the info in:

Which values browser collects as a postback data?

the value of the HTML input button is sent in a post back. I'm testing in ASP.NET with IE and I am not finding this to be the case.

The markup for my test case is:

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Test.aspx.vb" Inherits="Test" %>

<!DOCTYPE 开发者_开发技巧html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>test postback</title>
    <script type="text/javascript">
    function doTest() {
        var button = document.getElementById("btnTest");
        button.value = "new-value";
        alert("button contents = " + button.value);
        return true;
    }
    </script>
</head>
<body>
    <form id="myForm" runat="server">
    <div>
        <asp:Panel ID="pnlTest" runat="server"
            DefaultButton="btnTest">
            Textbox: 
            <asp:TextBox ID="txtTest" runat="server" />
            <asp:Button ID="btnTest" runat="server" 
                Text="change" OnClientClick="doTest()" />
        </asp:Panel>
    </div>
    </form>

The code behind is:

Partial Class Test
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        txtTest.Text = btnTest.Text
    End Sub
End Class

My result is that the value of the input button is always "change" when the browser loads the page, but I was expecting it to be "new-value" after postback. The Javascript doTest() function is changing the value when the button is clicked.

Is there something more I'm supposed to do for ASP.NET or IE to get the input button value posted back? Or is the information about this functionality wrong?


In a case like this I would probably use:

<input type="button" ID="btnTest" runat="server" onclick="doTest()" value="change" />

Note the runat="server". While asp:button probably renders similarly, if what you really want it an HTML button input, you can use that. Yes, ASP.NET will pick up the value on the server side.

Also, do a view source and make sure the ASP.NET panel is not munging up the ID of the input. More generally, have you tested this without the asp:panel tag? I wonder if that affects anything.


I believe IE just hates input submits....

But you should also know...

ASP uses viewstate to ensure there is no tampering with server controls. The value of the submit button is stored in the view state and most likely the only way to modify the value of it is to use the ASP.NET JS API.

More commonly you see this problem with <selects> (Options added to by javascript lost in postback), but <input type="submit" /> is very similar


It's not that it's not being set, but the javascript sets the value, which gets reset back to "change" on the postback. If you're looking for a button that works with your javascript, use the client input control:

<input type="button" ID="btnTest" onclick="doTest()" />

Otherwise, if you want a server control, you should set btnTest.Text on the server side.


You are using the wrong id for the button. ASP.NET gives each control a unique id. It is made up of all the ids in the chain to your control.

Therefore your button probably has an id something like ctl00_pnlTest_btnTest. This is why your JavaScript is not setting the buttons text.

view source in your browser to see the actual ID of the control and adjust your JavaScript accordingly.

From code you can get the actual ID used in the page with the ClientID property. So you could change your JavaScript to:

var button = document.getElementById("<%= btnTest.ClientID %>");


Just tried Marc's solution like this:

<script type="text/javascript">
        function doTest() {
            var button = document.getElementById("btnTest1");
            button.value = "new-value";
            alert("button contents = " + button.value);                
            return true;
        }
</script>


<input type="submit" id="btnTest1" name="btnTest1" value="Submit 1" runat="server" onclick="doTest()" />

When I posted back the Load event still had Submit 1 as the value of the button. You could use a hidden field, set that value with the button in JS and post back. That does in fact work.

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>test postback</title>
    <script type="text/javascript">
        function doTest() {
            var button = document.getElementById("btnTest1");
            button.value = "new-value";
            alert("button contents = " + button.value);
            var hdn = document.getElementById("hdnTextboxName");
            hdn.value = button.value;
            return true;
        }
    </script>
</head>
<body>
    <form id="myForm" runat="server">    
    <div>

        <asp:Panel ID="pnlTest" runat="server" DefaultButton="btnTest">
            Textbox:
            <asp:TextBox ID="txtTest" runat="server" /><asp:HiddenField ID="hdnTextboxName" runat="server" ClientIDMode="Static" />
            <asp:Button ID="btnTest" runat="server" Text="change" OnClientClick="doTest()" ClientIDMode="Static" />                        
        </asp:Panel>
    </div>
    </form>
</body>
</html>

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        txtTest.Text = hdnTextboxName.Value
    End Sub
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜