How to validate WebUser Control inside Page - c#

I have a web user control inside my page. In the web user control I have a radio button list and a required field validator to check whether any of the radio buttons are clicked or not. However, on the main page I have a button which, when clicked, should check the validation. I don't know how to do that.
Here is the structure:
Page -> usercontrol -> radiobuttonList
|
|-> button
When the button is clicked, I would like the web user control radiobuttonlist validator to perform its validations and set Page.IsValid accordingly.

If you set the validationgroup property on all your controls and button to the same string it won't matter if the validators are in separate controls or all on the one web form.
I.E validationgroup="MyFormToValidate"
Here is the documentation on that property:
http://msdn.microsoft.com/en-us/library/ms227424(v=vs.100).aspx

You can use a CustomValidator Control and specify the OnServerValidate method.
protected void cvRBLCheck_ServerValidate(object source, ServerValidateEventArgs args)
{
args.IsValid = radiobutton.Checked;
}
On your button, ensure that CausesValidation is set to true.
When your button is clicked, the OnServerValidate method will fire and you can do your checks in there.

Related

add custom web user control in onclick event handler function

I have an asp.net project and I have a panel in it in which I want to add multiple user control on the panel by clicking on the button, It does this when clicking on the button
System.Web.UI.Control _msg = (System.Web.UI.Control)Page.LoadControl("msgsend.ascx");
pnl.control.add(_msg)
but it clears all the user controls on the panel before and adds only one user control. What should I do to avoid this?

Reset button c# asp.net and clear validation controls

I have a c# asp.net registration form , but i'm unable to use the reset button to achieve the objective I want. Upon clicking the reset button, the validators I have demand that I fill up ALL textboxes before I can reset the fields.
If a user were to fill up half of the form and use the reset fields, the reset button does not achieve the objective I want it to, as all textboxes have to be filled.
The codes I'm using are as follows:
protected void resetButton_Click(object sender, EventArgs e)
{Response.Redirect(Registration.aspx);}
Since this looks like Asp.Net Web Forms, as opposed to MVC, On the aspx file where you set up the button, set the CausesValidation property to false.
Link: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.button.causesvalidation(v=vs.110).aspx
Example:
<asp:Button CausesValidation="False" />
Add this to the server-side handler of the reset button:
Response.Redirect("~/Registration.aspx", true);
you can clear the modelstate on the controller. ModelState.Clear();

How to caputure page level events on a user control on that same page

Is it posible to caputure button click event that is on a page, to a User Control which is with in that page.
If you are trying to pass a parent's button click event to a child, you can do it.
In the User Control, make a method called HandleParentButtonClick(passing necessary parameters). In the parent's OnClick event, call the User Control's HandleParentButtonClick method.

UpdatePanel's PostbackTrigger that is inside a usercontrol inside a masterpage?

I have a masterpage and inside that masterage is a user control that has a toolbar with a save button. I then have an aspx page that inherits form t he master page. In that page I have and updatepanel. Is it possible to set the post back trigger to the Save button inside the usercontrol?
You should be able to use Master.FindControl("MySaveButton") from within the content page, and attach it to the scriptmanager's trigger list:
this.MyScriptManager.RegisterAsynchPostBackControl(Master.FindControl("MySaveButton"))
Unless I'm not understanding the question correctly.
Check the fourth post down (marked as answer) here, it ought to help.
In short, create an PostBackTrigger instance, set fields appropriately and then add to the UpdatePanel's Triggers collection.
For example (from linked site):
//Creates a new async trigger
AsyncPostBackTrigger trigger = new AsyncPostBackTrigger();
//Sets the control that will trigger a post-back on the UpdatePanel
trigger.ControlID = "btnCommit";
//Sets the event name of the control
trigger.EventName = "Click";
//Adds the trigger to the UpdatePanels' triggers collection
pnlMain.Triggers.Add(trigger);
I went with a different approach of find my controls. I used this method. I have used this in the past and not sure why I didn't think about it earlier. In my user control I expose controls as properties. In my master page I created a property that allows me to get the user control instance. In my page I can call this: Master.UserControlName.PropertyInControl
So, if I expose a button or control in the user control, I should be able to add that to the trigger collection.

ASP.NET - How to track event from previous postback?

I have a requirement to check for a certain condition on postback before redirecting (Response.Redirect) to another page.
Note... I cannot use JavaScript to detect whether or not to confirm (this is also a requirement) :s
Pseudo:
protected void lbtnRedirect_OnClick(object sender, EventArgs e)
{
if (showConfirm)
{
// Set flag for client side
this.ShowConfirm = true;
// Track this event for next postback.
}
else
{
Response.Redirect("somepage.aspx");
}
}
If the showConfrim flag == true, then the client will be show a modal dialog box asking them if they are sure they want to redirect. If the user clicks on "Yes", then the page posts back and the desired effect is that the lbtnRedirect_OnClick event is fired. How would I about tracking the lbtnRedirect event?
Edit:
I have no problem tracking the flag to show the modal (yes JS must be used to show the modal... somethings you just cannot get rid of :)). I should have been more clear.
It is when the user clicks "Yes" to continue the redirect. The page will postback again but needs to know which event to go through.
i.e. Suppose there are 3 onclick events, 1) lbtnRedirect1_Onclick 2) lbtnRedirect2_OnClick 3) lbtnRedirect3_OnClick... each of which does the confirm check.
Each onclick event does the check. So when the user clicks on "Yes" on the modal, how does the page know which event to drop back into?
You can use ViewState if you're in WebForms.
Implement a ShowConfirm property encapsulating ViewState["ShowConfirm"].
In the first postback you'll set ShowConfirm 'true', and this will activate that modal during the render (if ShowConfirm is true, that's setting as visible 'true' some control).
In the next postback, you'll set ShowConfirm 'false' because is 'true', and finally you'll do the whole redirect!
You can use an ajax call from javascript to set the required values.
Since the postback will happen before even the execution reaches to your button click event we need a workaround here, And if you don't need JS as your requirement, so take a look at
Implementing Client Callbacks Programmatically without Postbacks in ASP.NET
This is much like a wrapper for XMLHttp Ajax call IMHO.
You cannot easily create a model form, without javascipt.
One suggestion I would make is to have panels in your page.
Panel one is visible.
On submit one; panel one hides and panel two is visible asking for a confirmation.
On panel two is a confirm button, clicking this button your redirection is performed.

Categories