I have an ASP.NET control that has an onclick event handler rendered inline on the element. I would like to call that function and have it raise the target control's server side event handler.
<asp:CheckBox ID="Foo"
runat="server"
AutoPostBack="true"
Text="Foo" />
<a href="#"
onclick="javascript:setTimeout('__doPostBack(\'Foo\',\'\')', 0)">Test
</a>
I created the checkbox, looked at the rendered function on the field, and then copied that into the onclick on the anchor element.
The anchor will raise a postback, but the event handler for the check box is not raised.
protected override void OnLoad(EventArgs e)
{
// fires for checkbox
// fires for anchor (the anchor does cause a postback)
}
void Foo_CheckedChanged(object sender, EventArgs e)
{
// fires for checkbox
// does not fire for anchor
}
protected override void OnInit(EventArgs e)
{
this.Foo.CheckedChanged += new EventHandler(Foo_CheckedChanged);
}
Is it possible to do this?
Don't try to manually determine what the javascript should look like for a postback. Use the proper API for the task... in this case, this is exactly what GetPostBackEventReference is for.
myControl.Attributes.Add("onclick", "javascript:" + ClientScript.GetPostBackEventReference(targetControl));
Why not convert the tag to an asp:linkbutton and then have the one handler for both
protected override void OnInit(EventArgs e)
{
this.Foo.CheckedChanged += new EventHandler(Foo_CheckedChanged);
this.Bar.Click+= new EventHandler(Foo_CheckedChanged);
}
The client ID of you checkbox might now be "Foo".
Use the following to make sure you use the correct ClientID for your element
<a href="#"
onclick="javascript:setTimeout('__doPostBack(\'<%= Foo.ClientID %> \',\'\')', 0)">Test
</a>
Related
I'm teaching myself to use a site.master page with child webforms embedded using the ContentPlaceHolderID object. I'm figuring it out, but I have one question; is it possible to put a button on the site.master page that fires code on the codebehind pages of the child forms? If I can do that, it will really simplify what I'm trying to accomplish.
I tried to 'inherit' both codebehinds, but of course that didn't work. Is there a way to do this?
Yes there is a way to do this. You need to set UseSubmitBehavior button property to false and then you can access the control that causes the post back using Request.Params.Get("__EVENTTARGET"); So the code would look like this:
Button definition in Site.Master markup:
<asp:Button ID="MyButton" runat="server" Text="Button" UseSubmitBehavior="false" />
code behind within any inherited page:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
// in case you have code to be executed on first load
}
else
{
string myButtonCtrl = Request.Params.Get("__EVENTTARGET");
if (myButtonCtrl != null && myButtonCtrl.EndsWith("MyButton"))
{
// MyButton has been clicked!
}
}
}
I have one master page with two content page each content page has submit button:
<asp:ImageButton runat="server" type="image" id="buttonSubmit" name="buttonSubmit"
alt="ImageButton 1" src="images/button.png" OnClientClick="PreventExitPop=true"/>
I want to be able to create one onclick event on the masterpage.cs:
protected void buttonSubmit_Click(object sender, EventArgs e)
{
//
}
and attach the two buttons from the content pages to this event.
<asp:ImageButton runat="server" type="image" id="buttonSubmit" name="buttonSubmit"
alt="ImageButton 1" src="images/button.png"
onclick="buttonSubmit_Click"
OnClientClick="PreventExitPop=true"/>
The problem is that each content page knows only his code file and not the masterpage.cs.
You could write event handlers for each control and in those handlers you call a event handler method in the master page
protected void buttonSubmit_Click(object sender, EventArgs e)
{
((MasterType) this.Master).buttonSubmit_Click(sender, e);
}
i think you should not be able to do that,
and its not true that a event handler bind to two separate event at all...
i think, if you can, you should create your button on master page...
if you can not, you should have two event handler for these buttons, but u can create a method and in this method do everything you want, and call this method from two defined handlers...
I have a user control which has html elements like <input type="button".... and i want to set its display property on preRender state.
Would you please explain, what kind things i have to handle this user control? So, in this function
protected override void OnPreRender(EventArgs e) { }
I have only EventArgs e and it doesn't have proper method or properties to bring me the html of user control.
Thank you from now...
The easiest way to control visibility of elements from the server side is to promote them to server controls. For example:
<input id="mybutton" runat="server" type="button" ...
Doing so would allow you to execute a statement like the following in your OnPreRender() event:
mybutton.Visible = false; // removes the element
Or...
mybutton.Style[HtmlTextWriterStyle.Display] = "none"; // styles the element
I have a user control, which is added to another user control. The nested user control is built up of a GridView, an image button and a link button. The nested user control is added to the outer control as a collection object based upon the results bound to the GridView.
The problem that I have is that my link button doesn't work. I click on it and the event doesn't fire. Even adding a break point was not reached. As the nested user control is added a number of times, I have set image button to have unique ids and also the link button. Whilst image button works correctly with its JavaScript. The link button needs to fire an event in the code behind, but despite all my efforts, I can't make it work. I am adding the link button to the control dynamically. Below is the relevant code that I am using:
public partial class ucCustomerDetails : System.Web.UI.UserControl
{
public event EventHandler ViewAllClicked;
protected override void CreateChildControls( )
{
base.CreateChildControls( );
string strUniqueID = lnkShowAllCust.UniqueID;
strUniqueID = strUniqueID.Replace('$','_');
this.lnkShowAllCust.ID = strUniqueID;
this.lnkShowAllCust.Click += new EventHandler(this.lnkShowAllCust_Click);
this.Controls.Add(lnkShowAllCust);
}
protected override void OnInit (EventArgs e)
{
CreateChildControls( );
base.OnInit(e);
}
protected override void OnLoad(EventArgs e)
{
base.EnsureChildControls( );
}
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
CreateChildControls( );
}
}
protected void lnkShowAllCust_Click(object sender, EventArgs e)
{
this.OnCustShowAllClicked(new EventArgs ( ));
}
protected virtual void OnCustShowAllClicked(EventArgs args)
{
if (this.ViewAllClicked != null)
{
this.ViewAllClicked(this, args);
}
}
}
I have been stuggling with this problem for the last 3 days and have had no success with it, and I really do need some help.
Can anyone please help me?
My LinkButton wasn't firing it's Click event, and the reason was I had its CausesValidation property set to True. If you don't want the link to validate the form, be sure to set this to False.
Try adding your click event to the linkbutton tag:
<asp:LinkButton runat="server" OnClick="linkShowAllCust_Click" />
Or adding it to your Page_Load:
Page_Load(object sender, EventArgs e)
{
this.lnkShowAllCust.Click += new EventHandler(this.lnkShowAllCust_Click);
}
Is the usercontrol within the gridview? If so register the event handler on the gridview's onrowcreated event.
It appears that you have a viewstate issue. Because the control isn't there when the viewstate is loaded the application doesn't know how to hook up the event to be fired. Here is how to work around this.
You can actually make your app work like normal by loading the control tree right after the loadviewstateevent is fired. if you override the loadviewstate event, call mybase.loadviewstate and then put your own code to regenerate the controls right after it, the values for those controls will be available on page load. In one of my apps I use a viewstate field to hold the ID or the array info that can be used to recreate those controls.
Protected Overrides Sub LoadViewState(ByVal savedState As Object)
MyBase.LoadViewState(savedState)
If IsPostBack Then
CreateMyControls()
End If
End Sub
I had the same issue. I had viewstate="false" on the page I was adding the control to. (on the aspx page)
i have seen some code in which people pass parameter through commandParameter Property of web control.then what is use of eventargs.
This can be useful if you have the same EventHandler method for different buttons. Example, say your markup looks like this:
<asp:Button ID="button1" runat="server" CommandArgument="MyVal1"
CommandName="ThisBtnClick" OnClick="MyBtnHandler" />
<asp:Button ID="button2" runat="server" CommandArgument="MyVal2"
CommandName="ThatBtnClick" OnClick="MyBtnHandler" />
You can have the same event handler for both buttons and differentiate based on the CommandName:
protected void MyBtnHandler(Object sender, EventArgs e)
{
Button btn = (Button)sender;
switch (btn.CommandName)
{
case "ThisBtnClick":
DoWhatever(btn.CommandArgument.ToString());
break;
case "ThatBtnClick":
DoSomethingElse(btn.CommandArgument.ToString());
break;
}
}
Source: aspnet-passing-parameters-in-button-click-handler
Various Button type controls in .NET have an OnCommand event as well as an OnClick event. When using the OnCommand event you have additional parameters you can apply to the Button such as CommandName and CommandArgument. These can then be accessed in the CommandEventArgs.
It is useful in places where you want to assign the same method to multiple buttons and use the CommandName and CommandArgument parameters to indicate what functionality clicking that button will produce.
The EventArgs class is a base class. Other web methods use other Event args.
e.g. a LinkButton has an OnClick event with a default EventArgs parameter, but it also has an OnCommand event that takes a CommandEventArgs : EventArgs parameter which has some extra information (namely CommandName & CommandArgument).
The event args base class is just there as a place holder so that all the EventHandlers conform to a similar signature.