<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<script src="development-bundle/jquery-1.7.1.js" type="text/javascript"></script>
<script src="js/jquery-1.7.1.min.js" type="text/javascript"></script>
<script src="js/jquery-ui-1.8.18.custom.min.js" type="text/javascript"></script>
<script src="development-bundle/ui/jquery.ui.widget.js" type="text/javascript"></script>
<script src="development-bundle/ui/jquery.ui.dialog.js" type="text/javascript"></script>
<link href="Styles/jquery-ui-1.8.18.custom.css" rel="stylesheet" type="text/css" />
<title>jQuery Popup</title>
<script type="text/javascript">
$(function () {
// Dialog
$('#dialog').dialog({
autoOpen: false,
width: 600,
buttons: {
"Ok": function () {
$(this).dialog("close");
},
"Cancel": function () {
$(this).dialog("close");
}
}
});
// Dialog Link
$('#btnDelete').click(function () {
$('#dialog').dialog('open');
return false;
});
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div id="dialog" title="Dialog Title">
<p>
Do you want to delete record?</p>
</div>
<input type="button" id="btnDelete" value="Delete" />
</form>
</body>
</html>
Confirmationbox in javascript
Sample code to show confirmation box in Javascript
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript">
function Confirmation() {
if (confirm("Do you want to delete record?") == true)
return true;
else
return false;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<input type="button" value="Submit" onclick="return Confirmation();" />
</div>
</form>
</body>
</html>
Disable button jQuery

Disabling button after clicking on it is bit easy in jQuery.We can access button element of the page in jQuery and disable button using jquery. For that we can get button element using Id attribute selector or using class attribute selector.For example We may need to disable submit button after submitting form by clicking on it.
Sample code to disable submit button[jQuery + asp.net]
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" 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></title>
<script type="text/javascript">
$(document).ready(function () {
});
function Disable(btn) {
$(btn).attr("disabled", "true");
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<input type="button" value="Submit" onclick="return Disable(this);" />
</div>
</form>
</body>
</html>
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
<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 "
<httpRuntime maxRequestLength="10000" executionTimeout="3600" <b>requestValidationMode="2.0"</b>/>
Assign value 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
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
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>
Get selected items in checkboxlist asp.net
When using checkbox list control in asp.net we need to find multiple selected items from Checkbox list. For example there is a Checkbox list with subject list and we need to show the items those are selected.
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:CheckBoxList ID="CheckBoxList1" runat="server">
<asp:ListItem Value="0">ASP.NET</asp:ListItem>
<asp:ListItem Value="1">C#.NET</asp:ListItem>
<asp:ListItem Value="2">VB.NET</asp:ListItem>
<asp:ListItem Value="3">WCF</asp:ListItem>
</asp:CheckBoxList>
<br />
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Show Selected" />
<br />
<br />
<asp:Label ID="lblSelected" runat="server" Text=""></asp:Label>
</div>
</form>
</body>
</html>
Code that shows selected item from checkbox list [C#]
using System.Web.UI.WebControls;
namespace aspnetcontrol
{
public partial class CheckboxList : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
lblSelected.Text = "";
foreach (ListItem item in CheckBoxList1.Items)
{
if (item.Selected == true)
{
lblSelected.Text += "Selected Subject/s: " + item.Text + "
";
}
}
}
}
}
Get checkbox control in gridview C# - findcontrol
Example to get checked checkboxes in gridview[C#]
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" BorderWidth="1px"
CellPadding="3" BorderStyle="None" Font-Names="Arial">
<FooterStyle></FooterStyle>
<PagerStyle HorizontalAlign="Left"></PagerStyle>
<HeaderStyle Font-Bold="True"></HeaderStyle>
<Columns>
<asp:TemplateField HeaderText="Select">
<ItemTemplate>
<asp:CheckBox ID="CheckBoxSelect" runat="server" Enabled="true" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField HeaderText="Item No." DataField="ItemNo">
<ItemStyle HorizontalAlign="Center" VerticalAlign="Middle"></ItemStyle>
</asp:BoundField>
<asp:BoundField HeaderText="Item Name" DataField="ItemName">
<ItemStyle HorizontalAlign="Center" VerticalAlign="Middle"></ItemStyle>
</asp:BoundField>
</Columns>
</asp:GridView>
</div>
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
</form>
</body>
</html>
In code behind on click of button we will iterate through gridviews rows and find out checked checkboxes.
using System.Data;
namespace aspnetcontrol
{
public partial class GridviewImages : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GridView1.DataSource = BindImageData();
GridView1.DataBind();
}
}
public DataTable BindImageData()
{
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("ItemNo", typeof(int)));
dt.Columns.Add(new DataColumn("ItemName", typeof(string)));
// Create the four records
DataRow dr = dt.NewRow();
dr["ItemNo"] = 1;
dr["ItemName"] = "Tea";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["ItemNo"] = 2;
dr["ItemName"] = "Coffie";
dt.Rows.Add(dr);
dr = dt.NewRow();
return dt;
}
protected void Button1_Click(object sender, EventArgs e)
{
// Iterate through the Products.Rows property
foreach (GridViewRow row in GridView1.Rows)
{
CheckBox cb = (CheckBox)row.FindControl("CheckBoxSelect");
if (cb != null && cb.Checked)
{
//Do some stuff
}
}
}
}
asp.net add checkbox in gridview
In asp.net we need to add checkbox in gridview to select the row in gridview. To add checkbox in gridview we have to add ItemTemplate inside the asp:TemplateField. And inside ItemTemplate we can add checkbox control. We will how to add checkbox in gridview in asp.net using C#.
Example to add checkbox in gridview
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" BorderWidth="1px"
CellPadding="3" BorderStyle="None" Font-Names="Arial">
<FooterStyle></FooterStyle>
<PagerStyle HorizontalAlign="Left"></PagerStyle>
<HeaderStyle Font-Bold="True"></HeaderStyle>
<Columns>
<asp:TemplateField HeaderText="Select">
<ItemTemplate>
<asp:CheckBox ID="CheckBoxPurchase" runat="server" Enabled="true" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField HeaderText="Item No." DataField="ItemNo">
<ItemStyle HorizontalAlign="Center" VerticalAlign="Middle"></ItemStyle>
</asp:BoundField>
<asp:BoundField HeaderText="Item Name" DataField="ItemName">
<ItemStyle HorizontalAlign="Center" VerticalAlign="Middle"></ItemStyle>
</asp:BoundField>
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
Below code just binds the data to gridview.
using System.Data;
namespace aspnetcontrol
{
public partial class GridviewImages : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
GridView1.DataSource = BindImageData();
GridView1.DataBind();
}
public DataTable BindImageData()
{
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("ItemNo", typeof(int)));
dt.Columns.Add(new DataColumn("ItemName", typeof(string)));
// Create the four records
DataRow dr = dt.NewRow();
dr["ItemNo"] = 1;
dr["ItemName"] = "Tea";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["ItemNo"] = 2;
dr["ItemName"] = "Coffie";
dt.Rows.Add(dr);
dr = dt.NewRow();
return dt;
}
}
}