I have been often asked by people that how to pass query string to user control in ASP.NET.
I decided to solve this problem in this blog post.
In this post I am using example of a Menu Control (used as User Control) which needs to be binded dynamically depending on query string provided to the main page containing user control.
This can be done easily by using public property in the user control.
Following 3 easy steps will help in accomplishing this task!
In your user controls create this property (you can change this according to your situation)
private string menuId = string.Empty;
public string MenuID
menuId = value;
Private menuId As String = String.Empty
Public WriteOnly Property ChangeMenuID() As String
Set(value As String)
menuId = value
Set Menu ID property of your user control from parent page like:
YourUserControlID.MenuID = Request.QueryString["menuid"].ToString();// or from any other variable
YourUserControlID.MenuID = Request.QueryString("menuid").ToString() ' or from any other variable
Write this function in your user control which will bing the Menu from database depending on query string provided to the parent page having user control.
That's That! ;)
// This function is to be written in the user control
public void BindMenuData()
SqlCommand sqlCmd = new SqlCommand();
sqlCmd.CommandText = "select * from [YourTableName] Where MenuID = @MenuID";
sqlCmd.CommandType = CommandType.Text;
SqlConnection sqlCOn = new SqlConnection("Connection String.. bla bla");
SqlDataAdapter adapter = new SqlDataAdapter(sqlCmd);
DataSet ds = new DataSet();
Menu1.DataSource = ds;
'This function is to be written in the user control
Public Sub BindMenuData()
Dim sqlCmd As New SqlCommand()
sqlCmd.CommandText = "select * from [YourTableName] Where MenuID = @MenuID"
sqlCmd.CommandType = CommandType.Text
Dim sqlCOn As New SqlConnection("Connection String.. bla bla")
Dim adapter As New SqlDataAdapter(sqlCmd)
Dim ds As New DataSet()
Menu1.DataSource = ds
That's That! ;)
Still have confusion ??
Feel free to ask!