Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Retrieve the value of a control of the master page

Retrieve the value of a control of the master page

Scheduled Pinned Locked Moved ASP.NET
csharpsysadmin
7 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    amina89
    wrote on last edited by
    #1

    Hello; In the master page of my site I have a textbox that displays the school year, to recover the value of textbox in other pages I used: page.master.csprotected void Page_Load(object sender, EventArgs e) { Label13.Text = Convert.ToString(DateTime.Now); // SqlConnection maConnexionSQLsrv = new SqlConnection(@"Data Source=.\SQLEXPRESS;UID=sa;Password=forChariaa3*;Initial Catalog=scolarite"); SqlConnection myConnection = new SqlConnection(ConnectionString); myConnection.Open(); String maRequete = "select ann_uni as [p1]from annee where @param between date_debut and date_fin"; SqlCommand myCommand = new SqlCommand(maRequete, myConnection); myCommand.Parameters.Add(new SqlParameter("@param", SqlDbType.DateTime)); myCommand.Parameters["@param"].Value = Label13.Text; SqlDataReader drr2 = myCommand.ExecuteReader(); while (drr2.Read()) { Textbox1.Text = drr2["p1"].ToString(); } drr2.Close(); } private string ConnectionString { get { string connectionString = (@"Data Source=.\SQLEXPRESS;UID=sa;Password=*****;Initial Catalog=bd1"); // string connectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; return connectionString; } } public string ContentTextBoxOfMasterPage { get { return Textbox1.Text; } } defaul2.aspx <%@ Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" Title="Page sans titre" %> <%@ MasterType virtualPath="~/MasterPage.master"%> <asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server"> </asp:Content> <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server"> <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox></asp:Content> Default2.aspx.cs protected void Page_Load(object sender, EventArgs e) { TextBox2.Text = Master.ContentTextBoxOfMasterPage; } MasterPage.master But TextBox2 is empty. thank you

    N G P 3 Replies Last reply
    0
    • A amina89

      Hello; In the master page of my site I have a textbox that displays the school year, to recover the value of textbox in other pages I used: page.master.csprotected void Page_Load(object sender, EventArgs e) { Label13.Text = Convert.ToString(DateTime.Now); // SqlConnection maConnexionSQLsrv = new SqlConnection(@"Data Source=.\SQLEXPRESS;UID=sa;Password=forChariaa3*;Initial Catalog=scolarite"); SqlConnection myConnection = new SqlConnection(ConnectionString); myConnection.Open(); String maRequete = "select ann_uni as [p1]from annee where @param between date_debut and date_fin"; SqlCommand myCommand = new SqlCommand(maRequete, myConnection); myCommand.Parameters.Add(new SqlParameter("@param", SqlDbType.DateTime)); myCommand.Parameters["@param"].Value = Label13.Text; SqlDataReader drr2 = myCommand.ExecuteReader(); while (drr2.Read()) { Textbox1.Text = drr2["p1"].ToString(); } drr2.Close(); } private string ConnectionString { get { string connectionString = (@"Data Source=.\SQLEXPRESS;UID=sa;Password=*****;Initial Catalog=bd1"); // string connectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; return connectionString; } } public string ContentTextBoxOfMasterPage { get { return Textbox1.Text; } } defaul2.aspx <%@ Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" Title="Page sans titre" %> <%@ MasterType virtualPath="~/MasterPage.master"%> <asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server"> </asp:Content> <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server"> <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox></asp:Content> Default2.aspx.cs protected void Page_Load(object sender, EventArgs e) { TextBox2.Text = Master.ContentTextBoxOfMasterPage; } MasterPage.master But TextBox2 is empty. thank you

      N Offline
      N Offline
      Not Active
      wrote on last edited by
      #2

      And have you debugged and stepped through to ensure the code is working? Are you getting a value from the database? A few tips: DON'T hard code your connection string in your code, that is what the configuration file is for. See the element called connectionStrings? Guess what that is for. If you are returning a single value than use ExecuteScalar


      I know the language. I've read a book. - _Madmatt

      A 1 Reply Last reply
      0
      • N Not Active

        And have you debugged and stepped through to ensure the code is working? Are you getting a value from the database? A few tips: DON'T hard code your connection string in your code, that is what the configuration file is for. See the element called connectionStrings? Guess what that is for. If you are returning a single value than use ExecuteScalar


        I know the language. I've read a book. - _Madmatt

        A Offline
        A Offline
        amina89
        wrote on last edited by
        #3

        hello; the value of Textbox1 is 2010/2011 is retrieved from the database but the problem is to pass it to TextBox2. Thank

        N 1 Reply Last reply
        0
        • A amina89

          hello; the value of Textbox1 is 2010/2011 is retrieved from the database but the problem is to pass it to TextBox2. Thank

          N Offline
          N Offline
          Not Active
          wrote on last edited by
          #4

          You have got to give more information in your posts. We are not sitting next to you looking over your shoulder. What do you mean "the problem is to pass it to TextBox2"? Is the datareader valid? Do you have a value to assign? Is the problem extracting the value from the textbox rather than assigning it?


          I know the language. I've read a book. - _Madmatt

          1 Reply Last reply
          0
          • A amina89

            Hello; In the master page of my site I have a textbox that displays the school year, to recover the value of textbox in other pages I used: page.master.csprotected void Page_Load(object sender, EventArgs e) { Label13.Text = Convert.ToString(DateTime.Now); // SqlConnection maConnexionSQLsrv = new SqlConnection(@"Data Source=.\SQLEXPRESS;UID=sa;Password=forChariaa3*;Initial Catalog=scolarite"); SqlConnection myConnection = new SqlConnection(ConnectionString); myConnection.Open(); String maRequete = "select ann_uni as [p1]from annee where @param between date_debut and date_fin"; SqlCommand myCommand = new SqlCommand(maRequete, myConnection); myCommand.Parameters.Add(new SqlParameter("@param", SqlDbType.DateTime)); myCommand.Parameters["@param"].Value = Label13.Text; SqlDataReader drr2 = myCommand.ExecuteReader(); while (drr2.Read()) { Textbox1.Text = drr2["p1"].ToString(); } drr2.Close(); } private string ConnectionString { get { string connectionString = (@"Data Source=.\SQLEXPRESS;UID=sa;Password=*****;Initial Catalog=bd1"); // string connectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; return connectionString; } } public string ContentTextBoxOfMasterPage { get { return Textbox1.Text; } } defaul2.aspx <%@ Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" Title="Page sans titre" %> <%@ MasterType virtualPath="~/MasterPage.master"%> <asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server"> </asp:Content> <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server"> <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox></asp:Content> Default2.aspx.cs protected void Page_Load(object sender, EventArgs e) { TextBox2.Text = Master.ContentTextBoxOfMasterPage; } MasterPage.master But TextBox2 is empty. thank you

            G Offline
            G Offline
            gr33d
            wrote on last edited by
            #5

            Can you try, in Default2.aspx.cs : Page_Load

            Control masterControl = Master.FindControl("TextBox1");

            if(masterControl != null)
            {
            string textToCopy = (TextBox)masterControl.Text;
            TextBox2.Text = textToCopy;
            }

            Debug, and let us know if 1) masterControl is not null and 2) if textToCopy gets populated

            A 1 Reply Last reply
            0
            • G gr33d

              Can you try, in Default2.aspx.cs : Page_Load

              Control masterControl = Master.FindControl("TextBox1");

              if(masterControl != null)
              {
              string textToCopy = (TextBox)masterControl.Text;
              TextBox2.Text = textToCopy;
              }

              Debug, and let us know if 1) masterControl is not null and 2) if textToCopy gets populated

              A Offline
              A Offline
              amina89
              wrote on last edited by
              #6

              Hi; I try:

              Control masterControl = Master.FindControl("TextBox1");

              if(masterControl != null)
              {
              string textToCopy = (TextBox)masterControl.Text;
              TextBox2.Text = textToCopy;
              } else{TextBox2.Text="rr";
              }

              result:TextBox2 is empty :(

              1 Reply Last reply
              0
              • A amina89

                Hello; In the master page of my site I have a textbox that displays the school year, to recover the value of textbox in other pages I used: page.master.csprotected void Page_Load(object sender, EventArgs e) { Label13.Text = Convert.ToString(DateTime.Now); // SqlConnection maConnexionSQLsrv = new SqlConnection(@"Data Source=.\SQLEXPRESS;UID=sa;Password=forChariaa3*;Initial Catalog=scolarite"); SqlConnection myConnection = new SqlConnection(ConnectionString); myConnection.Open(); String maRequete = "select ann_uni as [p1]from annee where @param between date_debut and date_fin"; SqlCommand myCommand = new SqlCommand(maRequete, myConnection); myCommand.Parameters.Add(new SqlParameter("@param", SqlDbType.DateTime)); myCommand.Parameters["@param"].Value = Label13.Text; SqlDataReader drr2 = myCommand.ExecuteReader(); while (drr2.Read()) { Textbox1.Text = drr2["p1"].ToString(); } drr2.Close(); } private string ConnectionString { get { string connectionString = (@"Data Source=.\SQLEXPRESS;UID=sa;Password=*****;Initial Catalog=bd1"); // string connectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; return connectionString; } } public string ContentTextBoxOfMasterPage { get { return Textbox1.Text; } } defaul2.aspx <%@ Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" Title="Page sans titre" %> <%@ MasterType virtualPath="~/MasterPage.master"%> <asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server"> </asp:Content> <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server"> <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox></asp:Content> Default2.aspx.cs protected void Page_Load(object sender, EventArgs e) { TextBox2.Text = Master.ContentTextBoxOfMasterPage; } MasterPage.master But TextBox2 is empty. thank you

                P Offline
                P Offline
                Prasanta_Prince
                wrote on last edited by
                #7

                ((Label)Master.FindControl("lblTopUpMsg")).Text = "Text in Chield Page";

                Here lblTopUpMsg is a Label Name of Master page.

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups