You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

点击按钮后runat server输入框后台取值为空?ASP.NET问题咨询

Hey, I've run into this exact issue before in ASP.NET Web Forms—let's break down the most likely reasons and fixes for you:

1. You're missing a server-side form tag (<form runat="server">)

ASP.NET Web Forms relies entirely on a server-side form to handle postbacks and send user input to the backend. If your <input> and <asp:Button> aren't wrapped inside a <form runat="server">, clicking the Save button won't submit any of the user's input to your code-behind—hence the empty string you're seeing.

Fix it like this:

<form runat="server">
    <input type="text" runat="server" id="Password" /> 
    <asp:Button runat="server" id="SavePassword" Text="Save" OnClick="SavePassword_Click"/>
</form>
2. Your Page_Load is resetting the input value on postback

If you're setting Password.Value directly in Page_Load without checking if it's a postback, that code runs before your SavePassword_Click event. This means any value the user entered gets overwritten by your Page_Load code (even if you set it to an empty string) before you can read it.

Fix it by wrapping your initialization code in an IsPostBack check:

protected void Page_Load(object sender, EventArgs e)
{
    // Only set the default value when the page first loads, not on postback
    if (!IsPostBack)
    {
        Password.Value = "your-default-value-here";
    }
}
3. ViewState is disabled

ASP.NET uses ViewState to persist control values between postbacks. If ViewState is disabled either globally on the page or specifically for your Password input, the user's input won't be saved across the postback.

Check for these:

  • Page directive with EnableViewState="false": <%@ Page ... EnableViewState="false" %>
  • Input control with EnableViewState="false": <input type="text" runat="server" id="Password" EnableViewState="false" />

Fix it:
Remove the EnableViewState="false" attribute (or set it to true—this is the default, so you usually don't need to explicitly set it).

4. Double-check your control ID

Make sure the id of your input (Password) matches exactly with the variable name in your code-behind. C# is case-sensitive, so password vs Password would cause issues here.

Start with checking the first two issues—they're the most common culprits for this problem!

内容的提问来源于stack exchange,提问作者Thomas

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.12 05:05:36