hi, I have an aspx page which contains a button (Load User Controls). on click of this button, my user control has to be loaded. The user control consists of a text box and button. If i enter something in TextBox and on click of button in user control, my
user control button click event has to be fired. I have searched for log time, but i can find answers only for user controls which are added statically. Please somebody help me... below is my code snippet of .aspx.cs and .ascx.cs files
Default.aspx.cs:
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{ }
protected void LoadUserControl(object sender, EventArgs e)//ButtonClient event handler of aspx file
{
UserControl uc1 = Page.LoadControl("~/UserControls_1.ascx") as UserControl;
uc1.ID = "UControl1";
this.PanelUserControl.Controls.Add(uc1);
}
}
UserControls_1.ascx.cs:
public partial class UserControls_1 : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{ }
protected void ButtonClick(object sender, EventArgs e)
{
/*
* here i want to read the text box of User control and display some wish message
*/
}
}
So here I am invoking delegate so on user controls any of the event (in your case it will be button click event) so when invoke ths delegate and then it will executes attached method which is defined in your apsx.cs page.
My Tech Blogs MCPD Enterprise and Web Application
MCTS Web, Window and Enterprise Application
sampath.13
0 Points
2 Posts
Raise event for dynamically added user controls
May 21, 2012 12:05 PM|LINK
hi, I have an aspx page which contains a button (Load User Controls). on click of this button, my user control has to be loaded. The user control consists of a text box and button. If i enter something in TextBox and on click of button in user control, my user control button click event has to be fired. I have searched for log time, but i can find answers only for user controls which are added statically. Please somebody help me... below is my code snippet of .aspx.cs and .ascx.cs files
Default.aspx.cs:
public partial class Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void LoadUserControl(object sender, EventArgs e)//ButtonClient event handler of aspx file { UserControl uc1 = Page.LoadControl("~/UserControls_1.ascx") as UserControl; uc1.ID = "UControl1"; this.PanelUserControl.Controls.Add(uc1); } }UserControls_1.ascx.cs:
public partial class UserControls_1 : System.Web.UI.UserControl { protected void Page_Load(object sender, EventArgs e) { } protected void ButtonClick(object sender, EventArgs e) { /* * here i want to read the text box of User control and display some wish message */ } }Thank 'U'....
Mudasir.Khan
All-Star
15346 Points
3142 Posts
Re: Raise event for dynamically added user controls
May 21, 2012 12:13 PM|LINK
this.TextboxName.Text = "";
is this not working, may we get some more details is the textbox isn't inside usercontrol ?
or whether that textbox.text is not returning the text entered ?
amitpatel.it
Star
7976 Points
1865 Posts
Re: Raise event for dynamically added user controls
May 21, 2012 12:14 PM|LINK
To achive this you need to enter degate in your user control and need to raise event by invoking method from related pages.
Controls
public partial class Paging : System.Web.UI.UserControl { public Paging() { } public Delegate userFunctionPointer; ////your doee protected void LinkButtonPre_Click(object sender, EventArgs e) { userFunctionPointer.DynamicInvoke(pagesize, 1, Session["orderby"].ToString()); } }Below are the code for your aspx page, where loaded this cotrol.
public delegate void functioncall(int pagesize, int pageno, string orderby); private event functioncall formFunctionPointer; protected void Page_Load(object sender, EventArgs e) { formFunctionPointer += new functioncall(FillGrid); PagingBar1.userFunctionPointer = formFunctionPointer; } protected void FillGrid(int PageSize, int CurrentPage, string Orderby) { /// implement logic over here }MCPD Enterprise and Web Application
MCTS Web, Window and Enterprise Application
sampath.13
0 Points
2 Posts
Re: Raise event for dynamically added user controls
May 21, 2012 12:53 PM|LINK
yah, the text box and button are within user control. how to fire event on click of button available in user control.
amitpatel.it
Star
7976 Points
1865 Posts
Re: Raise event for dynamically added user controls
May 21, 2012 01:05 PM|LINK
As in my code I have define one event in user control so consider that event as button click event.
protected void LinkButtonPre_Click(object sender, EventArgs e) { userFunctionPointer.DynamicInvoke(pagesize, 1, Session["orderby"].ToString()); }So here I am invoking delegate so on user controls any of the event (in your case it will be button click event) so when invoke ths delegate and then it will executes attached method which is defined in your apsx.cs page.
MCPD Enterprise and Web Application
MCTS Web, Window and Enterprise Application