Set UserControl DropDownList selected value in another UserControl TextBox on Button Click in ASP.Net

Last Reply one month ago By dharmendr

Posted one month ago

this logic of assign user control one value to user control two textbox on click event works but when i use master page the same logic not work but without master page it works

pls advice

i want it should work with master page also

user control one

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="WebUserControl1.ascx.cs" Inherits="AccessUserControlToAnother.WebUserControl1" %>
<asp:DropDownList ID="ddlEmployees" runat="server">
    <asp:ListItem>Amol Malhotra</asp:ListItem>
    <asp:ListItem>Shambhu Sharma</asp:ListItem>
    <asp:ListItem>Hemant Chopra</asp:ListItem>
    <asp:ListItem>Vishwa M Goswami</asp:ListItem>
    <asp:ListItem>Mohit Kalra</asp:ListItem>
    <asp:ListItem>Abhishek Nigam</asp:ListItem>
    <asp:ListItem>Yogesh Gupta</asp:ListItem>
    <asp:ListItem>Mayank Dhulekar</asp:ListItem>
    <asp:ListItem>Saurabh Mehrotra</asp:ListItem>
    <asp:ListItem>Rakesh Dixit</asp:ListItem>
</asp:DropDownList>
<asp:LinkButton ID="LinkButton1" runat="server" OnClick="LinkButton1_Click">click me</asp:LinkButton>

 user control two

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="WebUserControl2.ascx.cs" 
    Inherits="AccessUserControlToAnother.WebUserControl2" %>

<table style="background-color: skyblue; width: 100%;">
    <tr>
        <td style="height: 10px;"></td>
    </tr>
    <tr>
        <td>
            <asp:Button ID="btnDDLValue" runat="server" OnClick="btnDDLValue_Click"
                 Text="Get DropDown Selected Value" /></td>
    </tr>
    <tr>
        <td style="height: 10px;"></td>
    </tr>
    <tr>
        <td>Your Selected Employee: 
            <asp:TextBox ID="txtDDLValue" runat="server"></asp:TextBox>
        </td>
    </tr>
</table>

 user control one click event

protected void LinkButton1_Click(object sender, EventArgs e)
{
    WebUserControl2 ctrlB = (WebUserControl2)Page.FindControl("WebUserControl2");
    //TextBox ddl = ctrlB.ControlB_txt;
    //ddl.Text = ddlEmployees.SelectedValue;

    ctrlB.assignvalue(ddlEmployees.SelectedValue);
}

 user control two method

protected void btnDDLValue_Click(object sender, EventArgs e)
{

}    

public void assignvalue(string value)
{
    txtDDLValue.Text = value;
}

 dfault page

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="AccessUserControlToAnother.Default" %>

<%@ Register Src="~/WebUserControl1.ascx" TagPrefix="ctrlA" TagName="WebUserControl1" %>
<%@ Register Src="~/WebUserControl2.ascx" TagPrefix="ctrlA" TagName="WebUserControl2" %>

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <table style="background-color: skyblue; width: 100%;">
                <tr>
                    <td style="height: 10px;"></td>
                </tr>
                <tr style="padding: 10px;">
                    <td>Select Employee : 
                        <ctrlA:WebUserControl1 runat="server" ID="WebUserControl1" />
                        <ctrlA:WebUserControl2 runat="server" ID="WebUserControl2" />
                    </td>
                </tr>
                <tr>
                    <td style="height: 10px;"></td>
                </tr>
                <tr>
                    <td>
                    </td>
                </tr>
            </table>
        </div>
    </form>
</body>
</html>

 

You are viewing reply posted by: dharmendr one month ago.
Posted one month ago

Hi nauna,

Instead of implementing the click event of inside UserControl you have to implement it inside the content page.

Check the example how to call event of UserControl placed in Content Page with Master Page.

Make sure the event has to be public in user control inorder to to access it everywhere.

HTML

UserControl1

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="WebUserControl1.ascx.cs"
    Inherits="WebUserControl1" %>
<asp:DropDownList ID="ddlEmployees" runat="server">
    <asp:ListItem>Amol Malhotra</asp:ListItem>
    <asp:ListItem>Shambhu Sharma</asp:ListItem>
    <asp:ListItem>Hemant Chopra</asp:ListItem>
    <asp:ListItem>Vishwa M Goswami</asp:ListItem>
    <asp:ListItem>Mohit Kalra</asp:ListItem>
    <asp:ListItem>Abhishek Nigam</asp:ListItem>
    <asp:ListItem>Yogesh Gupta</asp:ListItem>
    <asp:ListItem>Mayank Dhulekar</asp:ListItem>
    <asp:ListItem>Saurabh Mehrotra</asp:ListItem>
    <asp:ListItem>Rakesh Dixit</asp:ListItem>
</asp:DropDownList>
<asp:LinkButton ID="LinkButton1" runat="server" OnClick="LinkButton1_Click">click me</asp:LinkButton>

UserControl2

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="WebUserControl2.ascx.cs"
    Inherits="WebUserControl2" %>
