In this post we will discuss about how we can get user control values in .aspx pages in asp.net. Also you can also check my previous posts on Method Overloading in C#.Net, Read and write from Text file in Asp.Net and WCF interview questions and answers in C#.Net.
Here in this example we will show how a user will select a country from a dropdownlist and the country name will appear in a label in a .aspx page.
To get the use control value in the .aspx page, first we need to add a property in the user control and then we can retrieve the value in the page through the property.
Below is the full usecontrol and page code:
User Control .aspx code:
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="Country.ascx.cs" Inherits="Country" %>
Select Country:
<asp:DropDownList ID="ddlCountry" runat="server" AutoPostBack="True">
<asp:ListItem Text="--Select--" Value=""></asp:ListItem>
<asp:ListItem Text="France" Value="France"></asp:ListItem>
<asp:ListItem Text="Japan" Value="Japan"></asp:ListItem>
<asp:ListItem Text="India" Value="India"></asp:ListItem>
</asp:DropDownList>
User Control .cs code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Country : System.Web.UI.UserControl
{
public string CountryName
{
get { return ddlCountry.SelectedValue; }
set { ddlCountry.SelectedValue = value; }
}
protected void Page_Load(object sender, EventArgs e)
{
}
}
Page .aspx code:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Register src="Country.ascx" tagname="Country" tagprefix="uc1" %>
<!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">
<uc1:Country ID="Country1" runat="server" />
You have selected: <asp:Label ID="lblCountry" runat="server" Text=""></asp:Label>
</form>
</body>
</html>
Here in the page we have registered the user control.
Page .cs code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
lblCountry.Text = Country1.CountryName;
}
}
The output will come will like below.
Here in this example we will show how a user will select a country from a dropdownlist and the country name will appear in a label in a .aspx page.
To get the use control value in the .aspx page, first we need to add a property in the user control and then we can retrieve the value in the page through the property.
Below is the full usecontrol and page code:
User Control .aspx code:
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="Country.ascx.cs" Inherits="Country" %>
Select Country:
<asp:DropDownList ID="ddlCountry" runat="server" AutoPostBack="True">
<asp:ListItem Text="--Select--" Value=""></asp:ListItem>
<asp:ListItem Text="France" Value="France"></asp:ListItem>
<asp:ListItem Text="Japan" Value="Japan"></asp:ListItem>
<asp:ListItem Text="India" Value="India"></asp:ListItem>
</asp:DropDownList>
User Control .cs code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Country : System.Web.UI.UserControl
{
public string CountryName
{
get { return ddlCountry.SelectedValue; }
set { ddlCountry.SelectedValue = value; }
}
protected void Page_Load(object sender, EventArgs e)
{
}
}
Page .aspx code:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Register src="Country.ascx" tagname="Country" tagprefix="uc1" %>
<!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">
<uc1:Country ID="Country1" runat="server" />
You have selected: <asp:Label ID="lblCountry" runat="server" Text=""></asp:Label>
</form>
</body>
</html>
Here in the page we have registered the user control.
Page .cs code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
lblCountry.Text = Country1.CountryName;
}
}
The output will come will like below.