RegisterOnSubmitStatement after client-side validation - asp.net

I need to insert a bit of Javascript into the process when a Web Form is submitted, but after the client side validation takes place.
RegisterOnSubmitStatement seems to place the javascript before the validation.
Anyone know how to get it to render after?
Solution found:
In a web control, I put something like this:
protected override OnInit(EventArgs e) {
Page.SaveStateComplete += new EventHandler(RegisterSaveStuff);
base.OnInit(e);
}
void RegisterSaveStuff(object sender, EventArgs e) {
Page.ClientScript.RegisterOnSubmitStatement(typeof(Page), "name", "JS code here");
}

that´s right, the RegisterOnSubmitStatement DO NOT WORK in the init function.
It should be called after in the page lige cycle.
I thing the right place therefor is:
"PreRenderComplete"
protected override OnInit(EventArgs e)
{
Page.PreRenderComplete+= new EventHandler(Page_PreRenderComplete);
base.OnInit(e);
}
void Page_PreRenderComplete(object sender, EventArgs e)
{
Page.ClientScript.RegisterOnSubmitStatement(typeof(Page), "name", "JS code here");
}

After some research online and playing around with it, I figured out that you can do it by hooking into the Page's SaveStateComplete event. I guess the validation submit statement is registered during the PreRender event, so if you register it after that (in the SaveStateComplete event) you can get it afterward.
You do have to re-register it, but that's not a big deal because I'm not relying on ViewState for my JS.

Related

Web Forms Response.RedirectToRoute and Ajax UpdatePanel?

I added a button in datalist in updatepanel (Web Forms/AJAX). Can we use Response.RedirectToRoute() in codebehind? I would like to send cat_id with it ofcourse if it's possible.
I can use Response.Redirect in button click :
protected void goto_Click(object sender, EventArgs e)
{
Response.Redirect("Page.aspx?cat_id=3", true); // working
}
I can't use Response.RedirectToRoute in button click:
protected void goto_Click(object sender, EventArgs e)
{
Response.RedirectToRoute("go", new { cat_id= 3 }); //not working
}
I was going to be crazy.. i saw it's visual studio's problem. Response.RedirectToRoute(); is working in UpdatePanel. However, i coudn't understand why it didn't work (because it's same code) for a length of time.
Cheers

Page_Init and Page_Load

A page containts custom address control and checkBox. Why does the second example of code work properly, but first doesn't?
//1
protected void Page_Init(object sender, EventArgs e)
{
//doesn't work properly
ucLegalAddress.Visible = !chkLegalAddress.Checked;
}
//2
protected void Page_Load(object sender, EventArgs e)
{
//works properly
ucLegalAddress.Visible = !chkLegalAddress.Checked;
}
Because the viewstate of the controls is loaded between the init and the load event. This means that the init event does not know the state of the client yet.
MSDN lifecycle overview
Because all controls are create in OnInit() method, that call between Page_Init and Page_Load. In Page_Init all controls are null. Read more

UserControl Property Changing

I have created a User Control(Popupcontrol) and in that control i have created a property(PageType) and when i am using the Popupcontrol on the page then i set the property(pagetype) according to the page.
but now there is some problem i have to two button on the page and on the second button click i want to change the pagetype property .So is there any solution for the same.
Based on your comment, it seems you bind the data (PageType property in your question) in the Page_Load event, instead of this it should be done in overrided DataBind method which should be called if the page is not in post back request (otherwise your data will be overwritting in the next Page_Load event as you mentioned in your comments):
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
{
DataBind();
}
}
public override void DataBind()
{
PageType = someValue;
}
after this your click handler may looks like:
protected void button2_Clicked(object sender, EventArgs e)
{
PageType = someOtherValue;
}
Are you setting the variable in a page load event? You may need to add:
if (!Page.IsPostback) {
// Code here.
}

Click event not work properly

In my website I wrote that code:
protected void Page_Load(object sender, EventArgs e){ LinkButton lbtnTopicAddress = new LinkButton(); lbtnTopicAddress.Click += lbtnSpecificTopic1_Click;}
protected void lbtnSpecificTopic1_Click(object sender, EventArgs e){ Server.Transfer("~/SpecificTopic.aspx)"
}
But when I press on the link in run time, the caller doesn't go to the EventHandler method.
Why?
Note,
I wrote code like that in many pages in the same website but it work only in one page.
i added that code to many page in website but it worded only in one page every page has its specific code and no relation between them I hope you understand me thanks
I need help pleaseeeeeeee..........................
Did you mean to miss off a ;and a } here?
protected void lbtnSpecificTopic1_Click(object sender, EventArgs e){ Server.Transfer("~/SpecificTopic.aspx)"
I assume you've put a breakpoint in to ensure it isn't being fired?
I'm not exactly sure but I've got a feeling that instead of Page_Load you need to use Page_Init so your code would look this this:
protected void Page_Init(object sender, EventArgs e)
{
LinkButton lbtnTopicAddress = new LinkButton();
lbtnTopicAddress.Click += lbtnSpecificTopic1_Click;
}
protected void lbtnSpecificTopic1_Click(object sender, EventArgs e)
{
Server.Transfer("~/SpecificTopic.aspx");
}
p.s. 5 mins formatting your code can work wonders when trying to debug
Are you adding the button to the controls on your page, or are you trying to find the "lbtnTopicAddress" control on your page?
Simply declaring the button won't do anything -- you have to get a reference to the control itself from the page.

two UserControls, one page, need to notify each other of updates

I've got a user control thats used twice on the same page, each have the ability to be updated (a dropdown list gets a new item) and I'm not sure what might be the best way to handle this.
One concern - this is an older system (~4+ years, datasets, .net2) and it is amazingly brittle. I did manage to have it run on 3.5 with no problems, but I've had a few run-ins with the javascript validation (~300 lines per page) throwing up all over the place when I change/add/modify controls in the parent.
Add an event to your user control.
public event EventHandler MyEvent;
protected void OnMyEvent(EventArgs e)
{
if(MyEvent != null)
{
MyEvent(this, e);
}
}
protected void AddOptionAdded(object sender, EventArgs e)
{
OnMyEvent(EventArgs.Empty);
}
Then in your page you can subscribe to both controls event.
protected void Page_Load(object sender, EventArgs e)
{
WebUserControl1.MyEvent += OnMyEventHander;
WebUserControl2.MyEvent += OnMyEventHander;
}
protected void OnMyEventHandler(object sender, EventArgs e)
{
// Notify the other controls that something changed.
}
Then in your page's event handler you can do whatever you need to do to update the other control. Calling a method, etc.
You can also go as far as creating your own delegate and EventArgs classes to pass additional custom data that may be needed.
Didn't even need most of it, I forgot to mention it was vb, so I put this in the user control...
Public Event UpdateListings As EventHandler
Public Function SomethingToDo
'doing some cool stuff ...not really
RaiseEvent UpdateListings(Me, EventArgs.Empty)
Return result
End Function
then on the code behind on the parent page
Protected Sub UpdateStuff(ByVal sender As Object, ByVal e As EventArgs) Handles userControl1.UpdateListings, userControl2.UpdateListings
userControl1.BindStuff()
userControl2.BindStuff()
End Sub

Resources