V
vcinquini
I already did this in the past succesfully. I can't understand why it
isn' t working now. Anyone can see anything wrong?
The ASPX page
<%@ Page language="c#" Codebehind="WebForm1.aspx.cs"
AutoEventWireup="false" Inherits="datagrid.WebForm1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm1</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="C#" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5"
name="vs_targetSchema">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<asp:TextBox id="TextBox1" style="Z-INDEX: 101; LEFT: 32px;
POSITION: absolute; TOP: 32px" runat="server"
Width="248px"></asp:TextBox>
<asp:Button id="Button1" style="Z-INDEX: 102; LEFT: 296px; POSITION:
absolute; TOP: 32px" runat="server"
Text="Button"></asp:Button>
<aspataGrid id="DataGrid1" style="Z-INDEX: 103; LEFT: 32px;
POSITION: absolute; TOP: 72px" runat="server"
AllowPaging="True"
AutoGenerateColumns="False"></aspataGrid></form>
</body>
</HTML>
The code behind
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace datagrid
{
/// <summary>
/// Summary description for WebForm1.
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.TextBox TextBox1;
protected System.Web.UI.WebControls.DataGrid DataGrid1;
protected System.Web.UI.WebControls.Button Button1;
private void Page_Load(object sender, System.EventArgs e)
{
GetSource();
if(!IsPostBack)
{
CreateColumns();
DataGrid1.CurrentPageIndex = 0;
}
DataGrid1.DataBind();
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.DataGrid1.PageIndexChanged += new
System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.DataGrid1_PageIndexChanged);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void CreateColumns()
{
DataGrid1.AutoGenerateColumns = false;
DataGrid1.PageSize = 10;
HyperLinkColumn hl = new HyperLinkColumn();
hl.HeaderText = "Customer ID";
hl.DataTextField = "CustomerId";
hl.DataNavigateUrlField = "CustomerId";
hl.DataNavigateUrlFormatString = "Detail.aspx&cid={0}";
DataGrid1.Columns.Add(hl);
BoundColumn b = new BoundColumn();
b.DataField = "CompanyName";
b.HeaderText = "Company Name";
DataGrid1.Columns.Add(b);
b = new BoundColumn();
b.DataField = "ContactName";
b.HeaderText = "Contact Name";
DataGrid1.Columns.Add(b);
b = new BoundColumn();
b.DataField = "Address";
b.HeaderText = "Address";
DataGrid1.Columns.Add(b);
b = new BoundColumn();
b.DataField = "City";
b.HeaderText = "City";
DataGrid1.Columns.Add(b);
b = new BoundColumn();
b.DataField = "Region";
b.HeaderText = "Region";
DataGrid1.Columns.Add(b);
}
private void GetSource()
{
string sql;
SqlDataAdapter da;
DataSet ds = new DataSet();
SqlConnection cn = new SqlConnection("User ID=sa;Initial
Catalog=Northwind;Data Source=IDEAFIX;Password=;");
cn.Open();
sql = "select * from customers where country like '" +
TextBox1.Text.Trim() +"%'";
da = new SqlDataAdapter();
da.SelectCommand = new SqlCommand(sql, cn);
da.Fill(ds);
DataGrid1.DataSource = ds.Tables[0];
}
private void Button1_Click(object sender, System.EventArgs e)
{
GetSource();
}
protected void DataGrid1_PageIndexChanged(object source,
System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
DataGrid1.CurrentPageIndex = e.NewPageIndex;
DataGrid1.DataBind();
}
private bool DynamicColumnAdded
{
get
{
object b = ViewState["DynamicColumnAdded"];
return (b == null) ? false : true;
}
set
{
ViewState["DynamicColumnAdded"] = value;
}
}
protected override void LoadViewState(object savedState)
{
base.LoadViewState(savedState);
if (DynamicColumnAdded)
{
this.CreateColumns();
}
}
}
}
Thanks in advance
isn' t working now. Anyone can see anything wrong?
The ASPX page
<%@ Page language="c#" Codebehind="WebForm1.aspx.cs"
AutoEventWireup="false" Inherits="datagrid.WebForm1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm1</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="C#" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5"
name="vs_targetSchema">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<asp:TextBox id="TextBox1" style="Z-INDEX: 101; LEFT: 32px;
POSITION: absolute; TOP: 32px" runat="server"
Width="248px"></asp:TextBox>
<asp:Button id="Button1" style="Z-INDEX: 102; LEFT: 296px; POSITION:
absolute; TOP: 32px" runat="server"
Text="Button"></asp:Button>
<aspataGrid id="DataGrid1" style="Z-INDEX: 103; LEFT: 32px;
POSITION: absolute; TOP: 72px" runat="server"
AllowPaging="True"
AutoGenerateColumns="False"></aspataGrid></form>
</body>
</HTML>
The code behind
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace datagrid
{
/// <summary>
/// Summary description for WebForm1.
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.TextBox TextBox1;
protected System.Web.UI.WebControls.DataGrid DataGrid1;
protected System.Web.UI.WebControls.Button Button1;
private void Page_Load(object sender, System.EventArgs e)
{
GetSource();
if(!IsPostBack)
{
CreateColumns();
DataGrid1.CurrentPageIndex = 0;
}
DataGrid1.DataBind();
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.DataGrid1.PageIndexChanged += new
System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.DataGrid1_PageIndexChanged);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void CreateColumns()
{
DataGrid1.AutoGenerateColumns = false;
DataGrid1.PageSize = 10;
HyperLinkColumn hl = new HyperLinkColumn();
hl.HeaderText = "Customer ID";
hl.DataTextField = "CustomerId";
hl.DataNavigateUrlField = "CustomerId";
hl.DataNavigateUrlFormatString = "Detail.aspx&cid={0}";
DataGrid1.Columns.Add(hl);
BoundColumn b = new BoundColumn();
b.DataField = "CompanyName";
b.HeaderText = "Company Name";
DataGrid1.Columns.Add(b);
b = new BoundColumn();
b.DataField = "ContactName";
b.HeaderText = "Contact Name";
DataGrid1.Columns.Add(b);
b = new BoundColumn();
b.DataField = "Address";
b.HeaderText = "Address";
DataGrid1.Columns.Add(b);
b = new BoundColumn();
b.DataField = "City";
b.HeaderText = "City";
DataGrid1.Columns.Add(b);
b = new BoundColumn();
b.DataField = "Region";
b.HeaderText = "Region";
DataGrid1.Columns.Add(b);
}
private void GetSource()
{
string sql;
SqlDataAdapter da;
DataSet ds = new DataSet();
SqlConnection cn = new SqlConnection("User ID=sa;Initial
Catalog=Northwind;Data Source=IDEAFIX;Password=;");
cn.Open();
sql = "select * from customers where country like '" +
TextBox1.Text.Trim() +"%'";
da = new SqlDataAdapter();
da.SelectCommand = new SqlCommand(sql, cn);
da.Fill(ds);
DataGrid1.DataSource = ds.Tables[0];
}
private void Button1_Click(object sender, System.EventArgs e)
{
GetSource();
}
protected void DataGrid1_PageIndexChanged(object source,
System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
DataGrid1.CurrentPageIndex = e.NewPageIndex;
DataGrid1.DataBind();
}
private bool DynamicColumnAdded
{
get
{
object b = ViewState["DynamicColumnAdded"];
return (b == null) ? false : true;
}
set
{
ViewState["DynamicColumnAdded"] = value;
}
}
protected override void LoadViewState(object savedState)
{
base.LoadViewState(savedState);
if (DynamicColumnAdded)
{
this.CreateColumns();
}
}
}
}
Thanks in advance