Escolar Documentos
Profissional Documentos
Cultura Documentos
BLOG A R C H I V E
• ▼ 2008 (261)
o ▼ April (58)
GridView Tips and Trics
How To get IP Address to Country/State using www....
ModalPopupExtender Example for Editing Rows in a
G...
How To Redirect into a new window using
Response.R...
Exception Handling With Update Panel
ASP.NET AJAX: How to call client-side Javascript o...
How To Use ModalPopup
ModalPopup With Textbox and Validator
How To Trigger .net Function With Javascript
How To Disable Right Click
Accessing an array on the client that has been cre...
How To Convert XMLNodeList To DataTable
Send an e-mail with the content of a GridView
IFrame in ASP.NET
IFrame GoogleMAP and ModalPopup
Client Side Validation for CheckBoxes,textbox ins...
How To Convert a DataReader into a DataSet
How To Create ASP.NET Control From XML using XSLT
How To Add multiple dynamic onload events to body
...
How to bind RSS feeds to a GridView
How To Add Search Feature To GridView
How To Add client side calculation for gridview co...
Create new gridview row with javascript
How To Read a DataSet object in Javascript
How To Use XML Script in ASP.net 2.0
Is text/xml-script working ?
Passing arrayList to javascript function
How To Call a WebService with ASP.Net Ajax
When you use the UpdatePanel control you probably
...
How To embedded resource file(like-flash,javascrip...
GridView with DataTable as source with paging and ...
How To Use Menu and Multiview Control
How to read XML file using javascript
How To Create Database Driven Menu in ASP.NET
(C#...
How To -Cross page postbacks in ASP.NET 2.0
Use ICallbackEventHandler in Asp.Net 2.0
Modal Popup from Repeater
Displaying vertical rows in a GridView
How To populating DropDownList control from XML
d...
How to bind selected node values in Gridview
How To Load Web user control inside a GridView
Search And Highlight word in Gridview
How To Freez Header Of Gridview
How To Use Multiview As a Tab Control
Mark a GridView row, and move it with the up and
d...
Function to get parent GridView for GridViewRow
Validate DropDownlist or TextBox Inside GridView
Data-Binding to Methods in ASP.NET(conditionally)
Automatic Edit and save Action in a Html Table
Bind GridView To Generics
GridView Confirm When Delete.
ASP.NET AJAX Tabs Control
Maintain The State Of Checkbox After Postback (Ins...
Showing data from multiple tables effectively us...
How To Validate Calender Control in Asp.net 2.0
How To Bing GridView with Custom Class
passing data from javascript to asp.net cs code on...
How To Add Client Side code to to select Checkbo...
o ► March (97)
o ► February (14)
o ► January (92)
• ► 2007 (3)
o ► December (2)
o ► January (1)
ABOUT ME
D O T NE T LOV E R
VIE W M Y C OM PL E TE P ROF IL E
T H U R S D AY, A P R I L 2 4 , 2 0 0 8
GridView Tips and Trics
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class GridViewComplete : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGrid();
}
}
public void BindGrid()
{
if (Session["dt"] == null)
{
GridView1.DataSource = CreateDGDataSource();
GridView1.DataBind();
}
else
{
GridView1.DataSource = Session["dt"] as
DataTable;
GridView1.DataBind();
}
public DataTable CreateDGDataSource()
{
// Create sample data for the DataList control.
DataTable dt = new DataTable();
DataRow dr;
int i;
int y;
// Define the columns of the table.
dt.Columns.Add(new DataColumn("ID", typeof(int)));
dt.Columns.Add(new DataColumn("Name",
typeof(string)));
dt.Columns.Add(new DataColumn("Description",
typeof(string)));
//Make some rows and put some sample data in
for (i = 1; i <= 5; i++)
{
dr = dt.NewRow();
dr[0] = i;
dr[1] = "Name" + "-" + i;
dr[2] = "Item " + "_" + i;
//add the row to the datatable
dt.Rows.Add(dr);
}
Session["y"] = i;
Session["dt"] = dt;
return dt;
}
}
protected void GridView1_RowCommand(object sender,
GridViewCommandEventArgs e)
{
try
{
if (e.CommandName.Equals("New"))
{
LinkButton btnNew = e.CommandSource as
LinkButton;
GridViewRow row = btnNew.NamingContainer as
GridViewRow;
if (row == null)
{
return;
}
TextBox txtCatName =
row.FindControl("QuantityTextBox") as TextBox;
TextBox txtDescription =
row.FindControl("DescriptionTextBox") as TextBox;
DataTable dt = Session["dt"] as DataTable;
DataRow dr;
int intId = (int)Session["y"];
dr = dt.NewRow();
dr["Id"] = intId++;
Session["y"] = intId;
dr["Name"] = txtCatName.Text;
dr["Description"] = txtDescription.Text;
dt.Rows.Add(dr);
dt.AcceptChanges();
Session["dt"] = dt;
GridView1.DataSource = Session["dt"] as
DataTable;
GridView1.DataBind();
}
catch (Exception ex)
{
}
protected void GridView1_RowEditing(object sender,
GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
BindGrid();
}
protected void GridView1_RowUpdating(object sender,
GridViewUpdateEventArgs e)
{
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Net;
using System.Text;
Response.Write(ipInfo);
}
}
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
ds = Data();
if (!this.IsPostBack)
{
if (Session["parent"] == null)
{
gvCustomers.DataSource =
ds.Tables["Parent"];
gvCustomers.DataBind();
}
else
{
gvCustomers.DataSource = Session["Parent"]
as DataTable;
gvCustomers.DataBind();
}
}
}
private DataSet Data()
{
ds.Tables.Add(dt);
ds.Tables.Add(dtc);
Session["Parent"] = dt;
return ds;
}
protected void BtnViewDetails_Click(object sender,
EventArgs e)
{
// get the gridviewrow from the sender so we can
get the datakey we need
Button btnDetails = sender as Button;
GridViewRow row =
(GridViewRow)btnDetails.NamingContainer;
dv.RowFilter = "Id=" +
Convert.ToString(this.gvCustomers.DataKeys[row.RowIndex].Va
lue);
dvCustomerDetail.DataSource = dv;
dvCustomerDetail.DataBind();
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
}
public static class ResponseHelper
{
if ((String.IsNullOrEmpty(target) ||
target.Equals("_self",
StringComparison.OrdinalIgnoreCase)) &&
String.IsNullOrEmpty(windowFeatures))
{
context.Response.Redirect(url);
}
else
{
Page page = (Page)context.Handler;
if (page == null)
{
throw new InvalidOperationException(
"Cannot redirect to new window outside
Page context.");
}
url = page.ResolveClientUrl(url);
string script;
if (!String.IsNullOrEmpty(windowFeatures))
{
script = @"window.open(""{0}"", ""{1}"",
""{2}"");";
}
else
{
typeof(Page),
"Redirect",
script,
true);
}
protected void btn_Click(object sender, EventArgs e)
{
ResponseHelper.Redirect("popup.aspx", "_blank",
"menubar=0,width=100,height=100");
}
}
</form>
</body>
</html>
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
}
protected void handleClick(object sender, EventArgs e)
{
int a = 0;
int res = 10 / a;
}
protected void HandleError(object sender,
AsyncPostBackErrorEventArgs e)
{
//here we could log the error, and see which
exception we're getting in order to set a specific error
message
//in this case, I'm just returning the current
date/hour back
manager.AsyncPostBackErrorMessage = "Ooops...error
occurred: " +
DateTime.Now.ToString();
}
If you are using AJAX then the only way i have found yet to give an alert to a user on
return to the Asynchronous post back is to add an “end request” handler to the
PageRequestManager.
In this way you can tell the request manager to run a javascript function on returning
from a Asynchronous post back event of AJAX.
function load() {
Sys.WebForms.PageRequestManager.getInstance().add_endReque
st(EndRequestHandler);
}
<%@ Page Language="C#" AutoEventWireup="true"
CodeFile="JavascriptCallAfterAsync.aspx.cs"
Inherits="JavascriptCallAfterAsync" %>
<script runat="server">
protected void txtDataOnChange(object sender, EventArgs
e)
{
txtLength.Text = txtData.Text.Length.ToString();
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Client-side Javascript call after an UpdatePanel
asychronous request</title>
</head>
<script type="text/javascript">
function EndRequestHandler(sender, args) {
if (args.get_error() == undefined)
alert("Your text has: " +
document.getElementById("txtLength").value + "
character(s)");
else
alert("There was an error" +
args.get_error().message);
}
function load() {
Sys.WebForms.PageRequestManager.getInstance().add_endRequ
est(EndRequestHandler);
}
</script>
<body onload="load()">
<form id="form1" runat="server">
<asp:ScriptManager ID="_scriptManager" runat="server"
/>
<div>
<asp:UpdatePanel ID="UpdatePanel1"
runat="server">
<ContentTemplate>
Write something:
<asp:TextBox ID="txtData" runat="server"
AutoPostBack="true"
OnTextChanged="txtDataOnChange" /><br />
Server says the length is:
<asp:TextBox ID="txtLength"
runat="server" AutoPostBack="true" />
</ContentTemplate>
</asp:UpdatePanel>
</div>
</form>
</body>
</html>
.popupControl {
background-color:#AAD4FF;
position:absolute;
visibility:hidden;
border-style:solid;
border-color: Black;
border-width: 2px;
}
.modalBackground {
background-color:Gray;
filter:alpha(opacity=70);
opacity:0.7;
}
.modalPopup {
background-color:#EEEEEE;
border-width:3px;
border-style:solid;
border-color:Gray;
font-family:Verdana;
font-size:medium;
padding:3px;
width:250px;
}
.modalSignUp {
background-color:#FFFFFF;
border-width:3px;
border-style:solid;
border-color:Gray;
font-family:Verdana;
font-size:medium;
padding:3px;
width:250px;
}
.sampleStyleA {
background-color:#FFF;
}
.sampleStyleB {
background-color:#FFF;
font-family:monospace;
font-size:10pt;
font-weight:bold;
}
.sampleStyleC {
background-color:#ddffdd;
font-family:sans-serif;
font-size:10pt;
font-style:italic;
}
.sampleStyleD {
background-color:Blue;
color:White;
font-family:Arial;
font-size:10pt;
}
</style>
<script type="text/javascript">
function ok(sender, e)
{
$find('ModalPopupExtenderLogin').hide();
__doPostBack('LoginBtn', e);
}
function OnKeyPress(args)
{
if(args.keyCode == Sys.UI.Key.esc)
{
$find("ModalPopupExtenderLogin").hide();
}
}
function body_onkeydown()
{
if(event.keyCode==13 || event.keyCode==27)
{
var _defaultButtonName=getDefautButtonName(
event.keyCode==13 ? "submitButton" : "cancelButton");
var frm=document.forms[0];
if(frm && document.all(_defaultButtonName))
{
document.all(_defaultButtonName).click();
}
}
}
function getDefautButtonName(className)
{
var _defaultButtonName="";
var children =
document.getElementsByTagName("input");
for (var i = 0; i < children.length; i++)
{
var child = children[i];
var btnAction = child.buttonAction;
if(btnAction == className)
{
_defaultButtonName = child.id;
break;
}
}
return _defaultButtonName;
}
</script>
</head>
<body onkeydown="body_onkeydown()">
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<br />
<table style="width: 297px">
<tr>
<td style="width: 36px">
<asp:LinkButton ID="Loginlnk"
runat="server"
Width="47px">Login</asp:LinkButton></td>
</tr>
<tr>
<td colspan="2">
<asp:Label ID="successLabel"
runat="server"
Width="273px" Visible="False"></asp:Label></td>
</tr>
</table>
<br />
<div>
<ajaxToolkit:ModalPopupExtender
ID="ModalPopupExtenderLogin"
runat="server" TargetControlID="Loginlnk"
PopupControlID="LoginPanel"
BackgroundCssClass="modalBackground" DropShadow="true"
OkControlID="LoginBtn" OnOkScript="ok()"
CancelControlID="CancelBtn" />
<br />
<br />
<asp:Panel ID="LoginPanel" runat="server"
CssClass="modalPopup" Style="display: none"
Width="278px" Font-Names="@MS PGothic">
<table style="width: 270px">
<tr>
<td style="width: 30%" align="right">
<asp:Label ID="Label10"
runat="server" Height="23px"
Text="UserName :"></asp:Label>
</td>
<td style="width: 179px">
<asp:TextBox ID="loginId"
runat="server" Width="75%"></asp:TextBox></td>
</tr>
<tr>
<td style="width: 30%" align="right">
<asp:Label ID="Label9"
runat="server" Height="23px"
Text="Password :"></asp:Label>
</td>
<td style="width: 179px">
<asp:TextBox ID="pwd"
runat="server" Width="75%"></asp:TextBox></td>
</tr>
<tr>
<td style="width: 94px">
</td>
<td style="width: 179px">
<asp:Button ID="CancelBtn"
runat="server" Text="Cancel" Width="52px"
buttonAction="submitButton" />
<asp:Button ID="LoginBtn"
runat="server" Text="Log In" OnClick="LoginBtn_Click"
buttonAction="cancelButton" /></td>
</tr>
</table>
<br />
</asp:Panel>
<br />
</div>
</form>
</body>
</html>
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
}
protected void LoginBtn_Click(object sender, EventArgs e)
{
//Add your DB Authentication Module here....
//This is just for testing
if (loginId.Text.Equals("user") &&
pwd.Text.Equals("user"))
successLabel.Text = "Welcome User";
else
successLabel.Text = "Authentication
Failed...Retry";
successLabel.Visible = true;
Loginlnk.Visible = false;
.modalBackground
{
background-color: Gray;
filter: alpha(opacity=50);
opacity: 0.50;
.updateProgress
{
border-width: 1px;
border-style: solid;
background-color: #FFFFFF;
position: absolute;
width: 180px;
height: 65px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager runat="server" ID="Scriptmanager1">
<Scripts>
<asp:ScriptReference
Assembly="Microsoft.Web.Preview"
Name="PreviewScript.js" />
</Scripts>
</asp:ScriptManager>
<script type="text/javascript">
function postbackFromJS(sender, e) {
var postBack = new Sys.Preview.PostBackAction();
postBack.set_target(sender);
postBack.set_eventArgument(e);
postBack.performAction();
}
</script>
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
}
protected void OKButton_Click(object sender, EventArgs e)
{
if (IsValid)
{
//do
}
else
{
ModalPopupExtender.Show();
}
}
}
T U E S D AY, A P R I L 2 2 , 2 0 0 8
How To Trigger .net Function With Javascript
</div>
</form>
</body>
</html>
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
function clickIE4(){
if (event.button==2){
alert(message);
return false;
}
}
function clickNS4(e){
if (document.layers||
document.getElementById&&!document.all){
if (e.which==2||e.which==3){
alert(message);
return false;
}
}
}
if (document.layers){
document.captureEvents(Event.MOUSEDOWN);
document.onmousedown=clickNS4;
}
else if (document.all&&!document.getElementById){
document.onmousedown=clickIE4;
}
document.oncontextmenu=new Function("alert(message);return
false")
// -->
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
cstext.Append("script>");
String csname = "ConcatScript";
Type cstype = typeof(Page);
cs.RegisterClientScriptBlock(cstype, csname,
cstext.ToString(), false);
}
}
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;
GridView1.DataSource =
ConvertXmlNodeListToDataTable(elemList);
GridView1.DataBind();
int TempColumn = 0;
TempColumn++;
dt.Columns.Add(dc.ColumnName =
dc.ColumnName + TempColumn.ToString());
else
{
dt.Columns.Add(dc);
DataRow dr = dt.NewRow();
dr[j] = xnl.Item(i).ChildNodes[j].InnerText;
dt.Rows.Add(dr);
return dt;
P OSTE D BY D OT NE T LOVE R AT 12 :2 1 PM 0 C OM M E N TS
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Net.Mail;
using System.IO;
}
protected void Button1_Click(object sender, EventArgs e)
{
MyPage tmpPage = new MyPage();
smtpClient.Send(mailMessage);
}
public DataSet GetData()
{
DataSet ds = new DataSet();
DataTable dt = new DataTable("Product");
DataRow dr;
dt.Columns.Add(new DataColumn("Price",
typeof(Int32)));
dt.Columns.Add(new DataColumn("DisCount",
typeof(Int32)));
dt.Columns.Add(new DataColumn("SellPrice",
typeof(Int32)));
for (int i = 1; i <= 10; i++)
{
dr = dt.NewRow();
dr[0] = i;
dr[1] = i * 2;
dr[2] = 1 * 3;
dt.Rows.Add(dr);
}
ds.Tables.Add(dt);
Session["dt"] = dt;
return ds;
}
}
MyPage.cs
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
/// <summary>
/// Summary description for MyPage
/// </summary>
public class MyPage : Page
{
public override void
VerifyRenderingInServerForm(Control control)
{
GridView grid = control as GridView;
if (grid != null && grid.ID == "GridView1")
return;
else
base.VerifyRenderingInServerForm(control);
}
}
M O N D AY, A P R I L 2 1 , 2 0 0 8
IFrame in ASP.NET
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
}
public string TheURL = "";
P OSTE D BY D OT NE T LOVE R AT 12 :3 8 PM 0 C OM M E N TS
<script type="text/javascript">
function showDialog()
{
var modalPopupBehaviorCtrl = $find('<%=
mpeMap.ClientID %>');
modalPopupBehaviorCtrl.show();
document.getElementById('divMap').innerHTML
= '<iframe runat="server" id="mm" width="600" height="450"
frameborder="0"
scrolling="no" marginheight="0" marginwidth="0"
src="DisplayGoogleMap.aspx?lat=39.86887222271858&long=-
75.35767078399658&desc=just%26a%26sample"><
/iframe><br />';
}
</script>
</head>
<body>
<form id="Form1" runat="server" action="">
<asp:ScriptManager ID="ScriptManager1" runat="server" />
<a id="showModalPopupClientButton" href="#"
runat="server" onclick="showDialog();">Click
Me</a><br />
<br />
<br />
<asp:LinkButton ID="LinkButton1"
runat="server">LinkButton</asp:LinkButton>
<asp:Panel runat="server" ID="Panel1" Style="display:
none; width: 600px; height: 475px;"
CssClass="modalPopup">
<div id="divMap">
</div>
<asp:Button runat="server" ID="cmdClose"
Text="Close" />
</asp:Panel>
<iframe width="600" height="450" frameborder="0"
scrolling="no" marginheight="0"
marginwidth="0"
src="DisplayGoogleMap.aspx?lat=39.86887222271858&long=-
75.35767078399658&desc=just%26a%26sample">
</iframe>
<br />
<ajaxToolkit:ModalPopupExtender ID="mpeMap"
runat="server" TargetControlID="LinkButton1"
PopupControlID="Panel1"
BackgroundCssClass="modalBackground" DropShadow="true"
CancelControlID="cmdClose" OkControlID="cmdClose" />
</form>
</body>
</html>
DisplayGoogleMap.aspx
<script
src="http://maps.google.com/maps?file=api&v=2&key=ABQIAAAA6
TRHbwQ
EdJQJYJodaur9XhTJQa0g3IQ9GZqIMmInSLzwtGDKaBQN9ma6JnCY1kf4R6
Q-HCj5397ddA"
type="text/javascript"></script>
<script type="text/javascript">
var map;
var icon0;
var newpoints = new Array();
function addLoadEvent(func) {
var oldonload = window.onload;
if (typeof window.onload != 'function'){
window.onload = func
} else {
window.onload = function() {
oldonload();
func();
}
}
}
addLoadEvent(loadMap);
addLoadEvent(addPoints);
function loadMap()
{
var dLat =
Number(document.getElementById('<%= hdLat.ClientID
%>').value);
var dLong =
Number(document.getElementById('<%= hdLong.ClientID
%>').value);
var sCaption =
document.getElementById('<%= hdCaption.ClientID %>').value;
map = new
GMap2(document.getElementById("map"));
map.addControl(new GLargeMapControl());
map.addControl(new GMapTypeControl());
map.setCenter(new GLatLng( dLat, dLong), 13);
map.setMapType(G_MAP_TYPE);
//G_MAP_TYPE, G_SATELLITE_TYPE, or G_HYBRID_TYPE
icon0 = new GIcon();
icon0.image =
"http://www.google.com/mapfiles/marker.png";
icon0.shadow =
"http://www.google.com/mapfiles/shadow50.png";
icon0.iconSize = new GSize(20, 34);
icon0.shadowSize = new GSize(37, 34);
icon0.iconAnchor = new GPoint(9, 34);
icon0.infoWindowAnchor = new GPoint(9, 2);
icon0.infoShadowAnchor = new GPoint(18, 25);
}
function addPoints()
{
var dLat =
Number(document.getElementById('<%= hdLat.ClientID
%>').value);
var dLong =
Number(document.getElementById('<%= hdLong.ClientID
%>').value);
var sCaption =
document.getElementById('<%= hdCaption.ClientID %>').value;
<style type="text/css">
div#popup
{
background:#EFEFEF;
border:1px solid #999999;
margin:0px;
padding:7px;
width:270px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<asp:HiddenField runat="server"
Value="39.869053382069" ID="hdLat" />
<asp:HiddenField runat="server"
Value="-75.35773515701294" ID="hdLong" />
<asp:HiddenField runat="server" Value="Test"
ID="hdCaption" />
<div id="map" style="width: 600px; height: 450px">
</div>
</form>
</body>
</html>
P OSTE D BY D OT NE T LOVE R AT 12 :1 8 PM 0 C OM M E N TS
<script type="text/javascript">
function ValidateCheckBox()
{ validateTextBox();
//get target base & child control.
var TargetBaseControl =
document.getElementById('<%=this.GridView1.ClientID%>');
var TargetChildControl = "chkBxSelect";
//get all the control of the type INPUT in the base control.
var Inputs =
TargetBaseControl.getElementsByTagName("input");
function validateTextBox()
{
//get target base & child control.
var TargetBaseControl =
document.getElementById('<%=this.GridView1.ClientID%>');
var TargetChildControl1 = "txtInput";
//get all the control of the type INPUT in the base control.
var Inputs =
TargetBaseControl.getElementsByTagName("input");
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server"
AutoGenerateColumns="False">
<Columns>
<asp:BoundField HeaderText="n"
DataField="accountno">
<HeaderStyle HorizontalAlign="Center"
VerticalAlign="Middle" Width="50px" />
<ItemStyle HorizontalAlign="Center"
VerticalAlign="Middle" />
</asp:BoundField>
<asp:TemplateField HeaderText="Select">
<ItemTemplate>
<asp:CheckBox ID="chkBxSelect"
runat="server" />
</ItemTemplate>
<HeaderStyle HorizontalAlign="Center"
VerticalAlign="Middle" />
<ItemStyle HorizontalAlign="Center"
VerticalAlign="Middle" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:TextBox ID="txtInput"
runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Button ID="btnPost" runat="server" Text="Post"
OnClientClick="javascript:return ValidateCheckBox();"
OnClick="btnPost_Click" />
<asp:Label ID="lblMsg" runat="server"></asp:Label>
</div>
</form>
</body>
</html>
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
F R I D AY, A P R I L 1 8 , 2 0 0 8
How To Convert a DataReader into a DataSet
}
myTable.AcceptChanges();
DataSet ds = new DataSet();
ds.Tables.Add(myTable);
ds.AcceptChanges();
return ds;
}
}
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;
using System.Xml.Xsl;
using System.Xml.XPath;
using System.IO;
}
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
CreateControl();
}
// called from Page's Init event handler
private void CreateControl()
{
// Load the data source
XPathDocument surveyDoc = new
XPathDocument(Server.MapPath("Control.xml"));
Control.XSL
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:asp="remove">
<xsl:output method="xml" indent="yes" encoding="utf-8"
omit-xml-declaration="yes"></xsl:output>
<xsl:template match="/">
<table>
<xsl:for-each select="//question">
<tr>
<td valign="top">
<xsl:value-of select="@name" />
<xsl:if test="@required = 'yes'">
<asp:RequiredFieldValidator
ErrorMessage="Required
Field" runat="server" ControlToValidate="{@name}" />
</xsl:if>
</td>
<td>
<xsl:if test="@type='text'">
<asp:TextBox id="{@name}"
runat="server" />
</xsl:if>
<xsl:if test="@type='radio'">
<asp:RadioButtonList id="{@name}"
runat="server">
<xsl:for-each select="choice">
<asp:ListItem
Value="{@value}" Text="{@value}">
<xsl:value-of
select="current()"/>
</asp:ListItem>
</xsl:for-each>
</asp:RadioButtonList>
</xsl:if>
<xsl:if test="@type='select'">
<asp:DropDownList id="{@name}"
runat="server">
<xsl:for-each select="choice">
<asp:ListItem
Value="{@value}" Text="{@value}">
<xsl:value-of
select="current()"/>
</asp:ListItem>
</xsl:for-each>
</asp:DropDownList>
</xsl:if>
</td>
</tr>
</xsl:for-each>
</table>
<asp:button id="submit" runat="server" Text="Submit"
/>
</xsl:template>
</xsl:stylesheet>
Control.XML
P OSTE D BY D OT NE T LOVE R AT 12 :5 0 PM 0 C OM M E N TS
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
}
}
T H U R S D AY, A P R I L 1 7 , 2 0 0 8
How to bind RSS feeds to a GridView
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
}
public void BindGrid(string strRss, GridView oGrid, int
iTable)
{
DataSet oDs = new DataSet();
oDs.ReadXml(strRss);
oGrid.DataSource = oDs.Tables[iTable];
oGrid.DataBind();
}
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
if (filter.Trim() == "")
return persons;
return filteredPersons;
}
}
<script type="text/javascript">
function CalcSellPrice2(CurrentPrice, DiscountCtrlID,
CustomerCtrlID)
{
var Discount=
parseFloat(document.getElementById(DiscountCtrlID).value);
var SellPrice=document.getElementById(CustomerCtrlID);
var SellPriceValue = parseFloat(CurrentPrice -
((CurrentPrice*Discount)/100));
//var SellPriceValueRound = Math.round(SellPriceValue,4);
var SellPriceValueRound = SellPriceValue;
SellPrice.innerHTML= SellPriceValueRound ;
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server"
AutoGenerateColumns="False"
OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="List Price"
Visible="True">
<ItemTemplate>
<asp:Label ID="lblCurrentListPrice"
runat="server"
Text='<%# Eval("Price") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Discount">
<ItemTemplate>
<asp:TextBox ID="txtDiscountOnItem"
Width="100px"
runat="Server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Sell Price">
<ItemTemplate>
<asp:Label ID="lblSellPrice"
runat="Server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
}
public DataSet GetData()
{
DataSet ds = new DataSet();
DataTable dt = new DataTable("Product");
DataRow dr;
dt.Columns.Add(new DataColumn("Price", typeof(Int32)));
dt.Columns.Add(new DataColumn("DisCount",
typeof(Int32)));
dt.Columns.Add(new DataColumn("SellPrice",
typeof(Int32)));
for (int i = 1; i <= 10; i++)
{
dr = dt.NewRow();
dr[0] = i;
dr[1] = i * 2;
dr[2] = 1 * 3;
dt.Rows.Add(dr);
}
ds.Tables.Add(dt);
Session["dt"] = dt;
return ds;
}
protected void GridView1_RowDataBound(object sender,
GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Label lblCurrentListPrice =
(Label)e.Row.FindControl("lblCurrentListPrice");
Double CurPr =
Convert.ToDouble(lblCurrentListPrice.Text);
TextBox txtDiscountOnItem =
(TextBox)e.Row.FindControl("txtDiscountOnItem");
Label lblSellPrice =
(Label)e.Row.FindControl("lblSellPrice");
txtDiscountOnItem.Attributes.Add("onchange",
"CalcSellPrice2(" + CurPr + ", '" +
txtDiscountOnItem.ClientID + "','"
+ lblSellPrice.ClientID + "')");
}
}
P OSTE D BY D OT NE T LOVE R AT 12 :5 1 PM 0 C OM M E N TS
function AddNewProfession()
{
var DropDownList
var txtBox
var NewtxtBox
var GridView
var NewDropDownList
var count=document.getElementById('<%=hdValue.ClientID
%>').value;
DropDownList
=document.getElementById("gvProfessions_ctl03_ddlProfession
s");
txtBox
=document.getElementById("gvProfessions_ctl02_txtBox");
GridView =
document.getElementById('<%=gvProfessions.ClientID%>');
NewDropDownList = DropDownList.cloneNode(true);
NewtxtBox=txtBox.cloneNode(true);
NewtxtBox.id = "gvProfessions_ctl05_ddlProfessions";
NewtxtBox.name = "gvProfessions$ctl05$ddlProfessions";
NewDropDownList.id = "gvProfessions_ctl04_ddlProfessions";
NewDropDownList.name = "gvProfessions$ctl04$ddlProfessions";
var newGridViewRow =
GridView.insertRow(GridView.rows.length);
var cellLeft = newGridViewRow.insertCell(0);
var cellRight=newGridViewRow.insertCell(1);
cellRight.appendChild(NewtxtBox);
cellLeft.appendChild(NewDropDownList);
count++;
document.getElementById('<%=hdValue.ClientID
%>').value=count;
return false;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManger1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="u" runat="server">
<ContentTemplate>
<div>
<a href="#" onclick="return AddNewProfession()">Add
new profession</a>
<asp:GridView ID="gvProfessions"
runat="Server" AutoGenerateColumns="false"
OnRowDataBound="gvProfessions_RowDataBound"
EnableViewState="false">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:DropDownList
ID="ddlProfessions" runat="server">
<asp:ListItem Text="A"
Value="A"></asp:ListItem>
<asp:ListItem Text="B"
Value="B"></asp:ListItem>
<asp:ListItem Text="C"
Value="C">
</asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:TextBox ID="txtBox"
runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:HiddenField ID="hdValue"
runat="server" Value="2" />
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
gvProfessions.DataSource = CreateDS();
gvProfessions.DataBind();
if (intNewRow > 3)
{
ReBind();
}
}
protected void gvProfessions_RowDataBound(object
sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
}
Session["dsTemp"] = dt;
}
else
{
dt = (DataTable)Session["dsTemp"];
}
return dt;
}
public void ReBind()
{
DataRow dr;
int j = int.Parse(hdValue.Value);
DataTable dtTest = CreateDS() as DataTable;
for (int i = 3; i <= j; i++)
{
dr = dtTest.NewRow();
dr["ID"] = i;
dtTest.Rows.Add(dr);
Session["dsTemp"] = dtTest;
gvProfessions.DataSource = dtTest;
gvProfessions.DataBind();
}
}
strText += ddl.SelectedItem.Text;
}
txtValue.Text = strText+",";
}
protected void btnPost_Click(object sender, EventArgs e)
{
//as GridView will disappear if the page does a
PostBack. You would need
//to persist them so that they could be added to
the Grid's data source
//and then re-bind the Grid.So That i have used
AJAX enabled Grid controls
}
}
P OSTE D BY D OT NE T LOVE R AT 12 :2 1 PM 0 C OM M E N TS
W E D N E S D AY, A P R I L 1 6 , 2 0 0 8
How To Read a DataSet object in Javascript
Suppose that you have to read the records of DataTable in the DataSet
that was sent as a response value from an Ajaxed Web Service using
javascript.
<script runat="server">
[System.Web.Services.WebMethod]
[System.Web.Script.Services.ScriptMethod]
public static System.Data.DataTable MyMethod(int value)
{
return GetDataSet(value);
}
return dv.Table;
}
</script>
{
str+=result.rows[i].Author +"<br>";
}
document.getElementById('result').innerHTML=str;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="scriptManger1" runat="server"
EnablePageMethods="true">
<Services>
<asp:ServiceReference
Path="~/WebService.asmx" />
</Services>
</asp:ScriptManager>
<div id="result">
</div>
<input type="button" onclick="CallMyWebService();"
id="myButton"
value="Call MyWebService" />
</div>
</form>
</body>
</html>
if you tried, you likely received some sort of circular reference
serialization error to take advantage of this, you will need to add the
following to your web.config:
<system.web.extensions>
<scripting>
<webServices>
<jsonSerialization>
<converters>
<add name="DataSetConverter"
type="Microsoft.Web.Preview.Script.Serialization.Converters
.DataSetConverter,
Microsoft.Web.Preview"/>
<add name="DataRowConverter"
type="Microsoft.Web.Preview.Script.Serialization.Converters
.DataRowConverter,
Microsoft.Web.Preview"/>
<add name="DataTableConverter"
type="Microsoft.Web.Preview.Script.Serialization.Converters
.DataTableConverter,
Microsoft.Web.Preview"/>
</converters>
</jsonSerialization>
</webServices>
</scripting>
</system.web.extensions>
<script type="text/xml-script">
<page>
<components>
<application init="pageInit" />
</components>
</page>
</script>
<script type="text/javascript">
<!--
function pageInit() {
alert("Hello XML Script!");
}
//-->
</script>
</form>
</body>
</html>
Is text/xml-script working ?
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:HiddenField ID="HiddenField1" runat="server"
/>
<input type="button" onclick="Test();"
value="Get Value From ArrayList" />
</div>
</form>
</body>
</html>
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
}
private string ArrayListToString(ref ArrayList
_ArrayList)
{
int intCount;
string strFinal = "";
strFinal += _ArrayList[intCount].ToString();
}
return strFinal;
}
P OSTE D BY D OT NE T LOVE R AT 12 :0 9 PM 0 C OM M E N TS
When we will use ASP.Net Ajax on our pages we need to add the element. The
ScriptManager will automatically add the references to the required JavaScript files
that provide
asp.net ajax functionality. So it’s required on every page where we will use the
ASP.Net Ajax features.
When we want to add a reference to our WebService we use the Service child
element of the ScriptManager:
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="scriptManger1"
runat="server">
<Services>
<asp:ServiceReference
Path="~/WebService.asmx" />
</Services>
</asp:ScriptManager>
<input type="button" onclick="CallMyWebService();"
id="myButton" value="Call MyWebService" />
</div>
</form>
</body>
</html>
using System;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
/// <summary>
/// Summary description for WebService
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo =
WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService]
public class WebService : System.Web.Services.WebService
{
public WebService()
{
[System.Web.Services.WebMethod]
[System.Web.Script.Services.ScriptMethod]
public string MyMethod(string value)
{
return value;
}
$get('UpdateProgress1').style.display = 'block';
}
function EndRequest(sender, args)
{
if (postBackElement.id == 'Button1')
$get('UpdateProgress1').style.display = 'none';
}
</script>
</body>
</html>
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
}
protected void Button1_Click(object sender, EventArgs e)
{
Response.Redirect("AnotherPage.aspx");
}
}
..
.
.
.
[assembly: WebResource("FlashPlayer.swfobject.js",
"text/javascript")]
[assembly: WebResource("FlashPlayer.mediaplayer.swf",
"application/x-shockwave-flash")]
flashUrl =
Page.ClientScript.GetWebResourceUrl(this.GetType(),
"FlashPlayer.mediaplayer.swf");
StringBuilder sb = new StringBuilder();
sb.Append("<script language=\"javascript\">");
sb.Append("var s1 =new SWFObject(");
...
.
.
.
.
.
if
(!Page.ClientScript.IsClientScriptBlockRegistered(GetType()
, "FlashPlayer"))
{
Page.ClientScript.RegisterClientScriptResource(G
etType(),
"FlashPlayer.swfobject.js");
Page.ClientScript.RegisterStartupScript(this.Get
Type(),
"SWFObject", CreateAttributes());
}
}
T U E S D AY, A P R I L 1 5 , 2 0 0 8
GridView with DataTable as source with paging and
sorting
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
}
public String gvSortDirection
{
get { return ViewState["SortDirection"] as
String ?? "ASC"; }
set { ViewState["SortDirection"] = value; }
}
public String gvSortExpression
{
get { return ViewState["SortExpression"] as
String ?? ""; }
set { ViewState["SortExpression"] = value; }
}
gv.DataSource = GetSortedData(dataTable,
gvSortExpression, gvSortDirection);
gv.PageIndex = e.NewPageIndex;
gv.DataBind();
}
protected void gv_Sorting(object sender,
GridViewSortEventArgs e)
{
GridView gv = (GridView)sender;
DataView dv = gv.DataSource as DataView;
DataTable dataTable = dv.Table;
String sortdir = "";
if (e.SortExpression != "" & e.SortExpression !=
null)
{
if (gvSortExpression == e.SortExpression)
gvSortDirection = GetSortDirection();
else
gvSortDirection = "ASC";
gvSortExpression = e.SortExpression;
gv.EditIndex = -1;
}
gv.DataSource = GetSortedData(dataTable,
e.SortExpression, gvSortDirection);
gv.DataBind();
}
private String GetSortDirection()
{
String newSortDirection = String.Empty;
switch (gvSortDirection)
{
case "DESC":
newSortDirection = "ASC";
break;
case "ASC":
newSortDirection = "DESC";
break;
}
return newSortDirection;
}
private DataView GetSortedData(DataTable dataTable,
String SortExpression, String SortDirection)
{
dataTable = GetDataSet();
if (dataTable != null)
{
DataView dataView = new DataView(dataTable);
return dt;
}
}
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
}
protected void Menu1_MenuItemClick(object sender,
MenuEventArgs e)
{
switch (e.Item.Text)
{
case "Add":
MultiView1.ActiveViewIndex = 0;
break;
case "Delete":
MultiView1.ActiveViewIndex = 1;
break;
case "Search":
MultiView1.ActiveViewIndex = 2;
break;
}
}
Supoose you have the follwing XML file . If I want to retrieve game1
moves , how would I do it using the the simplest way in javascript ?
xmlDoc.load("Java.xml");
document.getElementById("displayresults").firstChild.n
odeValue = results;
}
</script>
</head>
<body onload="ProcessXML();">
<form id="form1" runat="server">
<div id="displayresults">
</div>
</form>
</body>
</html>
T H U R S D AY, A P R I L 1 0 , 2 0 0 8
How To Create Database Driven Menu in ASP.NET (C#)
This Post will show you how to binding a Menu Control to SQL Server
database using C#. By this sample, you can create a database driven
menu in ASP.Net.
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
}
public void PopulateMenu()
{
DataSet dst = CreateDS();
foreach (DataRow masterRow in
dst.Tables["Company"].Rows)
{
MenuItem masterItem = new
MenuItem((string)masterRow["CompanyName"]);
Menu1.Items.Add(masterItem);
foreach (DataRow childRow in
masterRow.GetChildRows("ParentChild"))
{
MenuItem childItem = new
MenuItem((string)childRow["Name"]);
masterItem.ChildItems.Add(childItem);
}
}
}
private DataSet CreateDS()
{
DataSet ds;
if (Session["DataList_ParentChild"] == null)
{
ds = new DataSet();
DataTable dt = new DataTable("Company");
DataRow dr;
dt.Columns.Add(new DataColumn("ID",
typeof(Int32)));
dt.Columns.Add(new DataColumn("CompanyName",
typeof(string)));
dt.Columns.Add(new DataColumn("Address",
typeof(string)));
dt.Columns.Add(new DataColumn("Name",
typeof(string)));
dt.Columns.Add(new DataColumn("Dept",
typeof(string)));
for (int i = 1; i < 10; i++)
{
dr = dt.NewRow();
dr[0] = i;
dr[1] = "Company " + i;
dr[2] = "Address " + i;
dr[3] = "Manager name";
dr[4] = "Adminstration";
dt.Rows.Add(dr);
}
ds.Tables.Add(dt);
DataColumn[] Parent_PKColumns = new
DataColumn[1];
Parent_PKColumns[0] = dt.Columns["ID"];
dt.PrimaryKey = Parent_PKColumns;
dt = new DataTable("Employees");
dt.Columns.Add(new DataColumn("ID",
typeof(Int32)));
dt.Columns.Add(new DataColumn("CompanyID",
typeof(Int32)));
dt.Columns.Add(new DataColumn("Name",
typeof(string)));
dt.Columns.Add(new DataColumn("Dept",
typeof(string)));
for (int i = 1; i < 10; i++)
{
int imax = 0;
if (i % 2 == 0) imax = 5;
else imax = 4;
for (int y = 2; y < imax; y++) //3
emplyees for each company
{
dr = dt.NewRow();
dr[0] = y + i * 5;
dr[1] = i;
dr[2] = "Employee # " + dr[0];
dr[3] = "Dept # " + (y + i);
dt.Rows.Add(dr);
}
}
DataColumn[] Child_PKColumns = new
DataColumn[1];
Child_PKColumns[0] = dt.Columns["ID"];
dt.PrimaryKey = Child_PKColumns;
ds.Tables.Add(dt);
DataColumn[] Child_FKColumns = new
DataColumn[1];
Child_FKColumns[0] = dt.Columns["CompanyID"];
ds.Relations.Add("ParentChild",
Parent_PKColumns, Child_FKColumns);
Session["DataList_ParentChild"] = ds;
}
else
{
ds = (DataSet)Session["DataList_ParentChild"];
}
return ds;
}
}
So, in v2.0 they came up with a way to support cross page postbacks.
This is enabled by a button on the first page setting PostBackUrl
property to the page that will handle the postback. Once in the second
page you can access the controls from the previous page by accessing
the Page.PreviousPage property. Here's a sample
CrossPagePostBackSource.aspx
CrossPagePostBackSource.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
}
public int Sum
{
get
{
return ((Int32.Parse(_tb1.Text) +
Int32.Parse(_tb2.Text)) * Int32.Parse(_tb3.Text));
}
}
}
TargetPage.aspx
<%@ PreviousPageType
VirtualPath="~/CrossPagePostBackSource.aspx" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0
Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-
transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
Answer is:
<asp:Label runat="server"
ID="_result"></asp:Label>
</div>
</form>
</body>
</html>
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
_result.Text = PreviousPage.Sum.ToString();
}
}
string _CallBackString;
2. Inherit ICallbackEventHandler in your page.
<script type="text/javascript">
function GetPop()
{
var justExample = 'Hi..All..';
CallPopBack(justExample)
}
function ShowPop(result, context)
{
document.getElementById("txt").value=result;
}
</script>
5.Now, In your code behind (.cs) file, write code for your EventHandle
Method.
<script type="text/javascript">
function GetPop()
{
var justExample = 'Hi..All..';
CallPopBack(justExample)
}
function ShowPop(result, context)
{
document.getElementById("txt").value=result;
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<input type="button" value="Text ICallBack"
onclick="return GetPop()"/>
<input type="text" name="txt" id="txt"/>
</div>
</form>
</body>
</html>
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
}
public string GetCallbackResult()
{
return _CallBackString;
}
W E D N E S D AY, A P R I L 9 , 2 0 0 8
Modal Popup from Repeater
<script type="text/jscript">
function ShowModalWindow(Id)
{
window.showModalDialog("Details.aspx?accountNo="+ Id +"","",
"status:no;dialogWidth:800px;
dialogHeight:570px;help:no;center:yes;resizable:no;scroll:y
es;status:no;");
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Repeater ID="Repeater1" runat="server">
<HeaderTemplate>
<table border="1" width="100%">
<tr>
<th>
COL1</th>
<th>
COL2</th>
<th>
COL3</th>
<th>
COL4</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<a
href='Details.aspx?FrromTest=true
&accountno=<%#
DataBinder.Eval(Container.DataItem,"accountNo")
%>&TypeDesc=<%#
DataBinder.Eval(Container.DataItem,"CompanyName") %>'>
<%#
DataBinder.Eval(Container.DataItem, "CompanyName")%>
</a>
</td>
<td>
<a href='#'
onclick="ShowModalWindow('<%#
DataBinder.Eval(Container.DataItem,"accountNo") %>');">
<%#
DataBinder.Eval(Container.DataItem, "CompanyName")%>
</a>
</td>
<td>
<%#
DataBinder.Eval(Container.DataItem,"Address") %>
</td>
<td>
This is a free column</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
</div>
</form>
</body>
</html>
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
Repeater1.DataSource = GetDataSet();
Repeater1.DataBind();
}
public DataSet GetDataSet()
{
DataSet ds = new DataSet();
DataTable dt = new DataTable("Company");
DataRow dr;
dt.Columns.Add(new DataColumn("accountNo",
typeof(Int32)));
dt.Columns.Add(new DataColumn("CompanyName",
typeof(string)));
dt.Columns.Add(new DataColumn("Address",
typeof(string)));
for (int i = 0; i <= 10; i++)
{
dr = dt.NewRow();
dr[0] = i;
dr[1] = "Company" + i + Environment.NewLine +
"Title" + i;
dr[2] = "Address" + i + Environment.NewLine +
"Title" + i;
dt.Rows.Add(dr);
}
ds.Tables.Add(dt);
return ds;
}
}
Details.aspx
}
}
P OSTE D BY D OT NE T LOVE R AT 3 :05 PM 0 C OMM E N TS
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Net;
using System.Text;
using System.IO;
GridView1.DataSource = FlipDataSet(c());
GridView1.DataBind();
return ds;
}
}
DropDown.XML
<?xml version="1.0" encoding="utf-8" ?>
<GATEWAYS>
<GATEWAY>
<CODE>YXX</CODE>
<NAME>Abbotsford</NAME>
<DESTINATIONS>
<DESTINATION>
<CODE>PVR</CODE>
<NAME>Puerto Vallarta</NAME>
</DESTINATION>
</DESTINATIONS>
</GATEWAY>
<GATEWAY>
<CODE>YKF</CODE>
<NAME>Kitchener</NAME>
<DESTINATIONS>
<DESTINATION>
<CODE>CUN</CODE>
<NAME>Cancun</NAME>
</DESTINATION>
<DESTINATION>
<CODE>X24</CODE>
<NAME>Cancun (Mayan Riviera)</NAME>
</DESTINATION>
<DESTINATION>
<CODE>POP</CODE>
<NAME>Puerto Plata</NAME>
</DESTINATION>
<DESTINATION>
<CODE>PUJ</CODE>
<NAME>Punta Cana</NAME>
</DESTINATION>
</DESTINATIONS>
</GATEWAY>
<GATEWAY>
<CODE>YQL</CODE>
<NAME>Lethbridge</NAME>
<DESTINATIONS>
<DESTINATION>
<CODE>PVR</CODE>
<NAME>Puerto Vallarta</NAME>
</DESTINATION>
</DESTINATIONS>
</GATEWAY>
</GATEWAYS>
T U E S D AY, A P R I L 8 , 2 0 0 8
How to bind selected node values in Gridview
<%@ Page Language="C#" AutoEventWireup="true"
CodeFile="ConditionalBinding.aspx.cs"
Inherits="ConditionalBinding" %>
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;
XmlNodeList lst =
doc.SelectNodes("//student[class='class2']");
IList datasource = new ArrayList();
this.GridView1.DataSource = datasource;
GridView1.DataBind();
}
public class Stu
{
private string name;
}
}
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
BindGrid();
}
public DataTable GetCustomMadeDataTable()
{
//Create a new DataTable object
System.Data.DataTable objDataTable = new
System.Data.DataTable();
//Create three columns with string as their type
objDataTable.Columns.Add("EmployeeId", typeof(int));
objDataTable.Columns.Add("Department",
typeof(string));
//Adding some data in the rows of this DataTable
DataRow dr;
for (int i = 1; i <= 10; i++)
{
dr = objDataTable.NewRow();
dr[0] = i;
dr[1] = "Department" + i.ToString();
objDataTable.Rows.Add(dr);
}
DataColumn[] dcPk = new DataColumn[1];
dcPk[0] = objDataTable.Columns["EmployeeId"];
objDataTable.PrimaryKey = dcPk;
Session["strTemp"] = objDataTable;
return objDataTable;
}
}
else
{
Gridview2.DataSource = GetCustomMadeDataTable();
Gridview2.DataBind();
}
}
}
WebUserControl
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
get
{
return _employeeid;
}
set
{
_employeeid = value;
}
dr = objDataTable.NewRow();
dr[0] = i;
dr[1] = "Address" + i.ToString();
dr[2] = "City" + i.ToString();
dr[3] = "Postalcode" + intIndex.ToString();
objDataTable.Rows.Add(dr);
intIndex++;
}
for (int i = 1; i <= 10; i++)
{
dr = objDataTable.NewRow();
dr[0] = 1;
dr[1] = "Address" + i.ToString();
dr[2] = "City" + i.ToString();
dr[3] = "Postalcode" + intIndex.ToString();
objDataTable.Rows.Add(dr);
intIndex++;
return dv;
}
GridView1.DataSource =
GetCustomMadeDataTable(EmployeeId);
GridView1.DataBind();
<script type="text/javascript">
Sys.Application.add_load(page_load);
Sys.Application.add_unload(page_unload);
function page_load(){
$addHandler($get('FilterText'),
'keydown', onFilterTextChanged);
}
function page_unload(){
$removeHandler($get('FilterText'), 'keydown',
onFilterTextChanged);
}
var timeoutID = 0;
function onFilterTextChanged(e){
if (timeoutID){
window.clearTimeout(timeoutID);
}
timeoutID = window.setTimeout(updateFilterText, 1000);
}
function updateFilterText(){
__doPostBack('FilterText', '');
}
</script>
</body>
</html>
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
if (!IsPostBack)
{
BindGrid();
}
}
public DataTable GetCustomMadeDataTable()
{
dr = objDataTable.NewRow();
dr[0] = i;
dr[1] = "Address" + i.ToString();
dr[2] = "City" + i.ToString();
dr[3] = "Postalcode" + intIndex.ToString();
objDataTable.Rows.Add(dr);
intIndex++;
}
for (int i = 10; i < 20; i++)
{
dr = objDataTable.NewRow();
dr[0] = i;
dr[1] = "bbb" + i.ToString();
dr[2] = "City" + i.ToString();
dr[3] = "Postalcode" + intIndex.ToString();
objDataTable.Rows.Add(dr);
intIndex++;
}
for (int i = 20; i <30; i++)
{
dr = objDataTable.NewRow();
dr[0] = i;
dr[1] = "cde" + i.ToString();
dr[2] = "City" + i.ToString();
dr[3] = "Postalcode" + intIndex.ToString();
objDataTable.Rows.Add(dr);
intIndex++;
GridView1.DataSource = Session["strTemp"] as
DataTable;
GridView1.DataBind();
}
else
{
GridView1.DataSource = GetCustomMadeDataTable();
GridView1.DataBind();
}
return;
int colIndex = GetColumnIndex("Address");
TableCell cell = e.Row.Cells[colIndex];
}
protected void FilterText_TextChanged(object sender,
EventArgs e)
{
UpdateFilter();
}
private void UpdateFilter()
{
DataTable dt = Session["strTemp"] as DataTable;
DataView dv = new DataView(dt);
</head>
<body>
<form id="form1" runat="server">
<table style="border: 1px solid black; width: 600px"
cellpadding="0" cellspacing="0">
<tr>
<td width="193px" nowrap style="border-right:
1px solid black">
ID</td>
<td width="195px" nowrap style="border-right:
1px solid black">
Address</td>
<td width="212px" nowrap>
City</td>
</tr>
<tr>
<td colspan="3" width="600px" style="border-
top: 1px solid black">
<asp:Panel ID="Panel1" runat="server"
Height="250px"
ScrollBars="Vertical" Width="600px">
<asp:GridView ID="GridView1"
ShowHeader="false" runat="server"
AutoGenerateColumns="false"
OnRowDataBound="GridView1_RowData
Bound">
<Columns>
<asp:BoundField DataField="Id"
HeaderText="Id"
ItemStyle-Width="200px" />
<asp:BoundField
DataField="Address" HeaderText="Address"
ItemStyle-Width="200px" />
<asp:BoundField
DataField="City" HeaderText="City"
ItemStyle-Width="200px" />
</Columns>
</asp:GridView>
</asp:Panel>
</td>
</tr>
</table>
</form>
</body>
</html>
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
dr = objDataTable.NewRow();
dr[0] = i;
dr[1] = "Address" + i.ToString();
dr[2] = "City" + i.ToString();
dr[3] = "Postalcode" + intIndex.ToString();
objDataTable.Rows.Add(dr);
intIndex++;
return objDataTable;
}
GridView1.DataSource = Session["strTemp"] as
DataTable;
GridView1.DataBind();
}
else
{
GridView1.DataSource = GetCustomMadeDataTable();
GridView1.DataBind();
}
}
}
Note-The
GridView in this post don’t support sorting (But could easy be added if
you rebind the data source to the Grid with server side code). You also
need to specify the header manually; it will not get the header from
the data source associated to the GridView. To add a scroller to the
Panel, you can use the Panel control’s ScrollBars property.
border-color: Black;
border-width: 1px;
border-style:solid;
background-color: WhiteSmoke;
color: Black;
text-align: center;
height:25px;
font-size: x-small;
}
.MenuCellSelected
border-top-color:Black;
border-left-color:Black;
border-right-color:Black;
border-bottom-color:Gainsboro;
border-width:1px;
background-color:Gainsboro;
font-weight:bold;
font-size: x-small;
.MenuCellHover
background-color: LightSteelBlue;
font-size: x-small;
.Canvas
background-color: Gainsboro;
font-size: x-small;
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Menu ID="Menu1" Width="300px" runat="server"
Orientation="Horizontal"
StaticEnableDefaultPopOutImage="False"
OnMenuItemClick="Menu1_MenuItemClick">
<StaticMenuItemStyle CssClass="MenuCell"
ItemSpacing="0px" />
<StaticHoverStyle CssClass="MenuCellHover" />
<StaticSelectedStyle
CssClass="MenuCellSelected" ItemSpacing="0px" />
</asp:Menu>
<asp:MultiView ID="MultiView1" runat="server"
ActiveViewIndex="0">
<asp:View ID="View1" runat="server">
<table width="600px" height="400px"
cellpadding="0" cellspacing="0">
<tr>
<td class="Canvas">
<asp:TextBox ID="TextBox2"
runat="server"></asp:TextBox>
</td>
</tr>
</table>
</asp:View>
<asp:View ID="View2" runat="server">
<table width="600px" height="400px"
cellpadding="0" cellspacing="0">
<tr>
<td class="Canvas">
<asp:Button ID="Button2"
runat="server" Text="Button" />
</td>
</tr>
</table>
</asp:View>
<asp:View ID="View3" runat="server">
<table width="600px" height="400px"
cellpadding="0" cellspacing="0">
<tr>
<td class="Canvas">
<asp:DropDownList
ID="DropDownList1" runat="server">
</asp:DropDownList>
</td>
</tr>
</table>
</asp:View>
</asp:MultiView></div>
</form>
</body>
</html>
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
if (!Page.IsPostBack)
{
Menu1.Items.Add(new
MenuItem(MultiView1.Views[index].ID, index.ToString()));
Menu1.Items[0].Selected = true;
}
}
MultiView1.ActiveViewIndex =
Int32.Parse(e.Item.Value);
<script type="text/javascript">
var currentRowId = 0;
function SelectRow()
{
if (event.keyCode == 40)
MarkRow(currentRowId+1);
else if (event.keyCode == 38)
MarkRow(currentRowId-1);
}
function MarkRow(rowId)
{
if (document.getElementById(rowId) == null)
return;
if (document.getElementById(currentRowId) != null
)
document.getElementById(currentRowId).style.
backgroundColor = '#ffffff';
currentRowId = rowId;
document.getElementById(rowId).style.backgroundC
olor = '#ff0000';
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:GridView ID="GridView1" runat="server"
AutoGenerateColumns="true"
OnRowDataBound="GridView1_RowDataBound">
</asp:GridView>
</form>
</body>
</html>
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
}
protected void GridView1_RowDataBound(object sender,
GridViewRowEventArgs e)
{
e.Row.Attributes.Add("id", _i.ToString());
e.Row.Attributes.Add("onKeyDown",
"SelectRow();");
e.Row.Attributes.Add("onClick", "MarkRow(" +
_i.ToString() + ");");
_i++;
}
public DataTable GetCustomMadeDataTable()
{
dr = objDataTable.NewRow();
dr[0] = i;
dr[1] = "Address" + i.ToString();
dr[2] = "City" + i.ToString();
dr[3] = "Postalcode" + intIndex.ToString();
objDataTable.Rows.Add(dr);
intIndex++;
return objDataTable;
}
GridView1.DataSource = Session["strTemp"] as
DataTable;
GridView1.DataBind();
}
else
{
GridView1.DataSource = GetCustomMadeDataTable();
GridView1.DataBind();
}
}
}
M O N D AY, A P R I L 7 , 2 0 0 8
Function to get parent GridView for GridViewRow
A reference to the GridView can be obtained by using
gridViewRow.Parent.Parent or (GridView) gridViewRow.NamingContainer
}
</script>
</head>
<body>
<form id="form1" runat="server" onsubmit="return
Submit();">
<div>
<asp:GridView ID="GridView1" runat="server"
AutoGenerateColumns="False" Height="80px"
Width="224px">
<Columns>
<asp:TemplateField HeaderText="Select
Txt">
<ItemTemplate>
<asp:DropDownList ID="dropselect"
runat="server">
<asp:ListItem>
</asp:ListItem>
<asp:ListItem>aaa</asp:ListI
tem>
<asp:ListItem>bbbb</asp:List
Item>
<asp:ListItem></asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Enter
Text">
<ItemTemplate>
<asp:TextBox ID="txtNameChk"
runat="server">
</asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:TextBox ID="txtName"
runat="server"
Text='<%# DataBinder.Eval(Container.DataItem, "Name")%>'>
</asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Age">
<ItemTemplate>
<asp:Label ID="ibiage"
runat="server"
Text='<%# DataBinder.Eval(Container.DataItem,
"Age")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Button ID="Button1" runat="server"
Text="Submit" /></div>
</form>
</body>
</html>
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
return dt;
F R I D AY, A P R I L 4 , 2 0 0 8
Data-Binding to Methods in ASP.NET(conditionally)
}
.normal-address
{
color:green;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DataList ID="DataList1" runat="server">
<ItemTemplate>
<asp:Panel ID="AddressPanel" runat="server"
CssClass='<%#GetCssForPostalCode(Container.Da
taItem)%>'>
<asp:Label ID="Address" runat="server"
Text='<%# Eval("Address") %>' /><br />
<asp:Label ID="City" runat="server"
Text='<%# Eval("City") %>' /><br />
<asp:Label ID="PostalCode" runat="server"
Text='<%# Eval("Postalcode") %>' />
</asp:Panel>
</ItemTemplate>
</asp:DataList>
</div>
</form>
</body>
</html>
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
BindGrid();
}
protected string GetCssForPostalCode(object dataItem)
{
DataRowView row = dataItem as DataRowView;
if (row["PostalCode"].ToString().EndsWith("902"))
return "highlight-address";
return "normal-address";
DataList1.DataSource = Session["strTemp"] as
DataTable;
DataList1.DataBind();
}
else
{
DataList1.DataSource = GetCustomMadeDataTable();
DataList1.DataBind();
}
}
public DataTable GetCustomMadeDataTable()
{
return objDataTable;
}
<script language='javascript'>
function editCell(C)
{
document.getElementById("txt" + C).style.display="block";
document.getElementById("txt" + C).value =
document.getElementById("span" + C).value;
document.getElementById("span" + C).style.display="none";
}
function updateCell(C)
{
document.getElementById("span" + C).style.display="block";
document.getElementById("span" + C).value =
document.getElementById("txt" + C).value;
document.getElementById("span" + C).innerText=
document.getElementById("span" + C).value;
document.getElementById("txt" + C).style.display="none";
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<table>
<tr>
<td>
<span id='span1' onclick='editCell("1");'
value='This is some value'>This is some value</span>
<input type='text' id='txt1'
onblur='updateCell("1");'
value='' style='display: none;'>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
T H U R S D AY, A P R I L 3 , 2 0 0 8
Bind GridView To Generics
using System;
using System.Collections.Generic;
P OSTE D BY D OT NE T LOVE R AT 12 :4 4 PM 0 C OM M E N TS
var _source;
var _popup;
function showConfirm(source){
this._source = source;
this._popup = $find('mdlPopup');
this._popup.show();
function okClick(){
this._popup.hide();
__doPostBack(this._source.name, '');
function cancelClick(){
this._source = null;
this._popup = null;
</script>
<style>
.modalBackground {
background-color:Gray;
filter:alpha(opacity=70);
opacity:0.7;
.confirm{
background-color:White;
padding:10px;
width:370px;
</style>
</head>
<body>
<form id="form" runat="server" style="font-family:
Trebuchet MS;">
<asp:ScriptManager ID="scriptManager" runat="server"
/>
<div>
<asp:UpdatePanel ID="updatePanel" runat="server"
UpdateMode="Conditional">
<ContentTemplate>
<asp:Label ID="lblTitle" runat="server"
Text="User List"
BackColor="lightblue" Width="95%" />
<asp:GridView ID="gvUsers" runat="server"
AutoGenerateColumns="false" Width="95%">
<AlternatingRowStyle
BackColor="aliceBlue" />
<HeaderStyle HorizontalAlign="Left"
/>
<Columns>
<asp:BoundField DataField="ID"
HeaderText="ID" />
<asp:BoundField
DataField="FirstName"
HeaderText="FirstName" />
<asp:BoundField
DataField="LastName"
HeaderText="LastName" />
<asp:BoundField
DataField="Address" HeaderText="Address" />
<asp:TemplateField ControlStyle-
Width="50px"
HeaderStyle-Width="60px"
ItemStyle-
HorizontalAlign="Center">
<ItemTemplate>
<asp:Button
ID="btnDelete" runat="server"
OnClientClick="showConfi
rm(this); return false;"
OnClick="BtnDelete_C
lick" Text="Delete" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
<ajaxToolkit:ModalPopupExtender
ID="ModalPopupExtender1"
BehaviorID="mdlPopup" runat="server"
TargetControlID="div" PopupControlID="div"
OkControlID="btnOk"
OnOkScript="okClick();"
CancelControlID="btnNo"
OnCancelScript="cancelClick();"
BackgroundCssClass="modalBackground" />
<div id="div" runat="server" align="center"
class="confirm"
style="display: none">
<img align="absmiddle" src="Img/warning.jpg"
/>
Are you sure you want to delete this
item?
<asp:Button ID="btnOk" runat="server"
Text="Yes" Width="50px" />
<asp:Button ID="btnNo" runat="server"
Text="No" Width="50px" />
</div>
</div>
</form>
</body>
</html>
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
if (!this.IsPostBack)
{
this.gvUsers.DataSource = this.Users;
this.gvUsers.DataBind();
}
public class User
{
private int _id;
private string _firstName;
private string _lastName;
private string _Address;
// private bool _isCompleted;
this._id = id;
this._firstName = firstname;
this._lastName = lastname;
this._Address = address;
}
public int ID
{
}
public string Address
{
System.Collections.Generic.List<User> item =
this.Session["Users"] as
System.Collections.Generic.List<User>;
if (item == null)
{
item = new
System.Collections.Generic.List<User>();
this.Session["Users"] = item;
return item;
GridViewRow row =
(GridViewRow)btnDelete.NamingContainer;
this.Users.Remove(itemToRemove);
this.gvUsers.DataSource = this.Users;
this.gvUsers.DataBind();
W E D N E S D AY, A P R I L 2 , 2 0 0 8
ASP.NET AJAX Tabs Control
The Tabs control allows you to create the Tabs very quickly. The tabs
can be changed without causing a postback. This tabs based on the
data from the Categories and Products table in the Northwind
database(i this example i have used dummy data simmilar to
northwind database)
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using AjaxControlToolkit;
using System.Collections.Generic;
List<Category> categories =
Category.GetCategories();
gv.DataSource = category.Products;
gv.DataBind();
tab.Controls.Add(gv);
tab.HeaderText = category.CategoryName;
TabContainer1.Tabs.Add(tab);
}
public class Category
{
}
public static List<Category> GetCategories()
{
List<Category> categories = new List<Category>();
category.CategoryID = i;
category.CategoryName = "Column1Data" +
i.ToString();
categories.Add(category);
category.Products =
GetProducts(category.CategoryID);
return categories;
if (categoryID == 1)
{
for (int i = 1; i <= 20; i++)
{
product.ProductID = i;
product.ProductName = "Product1";
products.Add(product);
}
if (categoryID == 2)
{
for (int i = 1; i <= 20; i++)
{
product.ProductID = i;
product.ProductName = "Product2";
products.Add(product);
}
if (categoryID == 5)
{
for (int i = 1; i <= 20; i++)
{
product.ProductID = i;
product.ProductName = "product5";
products.Add(product);
return products;
}
public class Product
{
public Product()
{
}
}
P OSTE D BY D OT NE T LOVE R AT 12 :1 2 PM 0 C OM M E N TS
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
GridView1.DataSource = Session["strTemp"] as
DataTable;
GridView1.DataBind();
}
else
{
GridView1.DataSource = GetCustomMadeDataTable();
GridView1.DataBind();
}
}
public DataTable GetCustomMadeDataTable()
{
dr = objDataTable.NewRow();
dr[0] = i;
dr[1] = "Column1Data" + i.ToString();
dr[2] = "Column2Data" + i.ToString();
dr[3] = "Column3Data" + i.ToString();
objDataTable.Rows.Add(dr);
}
DataColumn[] dcPk = new DataColumn[1];
dcPk[0] = objDataTable.Columns["Id"];
objDataTable.PrimaryKey = dcPk;
Session["strTemp"] = objDataTable;
return objDataTable;
}
protected void CheckBox1_CheckedChanged(object sender,
EventArgs e)
{
CheckBox checkbox = (CheckBox)sender;
if (checkbox.Checked)
{
ViewState[checkbox.UniqueID] = true;
}
else
{
ViewState.Remove(checkbox.UniqueID);
}
}
}
protected void GridView1_RowEditing(object sender,
GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
BindGrid();
}
protected void GridView1_RowCancelingEdit(object sender,
GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
GridView1.DataSource = GetCustomMadeDataTable();
BindGrid();
T U E S D AY, A P R I L 1 , 2 0 0 8
Showing data from multiple tables effectively using
GridView
<%@ Page Language="C#" AutoEventWireup="true"
CodeFile="HireUsingCodeBhindGrid.aspx.cs"
Inherits="HireUsingCodeBhindGrid" %>
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
}
private DataSet CreateDS()
{
DataSet ds;
if (Session["DataList_ParentChild"] == null)
{
ds = new DataSet();
DataTable dt = new DataTable("Company");
DataRow dr;
dt.Columns.Add(new DataColumn("ID",
typeof(Int32)));
dt.Columns.Add(new DataColumn("CompanyName",
typeof(string)));
dt.Columns.Add(new DataColumn("Address",
typeof(string)));
dt.Columns.Add(new DataColumn("Name",
typeof(string)));
dt.Columns.Add(new DataColumn("Dept",
typeof(string)));
for (int i = 1; i < 10; i++)
{
dr = dt.NewRow();
dr[0] = i;
dr[1] = "Company " + i;
dr[2] = "Address " + i;
dr[3] = "Manager name";
dr[4] = "Adminstration";
dt.Rows.Add(dr);
}
ds.Tables.Add(dt);
DataColumn[] Parent_PKColumns = new
DataColumn[1];
Parent_PKColumns[0] = dt.Columns["ID"];
dt.PrimaryKey = Parent_PKColumns;
dt = new DataTable("Employees");
dt.Columns.Add(new DataColumn("ID",
typeof(Int32)));
dt.Columns.Add(new DataColumn("CompanyID",
typeof(Int32)));
dt.Columns.Add(new DataColumn("Name",
typeof(string)));
dt.Columns.Add(new DataColumn("Dept",
typeof(string)));
for (int i = 1; i < 10; i++)
{
int imax = 0;
if (i % 2 == 0) imax = 5;
else imax = 4;
for (int y = 2; y < imax; y++) //3
emplyees for each company
{
dr = dt.NewRow();
dr[0] = y + i * 5;
dr[1] = i;
dr[2] = "Employee # " + dr[0];
dr[3] = "Dept # " + (y + i);
dt.Rows.Add(dr);
}
}
DataColumn[] Child_PKColumns = new
DataColumn[1];
Child_PKColumns[0] = dt.Columns["ID"];
dt.PrimaryKey = Child_PKColumns;
ds.Tables.Add(dt);
DataColumn[] Child_FKColumns = new
DataColumn[1];
Child_FKColumns[0] = dt.Columns["CompanyID"];
ds.Relations.Add("ParentChild",
Parent_PKColumns, Child_FKColumns);
Session["DataList_ParentChild"] = ds;
}
else
{
ds = (DataSet)Session["DataList_ParentChild"];
}
return ds;
}
protected void GridView1_RowDataBound(object sender,
GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//Just cast the current DataItem to a
DataRowView and then
//use the method CreateChildView to get a view
of the related
//rows
((GridView)e.Row.FindControl("GridView2")).Data
Source
=
((DataRowView)e.Row.DataItem).CreateChildView("ParentChild"
);
((GridView)e.Row.FindControl("GridView2")).Data
Bind();
}
}
}
create a textbox and default all colors to white (or whatever you need
to hide it) create a event for OnSelectionChange on the calendar
control.
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
}
protected void Cal1_SelectionChanged(object sender,
EventArgs e)
{
txtCal.Text = Cal1.SelectedDate.ToShortDateString();
Req1.Validate();
}
}
P OSTE D BY D OT NE T LOVE R AT 12 :2 7 PM 0 C OM M E N TS
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
GridView1.DataSource = personList;
GridView1.DataBind();
}
public class Person
{
private int m_id;
public string _FirstName;
public string _LastName;
public string _Address;
}
public int ID
{
get { return m_id; }
}
public string FullName
{
get { return _FirstName + " " + _LastName; }
}
P OSTE D BY D OT NE T LOVE R AT 12 :1 5 PM 0 C OM M E N TS
passing data from javascript to asp.net cs code on the
server side
<script type="text/javascript">
function Pass()
{
var hidden = document.getElementById("ContentType");
hidden.value = "test value from client side";
var frm = document.getElementById("form1");
frm.submit();
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<input type="hidden" id="ContentType"
name="ContentType" />
<asp:TextBox ID="txt1"
runat="Server"></asp:TextBox>
<asp:Button ID="btnClick" runat="server"
Text="Test"
OnClientClick="Pass();" />
</div>
</form>
</body>
</html>
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
<script type="text/javascript">
function SetStatus(headObj) {
var obj = document.getElementsByTagName("input");
for(var i =0; i < obj.length ; i++) {
if(obj[i].type.toLowerCase() == "checkbox" &&
obj[i].name.toLowerCase() != headObj.name.toLowerCase() )
{
if(headObj.checked)
obj[i].checked = true;
else
obj[i].checked = false;
}
}
return true;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server"
AutoGenerateColumns="false">
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox runat="server"
ID="HeaderLevelCheckBox"
OnClick="SetStatus(this);" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox runat="server"
ID="RowLevelCheckBox" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="CompanyName"
HeaderText="CompanyName" />
<asp:BoundField DataField="Address"
HeaderText="Address" />
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
}
public DataSet c()
{
DataSet ds = new DataSet();
DataTable dt = new DataTable("Company");
DataRow dr;
dt.Columns.Add(new DataColumn("accountNo",
typeof(Int32)));
dt.Columns.Add(new DataColumn("CompanyName",
typeof(string)));
dt.Columns.Add(new DataColumn("Address",
typeof(string)));
for (int i = 0; i <= 10; i++)
{
dr = dt.NewRow();
dr[0] = i;
dr[1] = "Company" + i + Environment.NewLine +
"Title" + i;
dr[2] = "Address" + i + Environment.NewLine +
"Title" + i;
dt.Rows.Add(dr);
}
ds.Tables.Add(dt);
return ds;
}
}
P OSTE D BY D OT NE T LOVE R AT 8 :45 AM 0 C OM M E N TS
March 2008 Home
1h