Search code examples
c#asp.netwebformstextboxmaster-pages

Getting the Textbox Value from A MasterPage Master


I have a MasterPage Master and I want to set a Textbox value on this page instead of individually on the child pages. I want to do it this way so that it functions across the site for a newsletter signup. Currently, when I postback to the server, the textbox comes back with an empty string. How can I capture the value of the textbox in this scenerio?

Here's a code sample:

<%@ Master Language="C#" AutoEventWireup="true" CodeFile="MasterPageLO.master.cs" Inherits="masterpages_MasterPageLO" %>

<asp:Textbox runat="server" EnableViewState= "true" id="email" name="email" CSSclass="form-control required" placeholder="Enter your Email"></asp:textbox>

<asp:Linkbutton runat="server" ID="btnemail" name="btnemail" CSSclass="btn btn-success"  OnClick="btnemail_Click">Subscribe</asp:Linkbutton>

and email.TextBox after post is equal to "" but not null which is strange.


Solution

  • I ended up changing the post logic and moving most of it out of the Master MasterPage and implemented a solution utilizing the child Page(s)/Master combination and also utilizing JQUERY/AJAX to get the value of the textbox and then inserting it into the Database because the Master textbox kept losing its value after redirecting to the Default.aspx page when using the codebehind for that file. I also borrowed some of the logic from: https://www.aspsnippets.com/Articles/Save-Form-Data-to-Database-with-jQuery-AJAX-in-ASPNet.aspx to suit my needs and now everything works the way I want it to.