Reset button c# asp.net and clear validation controls - c#

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();

Related

With ASP.NET, how can I change what a dynamically-generated button does when pressed?

Right now I'm generating a bunch of information pulled from an API and storing it into strings, and displaying that info into cells in a table. I'd like to have a button on each row that, if the user presses it, will grab the information specific to that row. The goal is to be able to click a button and have that information transferred to another list/table of 'selected' items.
I'm relatively new to ASP.NET in general, and I'm aware of how button 'onclick' methods work when you've manually added the button to a web page yourself, but not how to go about doing so when they're generated through code.
If you create an event handler in your code behind for the button, you can then wire each button up to that event handler. Using the parameters of the handler, you know which button was handled.
public void Button_Clicked(object sender, EventArgs e)
{
var button = sender as Button;
button.DoSomething();
}
And each of your buttons would be attached to the event.
<asp:Button OnClick="Button_Clicked" />

stop postback from code behind using c#

I have a button and I want to stop post back whenever someone clicks on that button.
I don't want to use return false like this
<asp:Button runat="server" Text="Click" OnClick="Unnamed1_Click" OnClientClick="javascript:return false;" />
I want to stop it from code behind.
I want to create a div and some content inside the div , so when ever someone click on that button another div will be created with it's contents, but the problem is when I click the button and a post back happen , it remove the first div and construct the second one and so on
Something you can do is to disable your button after a user click on them it also disable the postback behaviour and prevent future postback, in this way
protected void Unnamed1_Click(object sender, EventArgs e) {
YourButton.Enabled = false;
}

ASP.Net and Ajax Server code behind constant re-create

I am writing a single ASP Form that dynamically changes Div boxes that are visible from mouse clicks on buttons and when text in textboxes is changed in an Ajax container. The problem i have is in the Page_Load function i create all the objects (text boxes, radio buttons, buttons, etc) dynamically from a .csv file template sitting on the asp hosting server. Everytime a postback happens, even within the Ajax window, the Page_Load function is called again and the .csv file is re-read, and all objects are re-created.
I have tried Checking for IsPostback before re-creating any objects but the objects are then nullified as they have never been created. The page is as a completely new page every single time.
Any ideas would be greatly appreciated.
Try using the Page_PreInit event rather than Page_Load to re-create/manipulate your dynamic controls:
protected void Page_PreInit(object sender, EventArgs e)
{
// create controls here
}
More info: http://msdn.microsoft.com/en-us/library/ms178472.aspx
When you create a dynamic control, you must accept the responsibility of
re-creating the control upon each postback.
You should create the control in the Page_Init event for every page
request whether it's a postback or not. If you create your controls in the Page_Init event then following that the
user entered values should automatically be filled in upon each postback so
you can acces them.

Set Focus to an Element from code behind after submit in the same page

I want to set focus to an element (a button) after the user clicks on Submit, but the focus is not on a new page it is in the same page. What is happening is that when they click submit i am evaluating a few conditions.. if one of them is met I send them back to the same page (or do not re-direct) in other terms but it still postsback so, when it postsback I again after the submit, i want to set the focus to this item.. how can i do this?
So you want to set the focus of the page to a WebControl on the page? i.e. a <asp:Button>?
You can use ControlId.Focus()
EDIT
If you are talking about the Scroll position after postback, then you could try this
<%# Page MaintainScrollPositionOnPostback="true" %>
Now I have never used it before or tried it, so not sure if it works.
Despite your question doesn't seem too clear to me, I am assuming that you want to set focus to the asp.net control, so you can do
protected void Page_Load(object sender, EventArgs e)
{
if(Page.IsPostBack)
{
if(vartemp === vartemp2) //assuming that you want to set focus when specific condition meets
myButton1.Focus();
}
}
Use a hiddenfield and set its value with the controlId of the control you are validating before making the postback. and in the pageload event you can check the value of the hidden field and focus that control.
Or you can use validationcontrols for validating.

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