Avatar of johnkainn
johnkainn
 asked on

webusercontrol interact with aspx

I have 2 question regarding webusercontrol

1) When a button in a web user control is clicked I want to add a text to a label in the aspx page that contains the webusercontrol. How do I do that?

2) When a button in the aspx page is clicked I want to add text to a label in the webusercontrol. How do I do that?
.NET ProgrammingASP.NET

Avatar of undefined
Last Comment
daveamour

8/22/2022 - Mon
daveamour

May I ask which version of Visual Studio you are using?
daveamour

Plese see my attached code for the main page and the user control.
Setting the user control lable is easy.
To set the lable in the main page we expose the lable text property via a public property.
You have to case th Page to an instance of the page in question to make this work.

Page
 
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="UserControlMainPage.aspx.cs" Inherits="EEWebModelApp.UserControlMainPage" %>
 
<%@ Register src="MyUserControl.ascx" tagname="MyUserControl" 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">
    <div>
    
        <asp:Label ID="MyLabel" runat="server"></asp:Label>
        
        <uc1:MyUserControl ID="MyUserControl1" runat="server" />
    
    </div>
    </form>
</body>
</html>
 
 
Page Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
 
namespace EEWebModelApp
{
    public partial class UserControlMainPage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
 
        }
 
        public string LabelText
        {
            set
            {
                MyLabel.Text = value;
            }
        }
    }
}
 
 
Control
 
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="MyUserControl.ascx.cs" Inherits="EEWebModelApp.MyUserControl" %>
<asp:Label ID="MyLabel" runat="server"></asp:Label><br /><br />
<asp:Button ID="MyButton" runat="server" Text="Click Me" 
    onclick="MyButton_Click" />
 
Control Code
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
 
namespace EEWebModelApp
{
    public partial class MyUserControl : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
 
        }
 
        protected void MyButton_Click(object sender, EventArgs e)
        {
            MyLabel.Text = "<p>This is some text in the user control</p>";
 
            ((UserControlMainPage)Page).LabelText = "<p>This is some text in the page</p>";
        }
    }
}

Open in new window

ASKER CERTIFIED SOLUTION
daveamour

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck