Pass (Send) data from aspx page to UserControl using C# and VB.Net

Last Reply 7 months ago By pandeyism

Posted 7 months ago

hi,

How to pass data from aspx page to User control, i have seen lot of examples 

i am not getting answer. please provide sample project VB

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        phprint.Controls.Clear()
        ucPrint = CType(Page.LoadControl("~/UserControls/ucPrint.ascx"), UserControl)
        phprint.Controls.Add(ucPrint)
    End Sub
 
 
    Protected Sub btnExportExcel_Click(sender As Object, e As EventArgs) Handles btnExportExcel.Click
        phprint.Controls.Clear()
        ucPrint = CType(Page.LoadControl("~/UserControls/ucPrint.ascx"), UserControl)
        If ucPrint IsNot Nothing Then
            Dim hdvalu As HiddenField = ucPrint.FindControl("hdformname")
            If hdvalu IsNot Nothing Then
                hdvalu.Value = "asdad"
            End If
        End If
        phprint.Controls.Add(ucPrint)
    End Sub
Posted 7 months ago

Hi rajus,

Check this example. Now please take its reference and correct your code.

HTML

WebUserControl.ascx

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

Default.aspx

<%@ Register Src="~/WebUserControl.ascx" TagName="ucName" TagPrefix="uc" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:TextBox ID="txtName" runat="server" />
        <asp:Button Text="Submit" runat="server" OnClick="Submit" />
        <uc:ucName ID="ucName" runat="server"></uc:ucName>
    </div>
    </form>
</body>
</html>

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)
{
    ucName.Name = txtName.Text;
}

Default.aspx.vb

Protected Sub Submit(ByVal sender As Object, ByVal e As EventArgs)
    ucName.Name = txtName.Text
End Sub

Screenshot