DataList里套DataGrid,DataBind
發(fā)表時間:2024-06-07 來源:明輝站整理相關(guān)軟件相關(guān)文章人氣:
[摘要]完整的例子:◆MyGrid.ascx:<%@ Control Language="c#" AutoEventWireup="false" Codebehind="MyGrid.ascx.cs" Inherits="XsSt...
完整的例子:
◆MyGrid.ascx:
<%@ Control Language="c#" AutoEventWireup="false" Codebehind="MyGrid.ascx.cs" Inherits="XsStudio.test.MyGrid" TargetSchema="http://schemas.microsoft.com/intellisense/ie5"%>
<asp:DataGrid id="DataGrid1" runat="server" ShowHeader="False" AllowPaging="True" PageSize="3"></asp:DataGrid>
◆MyGrid.ascx.cs
namespace test
{
using System;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
/// <summary>
/// MyGrid 的摘要說明。
/// </summary>
public class MyGrid : System.Web.UI.UserControl
{
protected System.Web.UI.WebControls.DataGrid DataGrid1;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此處放置用戶代碼以初始化頁面
}
public void loaddata(string id)
{
DataTable dt = new DataTable();
dt.Columns.Add("SN");
dt.Columns[0].AutoIncrementSeed=1;
dt.Columns[0].AutoIncrement = true;
dt.Columns.Add("AAA");
dt.Columns.Add("BBB");
dt.Columns.Add("CCC");
dt.Rows.Add(new string[]{"1",id,"435689",System.DateTime.Now.ToString()});
dt.Rows.Add(new string[]{"2",id,"34535",System.DateTime.Now.ToString()});
dt.Rows.Add(new string[]{"3",id,"4456",System.DateTime.Now.ToString()});
dt.Rows.Add(new string[]{"4",id,"64563",System.DateTime.Now.ToString()});
dt.Rows.Add(new string[]{"5",id,"84535",System.DateTime.Now.ToString()});
this.DataGrid1.DataSource = dt;
this.DataGrid1.DataBind();
ViewState[this.ClientID+"_id"] = id;
}
private void DataGrid1_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
{
this.DataGrid1.CurrentPageIndex = e.NewPageIndex;
loaddata(ViewState[this.ClientID+"_id"].ToString());
}
#region Web 窗體設(shè)計器生成的代碼
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 該調(diào)用是 ASP.NET Web 窗體設(shè)計器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 設(shè)計器支持所需的方法 - 不要使用代碼編輯器
/// 修改此方法的內(nèi)容。
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
this.DataGrid1.PageIndexChanged+=new DataGridPageChangedEventHandler(DataGrid1_PageIndexChanged);
}
#endregion
}
}
◆webform1.aspx:
<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="XsStudio.test.WebForm1" validateRequest=false%>
<%@ Register TagPrefix="uc1" TagName="MyGrid" Src="MyGrid.ascx" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm1</title>
<META http-equiv="Content-Type" content="text/html; charset=gb2312">
<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 bgColor="#cccccc" MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<asp:DataList id="DataList1" style="Z-INDEX: 101; LEFT: 8px; POSITION: absolute; TOP: 8px" runat="server">
<ItemTemplate>
<asp:TextBox id="TextBox1" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "ID") %>'></asp:TextBox>
<asp:TextBox id="Textbox2" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "smallPicPath") %>'></asp:TextBox>
<uc1:MyGrid id="MyGrid1" runat="server"></uc1:MyGrid><br/>
</ItemTemplate>
</asp:DataList>
</form>
<script language="javascript">
</script>
</body>
</HTML>
◆webform1.aspx.cs:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using Microsoft.Web.UI.WebControls;
using System.Data.OleDb;
namespace XsStudio.test
{
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.HtmlControls.HtmlInputText Text1;
protected System.Web.UI.WebControls.DataList DataList1;
protected System.Web.UI.WebControls.RadioButtonList RadioButtonList1;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此處放置用戶代碼以初始化頁面
if(!IsPostBack)
{
DataTable tb = new DataTable();
tb.Columns.Add("ID");
tb.Columns.Add("smallPicPath");
tb.Columns.Add("Remark");
tb.Columns.Add("Name");
tb.Columns.Add("SupportCount");
tb.Columns.Add("gdzs");
tb.Columns.Add("BrowserCount");
tb.Rows.Add(new string[]{"001","dwef321","weaf","ewfa","1","12","ewf"});
tb.Rows.Add(new string[]{"002","geawef","gaweg","43ga4","1","21","weg"});
tb.Rows.Add(new string[]{"003","berg","gaweg","43ga4","1","21","weg"});
tb.Rows.Add(new string[]{"004","54wh","gaweg","43ga4","1","21","weg"});
tb.Rows.Add(new string[]{"005","baerg","gaweg","43ga4","1","21","weg"});
DataList1.DataSource = tb;
DataList1.DataBind();
}
}
[page_break] private void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
{
if(e.Item.ItemIndex>-1)
{
string id = ((TextBox)e.Item.FindControl("TextBox1")).Text;
MyGrid tmpGrid = (MyGrid)e.Item.FindControl("MyGrid1");
tmpGrid.loaddata(id);
}
}
#region Web 窗體設(shè)計器生成的代碼
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 該調(diào)用是 ASP.NET Web 窗體設(shè)計器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 設(shè)計器支持所需的方法 - 不要使用代碼編輯器修改
/// 此方法的內(nèi)容。
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
this.DataList1.ItemDataBound+=new DataListItemEventHandler(DataList1_ItemDataBound);
}
#endregion
}
}
這個是我自己搞的:
在aspx文件里,是這樣子套的
在cs文件中有這兩個方法的
private void Page_Load(object sender, System.EventArgs e)
{
// 在此處放置用戶代碼以初始化頁面
if(!IsPostBack)
{
//打開數(shù)據(jù)庫
SqlConnection MyConnection=new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["sqlconnectionstring"]);
MyConnection.Open();
//打開表
string mysql;
mysql="select tid,tna from new_type where iscatalog=0";
SqlCommand MyCommand=new SqlCommand(mysql,MyConnection);
SqlDataReader dr=MyCommand.ExecuteReader();
//數(shù)據(jù)綁定
DataList1.DataSource=dr;
DataList1.DataBind();
dr.Close();
MyConnection.Close();
}
}
private void DataList1_ItemDataBound(object sender, System.Web.UI.WebControls.DataListItemEventArgs e)
{
//取tid
string tid;
//tid=e.Item.DataItem.GetType().ToString();
tid=((DbDataRecord)e.Item.DataItem).GetValue(0).ToString();
//Label mylabel=(Label)e.Item.FindControl("Label1");
//mylabel.Text=tid;
//綁定數(shù)據(jù)
DataGrid mygrid=(DataGrid)e.Item.FindControl("DataGrid1");
SqlConnection MyConnection=new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["sqlconnectionstring"]);
MyConnection.Open();
string mysql;
mysql="select top 10 cid,title from new_content where tid=" + tid;
SqlCommand MyCommand=new SqlCommand(mysql,MyConnection);
SqlDataReader dr=MyCommand.ExecuteReader();
mygrid.DataSource=dr;
mygrid.DataBind();
dr.Close();
MyConnection.Close();
}
補充一下下,要這個的:using System.Data.Common;
在這里我發(fā)現(xiàn)的是DataGrid1.Item.DataItem的類型是和一開始時綁定它的方法有關(guān)的,我用了SqlDataReader來綁定數(shù)據(jù),DataGrid1.Item.DataItem的類型就是System.Data.Common.DbDataRecord。我換了用DataTable來綁定時,就和msdn里的一樣了,DataGrid1.Item.DataItem的類型是DbDataRecord
DataGrid的數(shù)據(jù)綁定可以用好多方法啊,嘛干會這樣子的呢?我想沒透的就是嘛干會用沒同的方法綁定數(shù)據(jù),就會有沒同的類型呢?
DataGrid的數(shù)據(jù)綁定可以用好多方法啊,為什么會這樣呢?我想不通為什么用不同的方法綁定數(shù)據(jù)會有不同的類型呢?