Mi fai fare lavoro doppio!!!![]()
![]()
La pagina aspx è identica,mentre il codebehind
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 TestVariCSharp
{
/// <summary>
/// Summary description for WebForm7.
/// </summary>
public class WebForm7 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataList DataList1;
DataTable dtProdotti = new DataTable("Products");
#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.ItemDataBound += new System.Web.UI.WebControls.DataListItemEventHandler (this.DataList1_ItemDataBound);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void Page_Load(object sender, System.EventArgs e){
if (!Page.IsPostBack){
GetProdotti();
bindDataList();
}
}
private void bindDataList(){
SqlConnection cnn = new SqlConnection("Persist Security Info=True;User ID=sa;Initial Catalog=Northwind;Data Source=localhost");
SqlCommand cmd = new SqlCommand("SELECT * FROM Categories",cnn);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable("Categories");
cnn.Open();
da.Fill(dt);
cnn.Close();
this.DataList1.DataSource = dt;
this.DataList1.DataBind();
}
private void GetProdotti(){
SqlConnection cnn = new SqlConnection("Persist Security Info=True;User ID=sa;Initial Catalog=Northwind;Data Source=localhost");
SqlCommand cmd = new SqlCommand("SELECT * FROM Products",cnn);
SqlDataAdapter da = new SqlDataAdapter(cmd);
cnn.Open();
da.Fill(dtProdotti);
cnn.Close();
}
private void bindDataList2(string i,ref DataList dl ){
DataView dv = new DataView();
dv = dtProdotti.DefaultView;
dv.RowFilter = "CategoryID = " + i;
dl.DataSource = dv;
dl.DataBind();
}
private void DataList1_ItemDataBound(object sender, System.Web.UI.WebControls.DataListItemEventArgs e){
DataRowView d = null;
d = (DataRowView)e.Item.DataItem;
DataList d2 = new DataList();
d2 = (DataList)e.Item.FindControl("DataList2");
bindDataList2(d[0].ToString(),ref d2);
}
}
}
Fammi sapere!
Ciao
Kalman

Rispondi quotando