简体   繁体   中英

How to call a function from another .aspx page before the page_load() in asp.net?

I have a aspx page called test.aspx that inherits a master page.The master page has usercontrol file called lcont.ascx which has a treeview whose node is populated with data from database and a contentpalceholder.

The contentplaceholder is where i want to display data from database according to the treenode clicked and so has a Datalist defined within it.Its corresponding .cs file gives the sqlconnection and query to display data in page_load.

So whenever I click the treenode in the test.aspx , the page_load of its corresponding .cs file is called.But what i want is to call .cs file of usercontrol file lcont.ascx so that i can pass the id to query in .cs file of test.aspx.cs.

Is it possible to call function from another page before page_load? Or is it possible to go to .cs file of another file before going through its own .cs file?

Here is the code from lcont.ascx.cs

   protected void my_tv_SelectedNodeChanged(object sender, EventArgs e)
   {
    TreeView tv = sender as TreeView;
    var selectedN = tv.SelectedNode;

    if (selectedN.Parent != null)
    {
        var id = tv.SelectedNode.Value;
        var name = tv.SelectedNode.Text;
        //Session["mySvar"] = id;

        ScriptManager.RegisterStartupScript(this, this.GetType(), "nething", "show("+id+")", true);
    }
    else
    {
        //Response.Redirect("test.aspx");
    }


  }

I want to call this function before the page_load of test.aspx.cs file.

The code of test.aspx.cs file is

 protected void Page_Load(object sender, EventArgs e)
 {
    display();  
 }

public void display()
{

    string str = hdn.Value;
    slbl.Text = "value is" + str;
    //var a = Session["mySvar"];
    var a = 2;
    sq.connection();
    SqlCommand cmd = new SqlCommand("select * from sub_catTbl where sid='" + a + "' ", sq.con);
    SqlDataReader sd = cmd.ExecuteReader();

    mydatalist.DataSource = sd;
    mydatalist.DataBind();
    sq.con.Dispose();
    sq.con.Close();
}

I need to call the function before this page_load call so that i can give sid = "variable value that i obtain after clicking the treenode that is available in lcont.ashx.cs"

Partially answered here , but this answer is actually better: You shouldn't call an event from another page.

Either create a class that handles your TreeWiew events(to access it from another context), or rethink your application.

using VB.net , on Page Initializing

Private Sub _Default_Init(sender As Object, e As EventArgs) Handles Me.Init
    display()
End Sub

Here is the C# Code :

protected void Page_Init(object sender, EventArgs e)
    {
        display();
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM