Proper validation is ASP.net - asp.net

i have a field ProductID and another field ProductName.I would like a validation where if product ID is entered and product type is not selected it should alert user to select product Name (combo box) on the submit button click.
What will be the appropriate way to do it? I do not wish to write any script hence cannot use custom validator.
how else can i write it?
i am using ASP.NET C#.
Thanks

You can use jQuery to do this very easily. Just wire a click event handler on your submit button. Take a look at this fiddle -
http://jsfiddle.net/8efopLff/1/
JQuery code
$("#btn").click(function(){
if($("#field1").val() != "" && $("#field2").val() == "")
{
alert("Please select field 2");
}
return false;
});

Related

Hiding buttons in one web form through another web from in asp.net

I have a problem and it is like that I have two forms in asp.net, In one form I have Button1 one and in another form I have two buttons, Button1 and Buton2 now when I click Button1 in first web form the buttons of other web form should hide, how this can be done in asp.net
Solution1 : You can use Sessions as below on Button_Click Event
Session["DisableButtons"] = "True"
In 2nd page of Load Event you can check as below
if(!IsPostBack)
{
if (Session["DisableButtons"] = "True")
{
btn1.Visible = False;
btn2.Visible = False;
}
}
Solution 2 : You can send a variable's value in the URL when you are doing Response.Redirect and you can fetch from URL using Request.QueryString.
Try using jQuery
$(document).ready(function(){
$("#ButtonID1").click(function(){
$("#ButtonID2").hide() //or .toggle(); if you want to hide/show
});
})
Send values from one form to another form
you can use the logic from here for hiding the contents of webfroms using another either you can use Session .
I don't have enough Reputation else it will be posted in comment
.
Thank you
Udit Bhardwaj

Validation for Kendo AutoComplete when user does not select the value

I want the validation for kendo AutoComplete when user does not select any value from AutoComplete Value that is populated.
For Little Bit background say I do have two controls one as Kendo AutoComplete and other is Input box if user types something in the AutoComplete the values is populated and he does not select any value and switch to next control it must give a validation message that please select the value from the AutoComplete.
and Also if the user type any string in the AutoComplete and Switch to next control it must also give the validation that "hey,this value is not in the AutoComplete" so that it must not save any other data other than AutoComplete datas that is being Populated.
Heres how your auto complete is defined
#(Html.Kendo().AutoComplete()
.Name("countries")
// etc.
On submit button click event
<script>
$("#btnSubmit").click(function(){
var autoCompleteValue = $("#countries").val();
if (autoCompleteValue == "" || autoCompleteValue == null){
return false; //cancels submit action
}
// else let submit go through
});
</script>
The null check is probably useless, cuz if it doesn't have a value it's usually equal to ""
The .val() solution did not work for me, because the .val() value of the control is still the text I entered (custom text, not any of the AutoComplete values), and that's wrong.
Solution is in preventing the custom input of Kendo AutoComplete. See this link:
Kendo UI - Prevent Custom User Input in the AutoComplete

Prevent ASP.NET textbox within a form from submitting the form

Here's the page I'm working on:
http://mcstevenswholesale.com/catalog.aspx
The textbox below the catalog allows you to skip to a specific page in the catalog. However, if you hit enter in that textbox rather than clicking the "Go" button, it submits the search function in the upper left column. I thought this would be because the entire page is located within an ASP form, but I don't have the same problem with the email signup box on the right side.
I have tried putting the page number textbox into its own HTML form, and have tried changing the button to an image, a button, and a submit input. None of these have worked. I don't want the page to reload, just the page to flip. I'm fairly new to ASP, so I'm sorry if I'm making a very obvious mistake.
Here's the code for the page number textbox (goToPage is a JS function that flips the catalog page):
<div id="goToPage">
Go to Page:
<input id="pageTextbox" type="text" onkeydown="if (event.keyCode == 13) goToPage();"></input>
<a onclick="goToPage()" href="#"></a>
</div>
The onkeydown makes the page change work, but it still fires the search function. How do I prevent it from doing that?
The default behavior of the enter key is to submit the current form in most (if not all) browsers.
You need to suppress the enter key's default behavior. The proper way to suppress the default behavior is to have the event handler return false.
If goToPage() returns false the simplest solution is the following:
onkeydown="if (event.keyCode == 13) return goToPage();"
If not you can add return false after the call to goToPage();
onkeydown="if (event.keyCode == 13) { goToPage(); return false} "
That will cause the enter key to not submit the form when pressed within the page number text box. If you want to disable it for the entire page see the following:
http://webcheatsheet.com/javascript/disable_enter_key.php
https://stackoverflow.com/questions/6017350/i-need-javascript-function-to-disable-enter-key
Try this:
onkeydown="if (event.keyCode == 13) { goToPage(); return false; }"

