I have a master page with a treeview and I want to pass the onlick node value to a select statement that exists in the content page(in an sqldatasource control).
How can I do that?

Dani AI

Generated

A reliable pattern is to have the master page raise a typed event when a TreeView node is clicked and let the content page handle that event and set its SqlDataSource parameter. This keeps pages decoupled, avoids brittle FindControl code paths, and avoids using application-wide static state for per-user data.

Good points already posted: demonstrates accessing controls by walking the control tree (works but is fragile if the placeholders or IDs change). ’s public-method idea keeps things explicit but couples the master to the concrete page class. ’s static-field suggestion should be avoided for per-user values because static fields are shared across requests and users and can lead to cross-user leakage and hard-to-find bugs.

Example (master raises an event; content page subscribes). Add this directive at the top of the content page:

<%@ MasterType VirtualPath="~/Site.master" %>

Master page (C#):

public class NodeEventArgs : EventArgs
{
    public string NodeValue { get; }
    public NodeEventArgs(string value) { NodeValue = value; }
}

public partial class SiteMaster : MasterPage
{
    public event EventHandler<NodeEventArgs> NodeClicked;

    protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
    {
        string val = TreeView1.SelectedValue;
        NodeClicked?.Invoke(this, new NodeEventArgs(val));
    }
}

Content page (subscribe early and update the SqlDataSource):

protected override void OnInit(EventArgs e)
{
    base.OnInit(e);
    Master.NodeClicked += Master_NodeClicked;
}

private void Master_NodeClicked(object sender, NodeEventArgs e)
{
    SqlDataSource1.SelectParameters["MyParam"].DefaultValue = e.NodeValue;
    GridView1.DataBind(); // or call SqlDataSource1.Select(...) as needed
}

Troubleshooting notes: subscribe in OnInit so the handler runs during the ASP.NET event stage; make sure the parameter is set before any automatic Select occurs (if needed, handle SqlDataSource.Selecting to inject the value or call DataBind manually). Always validate node values before using them in queries and prefer parameterized SqlDataSource parameters to prevent injection.

Recommended Answers

All 3 Replies

Hi there

Try something like this

Dim MyTxt As TextBox
        MyTxt = CType(Page.Master.FindControl("ContentPlaceHolder1").FindControl("TextBox2"), TextBox)
        CType(Page.Master.FindControl("ContentPlaceHolder1").FindControl("TextBox1"), TextBox).Text = MyTxt.Text
        CType(Page.Master.FindControl("ScriptManager1"), ScriptManager).SetFocus(MyTxt.ClientID)
        ' ''Me.ScriptManager1.SetFocus(MyTxt.ClientID)

hope this helps you

A simpler way to do that would be to create a public method in the page and pass the data as a parameter.

In the master page:

void someClickEvent()
{
      if (Page is pgUserInformation)
      {
        ((pgUserInformation)Page).PassParameter("abc");
      }
}

Replace pgUserInformation with the name of the class from the code behind. This is an example of a page called "securitycode.aspx"

public partial class securitycode : System.Web.UI.Page

A more simple and safe way is to create a public static object in the master page cs and just before you open the window update its value, after that in the window you call A property to read the object.

somthing like this:

master page .cs

private static string theObjectName;

public static string TheObjectPropertyName
{
      get { return this.theObjectName; }
}

void methodOpenWindow()
{
     this.theObjectName = "somthing";
     //open the window
}

new page .cs

protected void Page_Load(object sender, EventArgs e)
{
    MasterPageName.TheObjectPropertyName;
    //and do what ever you want with it.
}

enjoy

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.