I have an ASP.NET web site with master/content pages. On the master page, there is a link to log-in and that brings up a modal jQuery form. How can I make sure that the info that is submitted on this form (which is just a DIV in my master page) is handled by a particular postback event?
Keep in mind that the modal can be submitted from any number of pages and I want to make sure that when the modal is submitted, the postback event of the content page is ignored while the postback of the master page handles the form.
if you leave the postback address blank (in html) it will post to the same page. if you can't do this, try passing in a variable with the page's address.
You can't prevent your Content page from detecting that a postback has happened. The way MasterPages/Content Pages work is that the MasterPage is applied to the Content page after OnPreInit at that point the MasterPage is accessibly from the Content page. However, if you define the button handler in your MasterPage
<asp:Button ... OnClick='myHandler'>
you can do any processing from within the MasterPage's code-behind.
protected void myHandler(object sender, EventArgs e)
{...handle button click...}
But, that handler will be triggered after the Content page and Master Page has fired all events before the OnLoadComplete() page method
Related
Currently, in a dot net project, i have a master page & a child page. I have given various options, like textbox, combo box etc, on a master page with a submit button. Now i have a child page where i want to process the request sent by master page.
I have heard that retaincontrolstate is used like
private void retainControlState()
{
}
But currently i am not recieving the values from the masterpage onto the child page, not even the click event of the master page.
How can i make the click of master page button to send the request to the child page ?
You should:
Make sure you can access the controls on your master page from the page, e.g. make them public fields
In the Page_Init of your page, cast your the Master property to its proper type (or just use the MasterType directive - https://msdn.microsoft.com/en-us/library/ms228274(v=vs.100).aspx)
Then you can hook up the event handler in your page to the controls on your master page
ControlState has nothing to do with this.
Does it make sense to handle stuff on your master page in your content page's code-behind though? Why not simply do this in the Master page's code-behind?
I have a MasterPage sectioned into a <div> tag for a Logo, Banner and a ASCX LogIn control, with the rest of the page in a <div> having the <asp:ContentPlaceHolder> to host all my ASPX pages. The Login control is in an Update panel.
Problem :
Some of the ASPX pages have an ASCX page with a input form with text boxes with Custom Validators. When I click on the LogIn button, the resulting postback also tries to post this input data and fails since the forms are blank with no data....and thus the postback for the LogIn too does not go through. On other ASPX pages with no validations, the LogIn goes through.
I could solve this by having a separate page for Login...maybe with a ModalPopup etc ? But isn't there any other way to tackle 2 buttons on the same page....one of which comes from the Master ?
If you aren't validating the controls in the login form, just use CausesValidation="false" on the login button. If you are validating the login form, use Validation Groups as recommended by MikeSmithDev to control which controls are validated on submit. Creating a Validation Group for just the login controls should segregate them from the other controls on the page.
I am using asp.net Routes in the Global.asax:
routes.MapPageRoute("Home", "home", "~/index.aspx");
routes.MapPageRoute("Profiles", "profile/{nick}/{profid}", "~/Profile.aspx");
I have in my master page a top menu, in which there is a LinkButton with OnClick event.
Problem is, the postback works only on routed pages like "HOME" but not in the Profile (which is constructed out of /asdf/12345 instead of just /1235.
When clicking the top menu LinkButton in the profile page I get 404 page.
it doesnt even post to the page!
what can be wrong?
thanks
Problem solved.
I had to put in my Masterpage:
this.form1.Action = Request.RawUrl;
this fixed the form's action!
Bog standard postback stuff going on a la this link: http://ruslany.net/2008/10/aspnet-postbacks-and-url-rewriting/
Adding form1.Action = Request.RawUrl; fixes it as you'd expect, but there is one bug...
I have an ashx which serves a page depending on the parameters. This page, and in fact, all pages, have a Page.master masterpage. On this page is a button, and it is hooked into an onclick event in the codebehind. Clicking this button however does nothing, because the form action code above is breaking it. If I comment it out the button click works fine, but none of my navigation does.
mysite.com/cats/123 is picked up by my ashx using rewrite just like the example up top.
When the button is clicked, the form1.Action = Request.RawUrl; which is in the master page load event fires, and sets the action to mysite.com/cats/123 as you'd expect.
At this point the page is served, but the onclick method (which is in the master page) does not fire. Any idea why?
Edit: Maybe my ashx has to do something to preserve the postback data?
When an asynchronous postback happened inside update panel, another postback happens also for MasterPage not only update panel embedded page .
I want to prevent this MasterPage postback .
is this possible ?
think like i have a MasterPage
and another page which is test.aspx which is content page of MasterPage
i have update panel at test.aspx
when asynchronous postback happens at this test.aspx update panel it also loads MasterPage Page_Load
i want to prevent this (it should not also load MasterPage Page_Load)
Thank you
ASP.NET's UpdatePanel (as well as any normal asp.net page)'s postback does postback the master page as well.
Normally, to properly deal with postbacks, programmers check for it and program accordingly :
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
dataBindAndSetUpControls();
}
}
So, my answer here is that I don't know if it is possible (and if it is, it certainly isn't easy), and that your request is not a normal thing to do, and programmers handle PostBacks via the blurb above.