hide div when click on form or focus on another control or press escape key

I am developing an web application. In this i provided a functionality means "autocomplete textbox". In this control, I need to show up 4 columns whenever user press keys in textbox, i need to collect these keys and send it to the service which will give us result as xml. then i convert xml to dataset and binded with datagird. for this i used jquery. after the result displayed ( i mean the result in datagrid which is placed in div), then i need to hide the div when the user clicks outside of the div or press escape key...
for this i used onblur event. but, when i click on the result then, i could not fire the click event for the div..
here is my jquery events...
function showList() {
if(document.getElementById("txt1").value.length > 3) {
$("#divList").hide("slow");
$("#divLoading").show();
$.ajax({
type : "POST",
url : "ajaxServerPage.aspx?streetname=" + document.getElementById("txt1").value,
success : function(responseText) {
$("#divLoading").hide();
$("#divList").show();
$('#divList').html(responseText);
//add button click events for buttons which are placed in table
$("#dataGridStreet .rowStyle").click(function(e) {
//Open_ModifyPopup($(this).attr("id"));
clickedRow($(this));
});
} // function(responseText)
});
}
}
How should I do this?
Thanks
Why reinvent the wheel: How about using a plugin to do it for you?

jQuery Autocomplete losing text on AutoPostBack

I have a jQuery Autocomplete field on an ASP.Net Webform and everything has been working great until now. I also have a DropDownList that I have a need to fire onSelectedIndexChanged with AutoPostBack.
When I changed my code to do the AutoPostBack, the text field that has the jQuery AutoComplete on it comes back blank. However, if I look at the source of the page, the text is in the text field. If I now post the form, the page will send back a blank field. My Google-Fu is weak on this one, as I could not come up with any workaround for it.
Has anyone had any issues like this with the Autocomplete field getting blanked out on an AutoPostBack, and how did you get around it?
I can post code if it's really necessary, but I'd need to sanitize a lot of it before I could due to company policy.
How is the autocomplete field being initialized? Is it being set to empty string either on page load from server or by the autocomplete jQuery plugin on document.ready?
If the source code for the plug-in is setting the textbox to empty string on document.ready then try the following:
// Store current textbox value in a var
var temp = $('#mytextbox').val();
// Initialize the autocomplete plugin (winging it on the syntax)
$('#mytextbox').autocomplete();
// Reset the value of the textbox.
$('#mytextbox').val(temp);
If you use jQuery Autocomplete plugin 1.1,
* Revision: $Id: jquery.autocomplete.js 15 2009-08-22 10:30:27Z joern.zaefferer $
Add "autoPostBackSelection: false," in the options Ex:
$.Autocompleter.defaults = {
inputClass: "ac_input",
resultsClass: "ac_results",
loadingClass: "ac_loading",
minChars: 1,
delay: 400,
autoPostBackSelection: false,
...
After, add this just before the "return true; at the end of the "selectCurrent() function.
if (options.autoPostBackSelection == true) {
__doPostBack($input.id, "");
}
Example:
function selectCurrent() {
...
if (options.autoPostBackSelection ==
true) {
__doPostBack($input.id, "");
}
return true;
}

Resources