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 System.Data.SqlClient;namespace DataList2 { /// <summary> /// Summary description for WebForm1. /// </summary> public class WebForm1 : System.Web.UI.Page { private static string Conn="Data Source=GPTEACHER;database=Northwind;uid=sa;pwd=sa"; protected System.Web.UI.WebControls.DataList DataList1;
private void Page_Load(object sender, System.EventArgs e) { // Put user code to initialize the page here } #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.DataList1.ItemCommand += new System.Web.UI.WebControls.DataListCommandEventHandler(this.DataList1_ItemCommand); this.Load += new System.EventHandler(this.Page_Load); } #endregion private void DataList1_ItemCommand(object source, System.Web.UI.WebControls.DataListCommandEventArgs e) { if(e.CommandName=="select") { DataList1.SelectedIndex=e.Item.ItemIndex; DataList.DataBind(); } } /*private void myDataBind() { SqlConnection sqlCon=new SqlConnection(Conn); SqlDataAdapter da=new SqlDataAdapter("select CompanyName,Address,Phone,Fax from Customers",sqlCon); DataSet ds=new DataSet(); try { da.Fill(ds,"good"); DataList1.DataSource=ds.Tables["good"]; DataList1.DataBind(); } catch(Exception error) { Response.Write(error.ToString()); } } */ 这个我自己写的连接数据库的function,可是没有用到,原书上也没有,为什么不用它 } } 你来顶真让小虾米我感动阿 谢
myDataBind得用,不然肯定没数据 在Page_Load里面写 if (!IsPostBack) { myDataBind(); } 试试
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 System.Data.SqlClient;namespace DataList2
{
/// <summary>
/// Summary description for WebForm1.
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
private static string Conn="Data Source=GPTEACHER;database=Northwind;uid=sa;pwd=sa";
protected System.Web.UI.WebControls.DataList DataList1;
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
} #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.DataList1.ItemCommand += new System.Web.UI.WebControls.DataListCommandEventHandler(this.DataList1_ItemCommand);
this.Load += new System.EventHandler(this.Page_Load); }
#endregion private void DataList1_ItemCommand(object source, System.Web.UI.WebControls.DataListCommandEventArgs e)
{
if(e.CommandName=="select")
{
DataList1.SelectedIndex=e.Item.ItemIndex;
DataList.DataBind();
}
}
/*private void myDataBind()
{
SqlConnection sqlCon=new SqlConnection(Conn);
SqlDataAdapter da=new SqlDataAdapter("select CompanyName,Address,Phone,Fax from Customers",sqlCon);
DataSet ds=new DataSet();
try
{
da.Fill(ds,"good");
DataList1.DataSource=ds.Tables["good"];
DataList1.DataBind();
}
catch(Exception error)
{
Response.Write(error.ToString());
}
}
*/
这个我自己写的连接数据库的function,可是没有用到,原书上也没有,为什么不用它
}
}
你来顶真让小虾米我感动阿
谢
在Page_Load里面写
if (!IsPostBack)
{
myDataBind();
}
试试
该实验的目标是点击button后就出现一些数据在button的下方,现在怎么没有这种效果〉?
改成myDataBind()
点击按钮后,选择项出现在button 下方?
private void DataList1_ItemCommand(object source, System.Web.UI.WebControls.DataListCommandEventArgs e)
{
if(e.CommandName=="select")
{
DataList1.SelectedIndex=e.Item.ItemIndex;
DataList.DataBind();
这需要对按钮下方(你的代码没太看明白)操作
}
}
实现了单击“选择"按钮,显示该行的详细信息(书上的话)我已经在DataList的selectedItemTemplate中绑定了3个子段,可是为什么点“选择“按钮后没有出现它们?
最后一个疑问:为什么在
private void DataList1_ItemCommand(object source, System.Web.UI.WebControls.DataListCommandEventArgs e)
中绑定了数据源,而在page_load中还要绑定一次数据源呢?这个疑问去掉后马上将两帖给你结帐
谢谢你的帮助 fancyf(凡瑞)大哥。
今天不光调好了一段代码,还学到了程序外的东西:耐心,对人友好,帮助他人。 谢你!
page_load()中的绑定用于显示第一次调用该页时显示的内容:button,公司名称
而DataList1_ItemCommand中的绑定用于显示当点击button时子项的内容,也就是selectItemTemplate中绑定的字段。