Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Tuesday, February 12, 2019 7:12 AM
Hi all, I need your help.
I have created in my website in c# the MasterPage Mpdue.
In this MasterPage **Mpdue **I have insert UserName method.
Now I need access call master page method UserName in child page using C# in ASP.Net.
I can't call the method UserName() method in child page.
Can you help me ?
My code below.
MasterPage Mpdue code-behind
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
UserName();
}
}
protected void UserName()
{
sql = @String.Format(" SELECT * FROM ");
sql += String.Format(" tbl_user ");
sql += String.Format(" WHERE username IN (?); ");
using (OdbcConnection myConnectionString =
new OdbcConnection(ConfigurationManager.ConnectionStrings["ConnMySQL"].ConnectionString))
{
using (OdbcCommand command =
new OdbcCommand(sql, myConnectionString))
{
try
{
if (username != null)
{
command.Parameters.AddWithValue("param1", username.ToString().ToUpper());
command.Connection.Open();
using (OdbcDataReader reader = command.ExecuteReader())
{
if (reader.HasRows)
{
while (reader.Read())
{
TES = reader["TES"].ToString();
colorList.Add(TES.ToString());
}
ns = "";
ns = string.Join(",", (from co in colorList select "?").ToList());
}
}
}
}
catch (Exception ex)
{
throw new ApplicationException("operation failed!", ex);
}
finally
{
command.Connection.Close();
}
}
}
}
Child page code-behind
private void sDDL()
{
sDDL.AppendDataBoundItems = true;
sql = @String.Format(" SELECT ");
sql += String.Format(" AREA ");
sql += String.Format(" FROM ");
sql += String.Format(" `tbl_area` ");
sql += String.Format(" WHERE CO IN ({0}) ", ns);
sql += String.Format(" GROUP BY ");
sql += String.Format(" AREA; ");
using (OdbcConnection myConnectionString =
new OdbcConnection(ConfigurationManager.ConnectionStrings["ConnMySQL"].ConnectionString))
{
using (OdbcCommand cmd =
new OdbcCommand(sql, myConnectionString))
{
foreach (var co in colorList)
{
cmd.Parameters.AddWithValue(co, co);
}
cmd.CommandType = CommandType.Text;
cmd.Connection.Open();
using (OdbcDataAdapter sda =
new OdbcDataAdapter(cmd))
{
sda.Fill(dsProducts);
sDDL.DataSource = dsProducts.Tables[0];
sDDL.DataTextField = "AREA ";
sDDL.DataValueField = "AREA ";
sDDL.DataBind();
}
}
}
}
Markup child page :
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="sDefault.aspx.cs" Inherits="sDefault" EnableEventValidation="false" MasterPageFile="Mpdue.master" %>
<%@ MasterType VirtualPath="Mpdue.master" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
...
</asp:Content>
All replies (4)
Wednesday, February 20, 2019 2:21 PM âś…Answered
Solved:
MasterPage Mpdue code-behind
private static string theObjectName;
public static string TheObjectPropertyName
{
get { return this.theObjectName; }
}
protected void UserName()
{
...
...
theObjectName = = reader["TES"].ToString();
}
Child page code-behind
protected void Page_Load(object sender, EventArgs e)
{
Response.Write(Mpdue.TheObjectPropertyName.ToString());
}
**Markup child page **
<%@ Page Title="" Language="C#" MasterPageFile="Mpdue.master" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ MasterType VirtualPath="Mpdue.master" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
</asp:Content>
Tuesday, February 12, 2019 1:14 PM
Looks you want reuse an existing function/sub, I suggest you move your function to another file in app_code then call it whenever you need, on Master Page or any other Page
Wednesday, February 13, 2019 7:14 AM
Hi
According to your description, I suggest that you could add the MasterType directive to your page markup at the top, where the standard <%@ Page %> directive is.
<%@ MasterType TypeName="YourNamespace.YourMasterPageType" %>
Then in your child page code, you could just add:
Master.Username();
For more about how to call method from master page you coulf refer to the following thread,
https://stackoverflow.com/questions/6332889/call-method-in-master-page
https://stackoverflow.com/questions/19265135/access-master-page-method-in-asp-net-c-sharp
https://stackoverflow.com/questions/5068521/how-to-use-a-method-in-a-master-page-from-a-content-page
Best Regards,
Jenifer
Tuesday, February 19, 2019 10:12 AM
Thank you for help, but your suggestion is ineffective, in my case.
In code-behind the UserName method is not available.
I have added the MasterType directive to my page markup at the top :
<%@ MasterType TypeName="Mpdue" %>