Pass (Send) QueryString to UserControl in ASP.Net

Last Reply 7 months ago By dharmendr

Posted 7 months ago

Hello

How to Pass Querystring or parameter to usercontrol from ASP page from code behind dayanamiccaly in c#

UserControl WebUserControl11 = (UserControl)LoadControl("~/UserControl/Seater41.ascx");
WebUserControl11.ID = "uc";
Page.Items.Add("Name", "CodeDigest");
PlaceHolder1.Controls.Add(WebUserControl11);

i want pass multiple parameter to usercontrol.

Posted 7 months ago Modified on 7 months ago

Hi dilipsharm,

The best way to pass data to a UserControl would be to have public properties.

Check this below example.

HTML

WebUserControl.ascx

Name:<asp:Label ID="lblName" runat="server" />

Default.aspx

<%@ Register Src="~/WebUserControl.ascx" TagName="ucName" TagPrefix="uc" %>
<asp:TextBox ID="txtName" runat="server" />
<asp:Button Text="Submit" runat="server" OnClick="Submit" />
<asp:PlaceHolder runat="server" ID="PlaceHolder1" />

Code

WebUserControl.ascx.cs

public string Name
{
    get
    {
        return lblName.Text;
    }
    set
    {
        lblName.Text = value;
    }
}

WebUserControl.ascx.vb

Public Property Name As String
    Get
        Return lblName.Text
    End Get
    Set(ByVal value As String)
        lblName.Text = value
    End Set
End Property

Default.aspx.cs

protected void Submit(object sender, EventArgs e)
{
    WebUserControl ucName = (WebUserControl)LoadControl("~/WebUserControl.ascx");
    ucName.ID = "uc";
    ucName.Name = txtName.Text;
    PlaceHolder1.Controls.Add(ucName);
}

Default.aspx.vb

Protected Sub Submit(ByVal sender As Object, ByVal e As EventArgs)
    Dim ucName As WebUserControl = CType(LoadControl("~/WebUserControl.ascx"), WebUserControl)
    ucName.ID = "uc"
    ucName.Name = txtName.Text
    PlaceHolder1.Controls.Add(ucName)
End Sub

Screenshot