views:

185

answers:

5

According to the info in:

http://stackoverflow.com/questions/669797/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"&gt;

<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<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?

A: 

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

Bob Fincheimer
A: 

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.

MCain
+1  A: 

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.

Marc
Taking out the panel makes no difference. I need it for the default button functionality. Using your suggestion works, but then I can't use it as the panel default button.
harrije
A: 

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 %>");
Philip Smith
Unless you're using ASP.NET 4.0 with ClientIDMode="Static".
Rob
I looked at the html source and the panel is not changing the id but just putting it into a div. I think you're confused with putting the button in the content area for a master page... for that I would have to use btnTest.ClientID
harrije
A: 

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"&gt;
<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
Rob