<table style="background-color: skyblue; width: 100%;">
    <tr>
        <td style="height: 10px;">
        </td>
    </tr>
    <tr>
        <td><asp:Button ID="btnDDLValue" runat="server" OnClick="btnDDLValue_Click" Text="Get DropDown Selected Value" /></td>
    </tr>
    <tr>
        <td style="height: 10px;"></td>
    </tr>
    <tr>
        <td>
            Your Selected Employee:<asp:TextBox ID="txtDDLValue" runat="server"></asp:TextBox>
        </td>
    </tr>
</table>

Default Page with Master Page

<%@ Register Src="~/WebUserControl1.ascx" TagPrefix="ctrlA" TagName="WebUserControl1" %>
<%@ Register Src="~/WebUserControl2.ascx" TagPrefix="ctrlA" TagName="WebUserControl2" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
    <table style="background-color: skyblue; width: 100%;">
        <tr>
            <td style="height: 10px;"></td>
        </tr>
        <tr style="padding: 10px;">
            <td>
                Select Employee :
                <ctrlA:WebUserControl1 runat="server" id="WebUserControl1" />
                <ctrlA:WebUserControl2 runat="server" ID="WebUserControl2" />
            </td>
        </tr>
        <tr>
            <td style="height: 10px;"></td>
        </tr>
        <tr>
            <td></td>
        </tr>
    </table>
</asp:Content>

Code

UserControl1

public void LinkButton1_Click(object sender, EventArgs e)
{

}

UserControl2

public void btnDDLValue_Click(object sender, EventArgs e)
{

}

public void assignvalue(string value)
{
    txtDDLValue.Text = value;
}

DefaultPage

Registering click event of user control in Page_Load event.

protected void Page_Load(object sender, EventArgs e)
{
    WebUserControl1 ctrlA = (WebUserControl1)Page.Master.FindControl("ContentPlaceHolder1").FindControl("WebUserControl1");
    WebUserControl2 ctrlB = (WebUserControl2)Page.Master.FindControl("ContentPlaceHolder1").FindControl("WebUserControl2");
    (ctrlA.FindControl("LinkButton1") as LinkButton).Click += LinkButton1_Click;
    (ctrlB.FindControl("btnDDLValue") as Button).Click += btnDDLValue_Click;
}

protected void LinkButton1_Click(object sender, EventArgs e)
{
    WebUserControl1 ctrlA = (WebUserControl1)Page.Master.FindControl("ContentPlaceHolder1").FindControl("WebUserControl1");
    WebUserControl2 ctrlB = (WebUserControl2)Page.Master.FindControl("ContentPlaceHolder1").FindControl("WebUserControl2");
    ctrlB.assignvalue((ctrlA.FindControl("ddlEmployees") as DropDownList).SelectedValue);
    ClientScript.RegisterClientScriptBlock(this.GetType(), "", "alert('User control 1 LinkButton clicked')", true);
}

protected void btnDDLValue_Click(object sender, EventArgs e)
{
    ClientScript.RegisterClientScriptBlock(this.GetType(), "", "alert('User control 2 Button clicked')", true);
}

Screenshot

Default Page without Master Page

<%@ Register Src="~/WebUserControl1.ascx" TagPrefix="ctrlA" TagName="WebUserControl1" %>
<%@ Register Src="~/WebUserControl2.ascx" TagPrefix="ctrlA" TagName="WebUserControl2" %>
<!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>
        <table style="background-color: skyblue; width: 100%;">
            <tr>
                <td style="height: 10px;"></td>
            </tr>
            <tr style="padding: 10px;">
                <td>
                    Select Employee :
                    <ctrlA:WebUserControl1 runat="server" ID="WebUserControl1" />
                    <ctrlA:WebUserControl2 runat="server" ID="WebUserControl2" />
                </td>
            </tr>
            <tr>
                <td style="height: 10px;"></td>
            </tr>
            <tr>
                <td></td>
            </tr>
        </table>
    </div>
    </form>
</body>
</html>

Code without Master page

protected void Page_Load(object sender, EventArgs e)
{
    WebUserControl1 ctrlA = (WebUserControl1)Page.FindControl("WebUserControl1");
    WebUserControl2 ctrlB = (WebUserControl2)Page.FindControl("WebUserControl2");
    (ctrlA.FindControl("LinkButton1") as LinkButton).Click += LinkButton1_Click;
    (ctrlB.FindControl("btnDDLValue") as Button).Click += btnDDLValue_Click;
}

protected void LinkButton1_Click(object sender, EventArgs e)
{
    WebUserControl1 ctrlA = (WebUserControl1)Page.FindControl("WebUserControl1");
    WebUserControl2 ctrlB = (WebUserControl2)Page.FindControl("WebUserControl2");
    ctrlB.assignvalue((ctrlA.FindControl("ddlEmployees") as DropDownList).SelectedValue);
    ClientScript.RegisterClientScriptBlock(this.GetType(), "", "alert('User control 1 LinkButton clicked')", true);
}

protected void btnDDLValue_Click(object sender, EventArgs e)
{
    ClientScript.RegisterClientScriptBlock(this.GetType(), "", "alert('User control 2 Button clicked')", true);
}

Screenshot