ASP CompareValidator and NOT submitting form - asp.net

I have a textbox and a submit button. The textbox is a date-entry field.
Attached to it, I have a compare validator with the type set to "date". It does validate and show an error message.
Problem is, the user can still click on the submit button. I'd like to prevent that. If the user has entered something like 03/hello/2011, he or she should not be able to submit the form.
How can I accomplish this?
Any ideas?
Thanks,
Jason

Associate the validator and the submit button in a single validation group. Both of them have the property validation group. Provide a name say pageValidation to both the control's property.

I've encountered this problem myself, that a page with validation errors can still continue on to submission when the user clicks the submit button.
What you can do is something like this :
protected void submitClicked(object sender, EventArgs e)
{
if (!Page.IsValid)
{
// somehow the user was able to submit their form even though there are
// validation errors. Stop here and let ASP.NET present the error messages
// to the user
return;
}
// do submission stuff here like putting things in the database
}

Related

checking for username in database exists or not

I want that if a user enters a username and as soon as he clicks tab or goes to the next option, a validation should be displayed whether the username is available or does not exist in the database. I don't want to use the functionality of the button of check availablity.
http://www.aspdotnet-suresh.com/2011/03/how-to-check-username-availability.html this might help it uses ajax .
Create a simple webservice that takes string as an argument and returns true/false as reply based on the availability of the username.
Now create a javascript function and call this webservice through the javascript function.
Call this javascript function on the "onchange" event of the input element(textbox) where user is entering the username.
Put a span element in-front of the textbox(input) and show relevant message based on the availability of the username.
That's it.
You can find the code on the following URL:
validate username ajax and json and asp.net
1st make textbox is autopostback =true
2nd go to events of the text box and go to textchanges event that will fire as soon as you make tab or textbox unfocused
3rd write the code that get the username from db and check exist or not from here
protected void TextBox1_TextChanged(object sender, EventArgs e)
{
//get user name from db
}

Can you do a Form submission with Javascript disable

If a user has disabled Javascript in their client browser and they try to use a Form on the Web Page, this causes a Postback. My understanding is that the Form would create a Javascript function __doPostBack to handle the Form submission, but with Javascript disabled a normal Postback occurs. When checking what as been posted back to the server, the Form is empty. Can a Form work with Javascript disabled?
A form ABSOLUTELY works without Javascript.
Just put your form consumption code in the button click event.
something like this perhaps
void btnSubmit_Click(Object sender,
EventArgs e)
{
// When the button is clicked,
// send the values of the input fields to the database.
}
The form will be submitted without javascript.
Any client-side validation will not run, so you may have invalid data. This is why you always, always perform server-side validation of your forms.
Now, you state that your form is empty. If you depend on javascript, jquery, or some other client-side programming to set values in your fields, then you may see empty values. I'm not sure where you're checking, but if you take a look at the request object in your page_load, you should see your data there.

Firing Postback in ASP.NET Without javascript

Is this possible?
EDIT
I want to user the asp:ListView
List item
I want to use it for editing
I dont want the postback to use Javascript when I put it into the edit mode
I've been trying to use a to do this instead of a link button but to no avail.
.
The only way to trigger a PostBack without JavaScript is by using a submit button. This is an HTML limitation, not an ASP.NET one.
I'm a little unsure of what you're trying to do without more detail. If I knew more specifically what you're trying to accomplish I could give you more details.
Like you mentioned in your post, you could use the <a> tag to send the user to the page you want.
You can add information to the link like so:
Click here for a new item
Then in the page load of newpage.aspx you can check what the action the user selected was, the query parameters are stored in the Request (Language is C#).
protected void Page_Load(object sender, EventArgs e)
{
string action = Request.Params["action"];
if(!String.IsNullOrEmpty(action))
{
switch(action)
{
case "newitem":
//handle the new item action
break;
case "deleteitem":
//handle the delete item action
break;
//handle other actions.
}
}
}
EDIT: You should be aware that the <a> tag will send the user to the page specified, the page though will act as if it is the first time the user has visited the page. With that said, the page variable IsPostBack will be false.

Refresh the page after a postback action in asp.net

I have command button added in my asp.net grids. After performing an action using that button, we refresh the grid to reflect the new data. (basically this action duplicates the grid row).
Now when user refresh the page using F5, an alert message is displayed (to resend the information to server) if we select "retry", the action is repeated automatically.
I know this is a common problem in asp.net, how can we best handle this?
Search for GET after POST - http://en.wikipedia.org/wiki/Post/Redirect/Get - basically, redirect to the current page after you're finished processing your event.
Something like:
Response.Redirect(Request.RawUrl)
If you think you don't need postback paradigm, you might want to look at ASP.NET MVC.
The problem is that asp.net buttons perform form posts when you push a button. If you replace the button with a link your problem should go away. You can also use a button that performs a javascript function that sets the document.location to the address of your page.
If I well understood, you simply have to check if you are in a post-back situation before populating your grid.
Assuming you do that on Page_Load, simply surround the operation with post-back test like this:
private void Page_Load(object sender, EventArgs e)
{
if(!this.IsPostBack)
{
// populate grid
}
}
You need to call response.Redirect(Request.Url.ToString());
or you can wrap the grid with updatepanel and after every command bind the datasource to grid
Inside your <asp:Repeater> tag put this:
EnableViewState="false"
This will cause your control to refresh every time the page loads, no matter if it's a postback or not.
for example:
if you click on 'button' system will catch the event 'button_click'.
if you refresh the page, system will re execute again the same event.
to don t have this problem, in your event insert :
on your event
private void button_click(object sender, System.EventArgs e)
{
button.Enabled =false;
button.Enabled =true;
}
is what you meant?

Determine which button inside a user control sent the event

I have a user control that has several buttons.
On page_load, I want to run a method unless a specific button was pressed.
When I check the sender on page_load inside the user control, I just get the name of the user control and not the button itself.
Is there a way that I can determine what button was pressed on page_load? Otherwise I will have to come up with some hacky method to solve the issue.
I think you can check Request.Form ("__EVENTTARGET") - that should contain the ClientID of your control.
This refers to the value of a hidden field the ASP.NET event handling framework uses to keep track of what the user clicked. When the user triggers a post-back, some JavaScript on the page sets this hidden field to the ClientID of the control you clicked before submitting the form.
Can you create a property in your user control to return the clicked button (or set a flag or whatever), an set it in each button's click event inside the user control?
Are you sure you're handling the page model correctly? The Page Load event happens (to build the server side object model), then your control is going to handle the button click event bound to the control.
Page Load can come for any number of postback reasons besides buttons in your user control being clicked.
What about buttons in other controls on the page?
There's sometimes good reasons to do this, but I also I worry that you're just hacking around the ASP.NET page model.
Here's a simple way to check if a specific button was pressed:
protected bool isButtonClicked(string buttonName)
{
bool isClicked = false;
foreach (string ctl in this.Request.Form)
{
if (ctl.EndsWith(buttonName))
{
isButtonClicked = true;
break;
}
}
return isClicked;
}

Resources