Subscribe to Subscribers

Forums » .NET » .NET »

Passing values from one user control


Posted Date: 16 Aug 2012      Posted By:: Jai     Member Level: Gold    Member Rank: 139     Points: 4   Responses: 3



Hi,

I have a main form. i have a split container and i have two user controls in each split container.

User control 1 -> I have a grid view which takes data from db and i have done.

User Control 2 -> I have some text boxes which should be populated based on selecting a row from user control 1


My question is how can i pass the data or access another usercontrol 2 controls from user control 1


Thanks
Jai




Responses

#684559    Author: Mahesh Durgam      Member Level: Gold      Member Rank: 137     Date: 17/Aug/2012   Rating: 2 out of 52 out of 5     Points: 4

Hi,

Try to use the below code snippet, hope it might help you




UserControl 1
---------------
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="UserControl1.ascx.cs" Inherits="UserControl1" %>
First name:

<asp:Label ID="lblFirstName" runat="server" Text="First name"></asp:Label><br />
Last Name:
<asp:Label ID="lblLastName" runat="server" Text="Last name"></asp:Label>

Code-behind:
--------------

public partial class UserControl1 : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{ }

public string LastName
{
set { lblLastName.Text = value; }
get { return value; }
}

public string FirstName
{
set { lblFirstName.Text = value; }
get { return value; }
}
}


UserControl 2
---------------
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="UserControl2.ascx.cs" Inherits="UserControl2" %>
First name:

<asp:Label ID="lblFirstName" runat="server" Text="First name"></asp:Label><br />
Last Name:
<asp:Label ID="lblLastName" runat="server" Text="Last name"></asp:Label>

Code-behind:
--------------

public partial class UserControl2 : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{ }

public string LastName
{
set { lblLastName.Text = value; }
get { return value; }
}

public string FirstName
{
set { lblFirstName.Text = value; }
get { return value; }
}
}


In the .aspx page, add the user-controls
---------------------------------------------

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="TestUserControls.aspx.cs" Inherits="TestUserControls" %>

<%@ Register src="UserControl1.ascx" tagname="UserControl1" tagprefix="uc1" %>
<%@ Register src="UserControl2.ascx" tagname="UserControl2" tagprefix="uc2" %>

<html>
<head runat="server">
</head>
<body>
<form id="form1" runat="server">
<div>
<uc1:UserControl1 ID="UserControl1" runat="server" />
</div>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
<uc2:UserControl2 ID="UserControl2" runat="server" />
</form>
</body>
</html>

In code-behind,
-----------------

public partial class TestUserControls : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void Button1_Click(object sender, EventArgs e)
{
Response.Write("First Name: " + UserControl1.FirstName);
Response.Write("<br/>Last Name: " + UserControl2.LastName);

UserControl2.FirstName = UserControl1.FirstName;
UserControl2.LastName = UserControl1.LastName;
}
}





Thanks,
Mahesh


 
#684566    Author: Jai      Member Level: Gold      Member Rank: 139     Date: 17/Aug/2012   Rating: 2 out of 52 out of 5     Points: 1

i am working on windows application not web

 
#684583    Author: Ashis kumar Nayak      Member Level: Gold      Member Rank: 0     Date: 17/Aug/2012   Rating: 2 out of 52 out of 5     Points: 4

As you said u want to access the testbox in usercontrol2 in usercontrol1, for that first set the textBox's "Modifier" property to "Public" in the usercontrol2.

Create a property of type "UserControl2" in usercontrol1. Let say 'Uc2"

Set the property value "Uc2= instance of the userControl2 created in the form load.

here is the code Sample

private void Form1_Load(object sender, EventArgs e)
{
UserControl1 uc1 = new UserControl1();
UserControl2 uc2 = new UserControl2();
uc1.Uc2 = uc2;
panel1.Controls.Add(uc1);
panel2.Controls.Add(uc2);
}

Code in usercontrol

namespace TestWinApp
{
public partial class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent();
}


private UserControl2 uc2;
public UserControl2 Uc2
{
get { return uc2; }
set { uc2 = value; }
}

private void UserControl1_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("Fname"));
dt.Columns.Add(new DataColumn("Lname"));
DataRow dr = null;
for (int i = 0; i <= 4; i++)
{
dr = dt.NewRow();
dr["FName"] = "Mc."+i.ToString();
dr["Lname"] = "Duck"+i.ToString();
dt.Rows.Add(dr);
}

dataGridView1.DataSource = dt;
}


private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
uc2.textBox1.Text = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString(); ;
}

}
}





 
Post Reply
You must Sign In to post a response.

Next : Save multiple data in variable and retrive data from varibale in windows application
Previous : How to get data within a specic dates from excel to dagagrid view
Return to Discussion Forum
Post New Message
Category:

Related Messages

Active Members
TodayLast 7 Daysmore...

Awards & Gifts
Talk to Webmaster Tony John
Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India
2005 - 2013 All Rights Reserved.
.NET and other trademarks mentioned in this site belong to Microsoft and other respective trademark owners.
Articles, tutorials and all other content offered here is for educational purpose only.
We are not associated with Microsoft or its partners.