potentially dangerous Request error

In asp.net C# sometimes we get potentially dangerous Request error. potentially dangerous Request error comes when we try to pass special character's like <,>,:...etc. In order to solve the potentially dangerous Request error we need to set validateRequest="false" in page directive for perticular page like:

<%@ Page Language="C#" AutoEventWireup="true" <b>ValidateRequest="false"</b>%>


Or if we want to set it for whole web application page then we can set it in web.config in section like

<pages smartNavigation="false" <b>validateRequest="false"</b> enableSessionState="true"></pages>

If we are using newer version of .net framework then we need to set "requestValidationMode="2.0"" in "" in web.config like:
<httpRuntime maxRequestLength="10000" executionTimeout="3600" <b>requestValidationMode="2.0"</b>/>

Assign value using jQuery

assign values in jQuery
In asp.net applications we need to assign value in javascript or jQuery to the controls like label or hiddenfield. We will see sample code that assigns value to lable and hidden field using jQuery.

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<script src="Scripts/jquery-1.4.1.js" type="text/javascript"></script>
<script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
<title>assign value in jQuery</title>
<script type="text/javascript">
$(document).ready(function () {

});

function AssignValue() {
$("#message").html('New Value assigned');
$("#hiddenValue").val('New Value assigned');
return false;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<p>
<label id="message">
Some Value</label>
<input type="submit" name="btnsubmit" id="btnsubmit" value="LogIn" onclick="return AssignValue();" />
<input type="hidden" id="hiddenValue" />
</p>
</div>
</form>
</body>
</html>

In above example if we want to assign value to hidden field and get hidden field value in code behind we need to remove "return false;" statement from jQuery code so that page will get postback.

jQuery validate form empty fields

validate empty fields using jQuery
In Web forms we need to validate fields on the form to check field should not be empty. For example for login form we need to validate textboxes for username and password to check textbox not empty.

Example to validate textbox not empty using jQuery

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<script src="Scripts/jquery-1.4.1.js" type="text/javascript"></script>
<script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
<title>Validate empty field using jQuery</title>

<script type="text/javascript">
$(document).ready(function () {

});

function Validate() {
var username = $("#username").val();
var password = $("#pwd").val();

if (username == "" && password == "") {
alert("Enter username and password");
return false;
}

if (username == "") {
alert("Enter username");
return false;
}

if (password == "") {
alert("Enter password");
return false;
}

}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<p> 
<label for="userName">Username</label>
<input type="text" name="log" id="username" value="" size="50" tabindex="10" />
</p> 
<p> 
<label for="pwd">Password</label> 
<input type="password" name="pwd" id="pwd" value="" size="50" tabindex="20" />
</p> 
<p> 
<input type="submit"  name="btnsubmit" id="btnsubmit" value="LogIn" onclick="return Validate();"/>
</p> 
</div>
</form>
</body>
</html>

Get RadioButtonList value in jQuery

Get radiobuttonlist value using jquery
In asp.net while using radiobuttonlist we need to find selected radio buttons value in jQuery. We see example how to get value of selected radio button in jQuery.

Example to get radio button value in jQuery

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<script src="Scripts/jquery-1.4.1.js" type="text/javascript"></script>
<script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
<title>Get radiobutton value in jQuery</title>

<script type="text/javascript">
$(document).ready(function () {
$('#radiolist-container input').click(function () {
alert($(this).val());
}); 

});
</script>
</head>
<body>
<form id="form1" runat="server">
<div id="radiolist-container">
<p><label>Get radiobutton value in jQuery</label> 
<asp:RadioButtonList ID="radioOption" runat="server"> 
<asp:ListItem Text="ASP.NET" Value="ASP.NET"></asp:ListItem> 
<asp:ListItem Text="C#" Value="C#"></asp:ListItem> 
</asp:RadioButtonList></p>
</div>
</form>
</body>
</html>