Search code examples
c#asp.netwebformspostbackmaster-pages

Text property of a TextBox is empty when an async PostBack occurs


I've had a look at lots of posts and I feel like I'm going crazy as nothing I've tried seems to work. I simply want to click a button and retrieve the value of a textbox.

I'm using web forms with a site.master so not sure if this could be affecting the problem, but most of the solutions I've seen don't appear to be using a site.master.

I'm not binding the textbox, initially I just wanted to create a contact form.

<%@ Page Title="About" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="About.aspx.cs" Inherits="Blackburn_Pulse.About" %>

<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
    <script>$("#menuAbout").addClass("navi-active");</script>

    <div class="contentBody">
        <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
            <ContentTemplate>
                <asp:TextBox ID="tb_Test" EnableViewState="true" CausesValidation="false" runat="server"></asp:TextBox>
                <asp:LinkButton ID="btn_Test" runat="server" OnClick="btn_Test_Click" Text="send test" />
            </ContentTemplate>
            <Triggers>
                <asp:AsyncPostBackTrigger ControlID="btn_Test" EventName="click" />
            </Triggers>
        </asp:UpdatePanel>
    </div>
</asp:Content>
public partial class About : Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        this.Page.Master.EnableViewState = true;

        if (IsPostBack)
        {
            var test_var = tb_Test.Text; //returning empty
        }
    }

    protected void btn_Test_Click(object sender, EventArgs e)
    {
        var test_var = tb_Test.Text; //returning empty
    }
}

UPDATE: site.master.cs

    public partial class SiteMaster : MasterPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            BindCategories();
        }
    }

    protected void BindCategories()
    {
        Categories Categories = new Categories();
        rpt_categories.DataSource = Categories.Get_Categories();
        rpt_categories.DataBind();
    }

    protected void lb_newsCat_Command1(object sender, CommandEventArgs e)
    {
        switch (e.CommandName)
        {
            case "naviTo":
                //Redirect user to selected category
                Response.Redirect("~/" + e.CommandArgument.ToString());
                break;
        }
    }
}

Solution

  • I'm converting a PHP website to an ASP.net website. Turns out I had another HTML form tag on a search box that I haven't started working on yet.

    Unfortunately the debugger doesn't throw an error if you have another form tag so anybody else who's just spent hours of their lives searching, double check you don't have more than 1 form tag!

    In my site.master.aspx file, I had a search box as follows:

    <form method="post" action="?">
        <input class="form-control mr-sm-2" type="search" placeholder="Search" aria-label="Search">
        <button class="btn btn-outline-primary my-2 my-sm-0" type="submit">Search</button>
    </form>
    

    Once I took the extra form tag out, everything worked fine.