0

Can i call a function from my master page to my default page? I need to reload the content on my default page when a button is clicked on my master page. Im using session variables. but the page loads before the variables are set. Its causing an issue. So i want to call a function on my default page from my master page.

default.aspx.cs

private void SendSessionVariables(object p, object p_2)
{
    //call this function from my master page
}

Masterpage.aspx.cs

protected void LinkButton1_Click1(object sender, EventArgs e)
{
    Session["SexType"] = "M";
    //session is set now i need to call the function on my default page
}
ianace
  • 1,646
  • 2
  • 17
  • 31
CsharpBeginner
  • 1,753
  • 8
  • 38
  • 68

4 Answers4

2

Check this answer to a similar question by Robert Paulson. He begins:

A more concrete example of what you're trying to do would be useful. Otherwise you'll get all sorts of answers, many of which will be off the mark.

You should put common code in the App_Code folder. You should also not have any business logic inside a forms code-behind.

The fact that you need one page to call a method in another page indicates that you haven't done this. Pages are for displaying and interpreting actions, but they should not hold any of the business logic.

Community
  • 1
  • 1
  • I've edited this to make your citation proper. When citing another answer, you need to link to the answer, as well as the user's profile page and clearly separate their work from your own. However, you really need to add something to this answer, as the citation is the total body of this answer. I'm not deleting it (yet), but will if flags continue to accrue. – Tim Post Jan 04 '12 at 09:24
  • Thank you @TimPost, But why it would be deleted?? – MostafaElHawary Jan 04 '12 at 12:59
  • 1
    While this does technically answer the question, It's not exactly _your_ answer unless you add something to it. The community tends to be extremely sensitive when it comes to copying answers from other questions. I edited it to be a proper citation so you'd have time to expand on it a bit. Generally, simply pointing to another answer is something you should do in a comment, unless you can add something to it. I don't think the questions are identical enough to close with my single vote, but 80% of the time if an answer to another question answers the current one, it probably needs flagged. – Tim Post Jan 04 '12 at 13:08
  • Ping me when you've added more to this, and I'll remove these comments. – Tim Post Jan 04 '12 at 13:09
0
protected void LinkButton1_Click1(object sender, EventArgs e)
{
    Session["SexType"] = "M";
    //session is set now i need to call the function on my default page
    if (Page is _default)
        ((_default)Page).SendSessionVariables(...)
}

You'll need to define that method as public

Claudio Redi
  • 67,454
  • 15
  • 130
  • 155
0

You can try to use the Reflection

Type t = this.ContentPlaceHolder1.Page.GetType();
MethodInfo mi = t.GetMethod("SendSessionVariables" , BindingFlags.Instance | BindingFlags.NonPublic);  // Add BindingFlags.Instance | BindingFlags.NonPublic to access private method.
object[] os = new object[2]; 
os[0] = "";
os[1] = "";
mi.Invoke(this.ContentPlaceHolder1.Page, os); 
shenhengbin
  • 4,236
  • 1
  • 24
  • 33
0

Set your SendSessionVariables in public

fiberOptics
  • 6,955
  • 25
  • 70
  • 105