how to call Web User Control Dynamically and raising events in it?? [Resolved]

Posted by Modit under ASP.NET on 1/13/2011 | Points: 10 | Views : 9715 | Status : [Member] | Replies : 4
hi , i have created a web user control by the name test.ascx , i source code is
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="test.ascx.cs" Inherits="test" %>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
<br />
<br />
<asp:Label ID="Label1" runat="server"></asp:Label>


the code behind of test.ascx is

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

}
protected void Button1_Click(object sender, EventArgs e)
{
Label1.Text = "Button clicked";
}
}


i am using the control in say del.aspx , which has the following source code

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

<!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>

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

</div>
<asp:Panel ID="Panel1" runat="server">
</asp:Panel>
</form>
</body>
</html>


on the button click event i am calling the web user control dynamically, the code behind of del.aspx is

public partial class del : System.Web.UI.Page
{


protected void Page_Load(object sender, EventArgs e)
{

}
protected void Button1_Click(object sender, EventArgs e)
{
Control c1;
c1 = LoadControl("~/test.ascx");
Panel1.Controls.Add(c1);

}
}


the above code is working fine for calling the control dynamically, but when i try to fire the event of the control , the control just vanishes. I need to keep the control after the button has been pressed and display the output .




Responses

Posted by: Shishirdk on: 1/13/2011 [Member] Starter | Points: 50

Up
0
Down

Resolved
Hi,
As there will be a post back happening when the usercontrol's btn is clicked and the usercontrol enabling code will not get executed thus
hidding the user control.
You can use one of these technique to achieve the goal.

Add a Hiddin Field "btnVisibility" in your del.aspx

<html xmlns="http://www.w3.org/1999/xhtml">

<head id="Head1" runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>

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

</div>
<asp:Panel ID="Panel1" runat="server">
</asp:Panel>
<asp:HiddenField ID="btnVisibility" runat="server"/>
</form>
</body>
</html>


Add the following code in your del.aspx

1.Private method to load the user control.
2.Set the Hidden field on button click and call the private method to enable the same.
3.On subsequent post backs you can check if the hidden field is set or not.If already set then call the
private method to load the user control.

Hope this will resolve your issue.

public partial class _Default : System.Web.UI.Page

{
protected void Page_Load(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(btnVisibility.Value) && btnVisibility.Value.Equals("True"))
{
EnableUserCtrl();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
btnVisibility.Value = "True";
EnableUserCtrl();
}

private void EnableUserCtrl()
{
Control c1;
c1 = LoadControl("~/test.ascx");
Panel1.Controls.Add(c1);
}

}




Modit, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Karthikanbarasan on: 1/13/2011 [Member] [Moderator] [Microsoft_MVP] [MVP] Silver | Points: 25

Up
0
Down
Hi,

try this

in usercontrol1 define event like below

delegate void ButtonClick(Object sender, EventArgs e);


public event ButtonClick OnButtonClick;

if (this.OnButtonClick!= null)

public void BubbleclickButtonClick(object sender, EventArgs e)



{

this.OnButtonClick(sender, e);}



In page code like below

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

this.UserControl1.OnButtonClick+= new UserControl1.ButtonClick (UserControl1_OnButtonClick);
}

void UserControl1_OnButtonClick(object sender, EventArgs e)
{

// write your code to update other control textbox using property

}


Thanks
Karthik
www.f5Debug.net

Modit, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Modit on: 1/13/2011 [Member] Starter | Points: 25

Up
0
Down
thank you very much Shishirdk, it solved my problem.

Modit, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Lxnitu on: 1/13/2012 [Member] Starter | Points: 25

Up
0
Down
I love you man. I really do. You helped me very much. THANK YOU. A dozen of beers from me

Modit, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response