How to handle or check User Control postback in Parent page? - asp.net

I have a checkbox in my user control. How do I disable my parent control's event when user control's checkbox's CheckedChanged event was triggered?

You cannot disable events from it ASP.Net Life Cycle.
However, you can check inside each Event whether the postback is triggered by Parent or User Control.
If you want to check which control fire event inside Parent page -
public partial class Default : Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
string id = Request.Form["__EVENTTARGET"];
if (!string.IsNullOrWhiteSpace(id) && id.Contains("WebUserControl11"))
{
}
}
}
}
If you want to check whether this event is fired by one of my control inside UserControl -
public partial class WebUserControl1 : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack) // *** This IsPostBack is not same as Parent's IsPostBack ***
{
}
}
protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
{
if (IsPostBack)
{
}
}
}

Related

Dynamic Control creation, event handling and control properties in asp.net

I am trying to solve the following issue:
I have a user control that needs to be dynamically loaded inside another control. This dynamically loaded control raises an event and as per my knowledge the events raised by dynamically loaded control will only be handled correctly if the control is created and loaded during the onload event. There is one more constraint that i have to consider when loading the control dynamically and that is a property in parent control. This property will determine if the control should be loaded or not.
Pseudo Code:
ControlA
Property ShowControl
ControlA has a CheckBox(chkShowControlIfSelected)
OnLoadEvent()
If chkShowControlIfSelected.checked checked and ShowControlProperty is set
{
reate ControlB Dynamically
ControlB.Event += EventHandler()
Add ControlB to ControlCollection
}
The problem i am running into is that if I include the code to load the controlB in prerender event then the property ShowControl is set correctly but the EventHandler() is not called. If I put the code to load the controlB dynamically in pageLoad event then property ShowControl is not yet set but in that case the eventHandler Code is called correctly.
Am i missing something or handling the code in incorrect event handlers?
Following is the working example:
ControlA:
public partial class ControlA : System.Web.UI.UserControl
{
public bool ShowControl
{
get
{
if (this.ViewState["ShowControl"] == null)
return false;
else
return (bool)this.ViewState["ShowControl"];
}
set
{
this.ViewState["ShowControl"] = value;
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (this.ShowControl)
{
var controlB = (ControlB)this.LoadControl("ControlB.ascx");
controlB.FileUploadingComplete += controlB_FileUploadingComplete;
this.pnl1.Controls.Add(controlB);
}
}
void controlB_FileUploadingComplete(object sender, EventArgs e)
{
//throw new NotImplementedException();
Trace.Write("file upload completed");
}
}
ControlB:
public partial class ControlB : System.Web.UI.UserControl
{
public event EventHandler FileUploadingComplete;
protected void OnFileUploadingComplete()
{
if (this.FileUploadingComplete != null)
this.FileUploadingComplete(this, EventArgs.Empty);
}
protected void btn1_Click(object sender, EventArgs e)
{
this.OnFileUploadingComplete();
}
}
Page (has ControlA present):
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
this.ControlA1.ShowControl = true;
}
}

Refresh ascx from button click

How to refresh another user control declared in a ascx page from button click event of that page(ascx.cs)?
You can load it dynamically.
First from page load event
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostback)
{
LoadControl();
}
}
And then load it from a button again on which you want to reload it
protected void Button_Click(object sender, EventArgs e)
{
LoadControl();
}
private void LoadControl()
{
usercontrols_SimpleControl ucSimpleControl = LoadControl("~/UserControlName.ascx") as usercontrols_SimpleControl;
Placeholder1.Controls.Add(ucSimpleControl);
}

How can this codebehind be written better?

I have a form which has a single textbox that sends some data to the database upon hitting enter. Data is displayed below the textbox in a repeater control. Input data is displayed on the form immediately by binding the data to the repeater in the TextChanged event of that textbox.
In the CodeBehind, I am calling BindRepeater method twice, once on every new page load and once on the TextChanged event of the textbox.
How can this be rewritten to call the BindRepeater only once and still achieve the same effect?
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
BindRepeater();
}
}
protected void BindRepeater()
{
// data retrieval
// repeater binding
}
protected void CreateData(string newdata)
{
// data insert
}
protected void TextBox1_TextChanged(object sender, EventArgs e)
{
if (TextBox1.Text != string.Empty)
{
string _newData = TextBox1.Text.Trim();
CreateData(_newData);
BindRepeater();
}
}
Use an event that would be fired after the text changed event to do the binding in. you can now remove it from the page load event.

Handling User Control Events on Containing Page

