TextBox loses value after post back with TextMode

2019-04-10 00:23发布

Got weird problem. I have simple page with TextBox:

<asp:ScriptManager runat="server" />
<asp:UpdatePanel runat="server">
    <ContentTemplate>
        <asp:TextBox ID="amountTextBox" runat="server" TextMode="Number" />
        <asp:Button runat="server" Text="Confirm" OnClick="confirmButton_Click" />
    </ContentTemplate>
</asp:UpdatePanel>

After Button click I try to get TextBox value:

public partial class test : Page {
    protected void confirmButton_Click(object sender, EventArgs e) {
        var answer = amountTextBox.Text;
    }
}

but it's empty. If I remove UpdatePanel, I manage to get the value. If I leave UpdatePanel and remove TextMode property, I manage to get the value. But why can't I have TextBoxt with TextMode set to Number in UpdatePanel?

1条回答
混吃等死
2楼-- · 2019-04-10 00:59

It is a known issue, a fix is available in .NET 4.5 RTM.

Description

HTML5 has new types for the input tag, such as type="number", which is needed to make mobile phones display a numerical keyboard instead of a text keyboard. When doing a full postback of the page it works in all browsers. But if I puts these in an updatepanel on a website to do a partical postback, then none of the new HTML input types are included in the postback response to the server from those browsers that understands these new types (Safari/WebKit and Opera). It works correctly in IE8 and Firefox 4, but probably only because they have not implemented these new types and falls back to understanding it as a type="text" field.

Here is another question with this issue and workarounds: UpdatePanel with input type other than text in html5.

查看更多
登录 后发表回答