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...
Related
Button on click method is not calling
Button code :
<asp:Button ID="personalSub" runat="server" ValidationGroup="personal" Text="Save" CausesValidation="false" OnClick="InsertPersonalDetail" />
C# Code :
protected void InsertPersonalDetail(object sender, EventArgs e)
{
Console.WriteLine("hello");
MessageBox.Show("hello");
}
If you have any problem on the page then you must see a compiler error.
You do NOT have compiler error witch is means that asp.net finds the InsertPersonalDetail function on code behind.
From what I see you call inside the button click two functions that are for desktop programming (not for web page).
Neither one can have any visible effect on your click - there is no console there to see anything, neither user interface to open the MessageBox.
protected void InsertPersonalDetail(object sender, EventArgs e)
{
Console.WriteLine("hello"); // have no effect on web page
MessageBox.Show("hello"); // have no effect on web page
}
So its called but you don't see it by just wait a pop up to appears
To check this out, run it with debuger and add a break point there.
Or add a literal on page and add some text there to verify that is called.
eg, add on page
<asp:Literal runat="server" ID="txtLiteral" />
and on code behind
protected void InsertPersonalDetail(object sender, EventArgs e)
{
txtLiteral.Text += "InsertPersonalDetail called <br />";
}
Asp button click does not fire. I wrote Response.Write("404.aspx"); in to button click event in codebehind. But it does not fire. Here is the page with a fire problem button:
please click here
thanks.
Here is the code behind:
protected void bntTest_Click(object sender, EventArgs e)
{
Response.Redirect("404.aspx");
}
protected void btn2_Click(object sender, EventArgs e)
{
Response.Redirect("404.aspx");
}
It does not work because your submit button is not inside the form tag so it won't be able to post the submitted data and the same time, the click event will not fire as the page does not post, you can see below:
EDIT:
You have added a nested form tag that you can not because form tag can not be nested within another form tag. So just remove the nested form tag and it will work.
Try to restructure your project using these guidelines:
Only add form elements to aspx pages
Add main content to MasterPage from pages
Add any content that needs to be nested within a form to a UserControl that is placed within a page.
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 created one page.In that page,I wrote one javascript function for displaying character count,when user enter something in textbox.
protected void Page_Load(Object sender, EventArgs e)
{
TextBox1.Attributes.Add("onkeyup", "DisplayCharCount()");
}
First time pageload,textbox is blank. Textbox does not have autopostback== true.After
pageload,user enter text into textbox.This time page is not loaded again.So when this function get called?
Page_Load will call everytime a page reloaded. so onkeyup will bind to the textbox everytime a page reloaded.
When this function get called?
This event is triggered when the user releases a Key while typing on TextBox1.
You can also bind it for the first time only:
protected void Page_Load(Object sender, EventArgs e)
{
if(!IsPostBack)
{
TextBox1.Attributes.Add("onkeyup", "DisplayCharCount()");
}
}
Or you can also bind it directly as:
<asp:TextBox ID="TextBox1" runat="server" TextMode="MultiLine" onkeyup="DisplayCharCount()">
</asp:TextBox>
The Page_Load method is called every time a user requests the page. The DisplayCharCount method is called whenever a user pressed a key (that is, release the key) in the TextBox1 element.
I've got an ASP.NET page on which I've got a GridView which is part of a form. I've enabled editing on the GridView, so it causes a postback to the page.
I've also got a form on the page, which causes a postback when it's submitted.
Can someone tell me how I can distinguish between the two postbacks? When the form is submitted I need to handle that, but not for example when someone hits edit on the GridView.
Thanks.
Have you got some code which looks like
<asp:Button OnClick="ButtonEventHandler" />
<asp:GridView OnRowEditing="GridViewEditEventHandler" />
you could do what ever it was you need to do in the eventhander specific to the event you were interested in handling at that point.
protected void ButtonEventHandler (object sender, EventArgs e)
{
// do stuff from the button click event
}
protected void GridViewEditEventHandler(object sender, GridViewEditEventArgs e)
{
// do something else from the gridview event
}