I want to have a user control containing a save button.
I want the click event attached to the save button to be handled by the containing page not the user control
Is this possible?
EDIT MORE SPECIFIC
Say I have a page called "Editor Page" and a user control called "Editor Buttons".
On the "Editor Buttons" user control I have a save button (which is a web control). I want the Click event of the save button to be handled by the "Editor Page" rather than the user control.
The reason for this is because I want to specify base pages that implement common behaviour.
This technique is commonly referred to as “event bubbling”.
Here is an exmple:
public partial class Test : System.Web.UI.UserControl
{
public event EventHandler buttonClick;
protected void Button1_Click(object sender, EventArgs e)
{
buttonClick(sender, e);
}
}
Then you can subscribe the event buttonClick at webpage to display the different information .
public partial class Test: System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
UserControlID.buttonClick+=new EventHandler(UserControlID_buttonClick);
}
protected void UserControlID_buttonClick(object sender, EventArgs e)
{
Response.Write("hello,I am jack.");
}
}
I will assume that you have the code for the UserControl. If that is the case, you can define a Save event in your user control and have it expose the Click event of the save button, either directly, or by internally setting up an event handler and raise the Save event when the button is clicked. That could look something like this in the UserControl:
public event EventHandler Save;
private void SaveButton_Click(object sender, EventArgs e)
{
OnSave(e);
}
protected void OnSave(EventArgs e)
{
// raise the Save event
EventHandler save = Save;
if (save != null)
{
save(this, e);
}
}
UserControl1.ascx
UserControl1.ascx.cs
public partial class UserControl1 : System.Web.UI.UserControl
{
public event EventHandler UserControl1Click;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btn1_Click(object sender, EventArgs e)
{
UserControl1Click(sender, e);
}
}
UserControl2.ascx
UserControl1.ascx.cs
public partial class UserControl2 : System.Web.UI.UserControl
{
public event EventHandler UserControl2Click;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btn2_Click(object sender, EventArgs e)
{
UserControl2Click(sender, e);
}
}
Then add one Asp.Net Text box(to display user control id) and above two user controls to Aspx page as shown below.
<title>Untitled Page</title>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="txt1" runat="server"></asp:TextBox> <br />
<UC1:UC ID="uc1" runat="server" />
<UC1:UC ID="uc2" runat="server" />
</div>
</form>
Now add event handler for each user control to handle button click event of two controls as shown below.
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
uc1.UserControl1Click += new EventHandler(uc1_UserControl1Click);
uc2.UserControl2Click += new EventHandler(uc2_UserControl2Click);
}
protected void uc1_UserControl1Click(object sender, EventArgs e)
{
txt1.Text = "User Control 1 Clicked";
}
protected void uc2_UserControl2Click(object sender, EventArgs e)
{
txt1.Text = "User Control 2 Clicked";
}
}
If we click User Control1, text box displays “User Control 1 Clicked” or if we click on User Control2, text box displays “User Control 2 Clicked”.

Pass MasterPage ImageButton event to content Page

I have ImageButton in a MasterPage. I want the OnClick event to fire and be captured by the .ASPX page hosted inside the MasterPage?
MasterPage:
<asp:ImageButton ID="btnClear" OnClick="Clear_Click"
ImageUrl="images/Back_Icon_06.png" runat="server" AlternateText="Clear"
width="38" height="39"/>
The masterpage is actually a child of the page (in fact, it's a UserControl). We don't want the page to have to be aware of the intimate details of its child controls (thats why we delegate those aspects to those controls in the first place), so the correct approach would be to handle the click event on the master page and from there fire another event on the masterpage which the page handles:
Master:
public event EventHandler SomethingHappened;
protected void Button_Click(object sender, EventArgs e)
{
OnSomethingHappened(EventArgs.Empty);
}
protected void OnSomethingHappened(EventArgs e)
{
if(this.SomethingHappened != null)
{
this.SomethingHappened(this, e);
}
}
Page:
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
//allows us to change master pages
if(this.Master is MyMaster)
{
((MyMaster)this.Master).SomethingHappened += new EventHandler(HandleSomethingHappened);
}
}
private void HandleSomethingHappened(object sender, EventArgs e)
{
//deal with it
}
I would recommend specifying a strongly typed master page in your content page and exposing the event on the master page side
Here's a good MSDN reference
This is similar to what Rex M specified but just simplifies accessing the Master Page a little bit.
// Master Page Code
public event EventHandler ClearClick
{
add
{
this.btnClear.Click += value;
}
remove
{
this.btnClear.Click -= value;
}
}
// Content Page markup
<%# Page masterPageFile="~/MasterPage.master"%>
<%# MasterType virtualPath="~/MasterPage.master"%>
// Content Page Code
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
this.Master.ClearClick += new EventHandler(OnClearClick);
}
private void OnClearClick(object sender, EventArgs e)
{
// Handle click here
}

Resources