Monday, September 13, 2010

Pass UserControl Value in ASP.NET

Add User control in project


Then add Textbox in User Control

<%@ Control Language="VB" AutoEventWireup="false" CodeFile="UC1.ascx.vb" Inherits="UC1" %>

<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>



Add New .aspx form

Register the user control

Add the user control and 1 button control in the form

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="UserControlForm.aspx.vb" Inherits="UserControlForm" %>

<%@ Register tagprefix="testuc" Tagname="tname" src="~/UC1.ascx"%>

<!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>Untitled Page</title>

</head>

<body>

<form id="form1" runat="server">

<div>

<testuc:tname runat="server" ID="myuc" />

<asp:Button ID="Button1" runat="server" Text="Get" />

</div>

</form>

</body>

</html>



Add New Class file with code

Imports Microsoft.VisualBasic



Public Class Uccls

Private Shared _txt1 As String



Public Shared Property txt1() As String

Get

Return _txt1

End Get

Set(ByVal Value As String)

_txt1 = Value

End Set

End Property

End Class



Write below code to User Control Page Load



Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

Uccls.txt1 = TextBox1.Text

End Sub







Write below code to .aspx file’s button control

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click

'MsgBox(objmyuc.txt1)

Dim fvalue As String

fvalue = Uccls.txt1

Response.Write("Your Value :-" & fvalue)



End Sub

No comments:

Post a Comment