This article is for beginners who are learning. NET. This will be really helpful for them. Beginners will be wondered how to pass values between User Controls and ASPX page .In this article I have listed some of the ways through which you can access and set the User control values in an ASPX page.
This article is for beginners who are learning. NET.This will be really helpful for them. Beginners will be wondered how to pass values between User Controls and ASPX page. In this article I have listed some of the ways through which you can access and set the User control values in an ASPX page. I started of with some of the basics, advantages and disadvantages of user controls.
User Control:
User Control is the custom, reusable controls. User Controls offers a way to partition and reuse User Interface (UI) functionality across ASP.NET Applications.
Advantages of User Control:
Disadvantages of User Control:
Adding User Controls to an WebForms Page:
At the top of the .aspx page, add the below line above <Html> tag.<%@ Register TagPrefix="Test" TagName="TestControl" Src="Test.ascx" %>
Declare user controls like
<
Accessing and Setting User Controls Values in the .aspx Page:
User can access and set the values of the User Control from .aspx page through properties,using javascript and in code-behind of aspx page.The details of it are shown below
1) Using Properties
If the test.ascx control has two textboxes and submit button.You can access the values of the textboxes in the control from an .aspx page by declaring public property in the .ascx page.
public
TestControl.FirstName
You can set the FirstName of the control from aspx page using
TestControl.FirstName = "Suzzanne"
2) Using Javascript
You can set the values of the controls declared in the .ascx page by
document.forms[0]['TestControl:txtFirstName'].
TestControl objTestControl = (TestControl)Page.FindControl("TestControl");TextBox objTextBox = objTestControl.FindControl("txtFirstName");
Hands on with ASP.Net MVC - Covering MVC 6