Retrieve the value of a control of the master page
-
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.cs
protected 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.csprotected void Page_Load(object sender, EventArgs e) { TextBox2.Text = Master.ContentTextBoxOfMasterPage; }
MasterPage.masterBut TextBox2 is empty. thank you
-
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.cs
protected 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.csprotected void Page_Load(object sender, EventArgs e) { TextBox2.Text = Master.ContentTextBoxOfMasterPage; }
MasterPage.masterBut TextBox2 is empty. thank you
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
-
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
-
hello; the value of Textbox1 is 2010/2011 is retrieved from the database but the problem is to pass it to TextBox2. Thank
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
-
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.cs
protected 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.csprotected void Page_Load(object sender, EventArgs e) { TextBox2.Text = Master.ContentTextBoxOfMasterPage; }
MasterPage.masterBut TextBox2 is empty. thank you
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
-
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
-
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.cs
protected 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.csprotected void Page_Load(object sender, EventArgs e) { TextBox2.Text = Master.ContentTextBoxOfMasterPage; }
MasterPage.masterBut TextBox2 is empty. thank you
((Label)Master.FindControl("lblTopUpMsg")).Text = "Text in Chield Page";
Here lblTopUpMsg is a Label Name of Master page.