|
code
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Trouble with DataList Controlof them to different datasets. Is this possible? I am currently using the following syntax to bind my first dataset. private void Page_Load(object sender, System.EventArgs e) { if (!Page.IsPostBack) { string sql = "select * from table1 where id = 1"; conn.Open(); DataSet ds = new DataSet(); SqlDataAdapter da = new SqlDataAdapter(sql, conn); da.Fill(ds, "data1"); DataList1.DataSource = ds; DataList1.DataMember = "data1"; DataList1.DataBind(); } } <asp:datalist id="DataList1", runat="server" > <itemtemplate> <table> <tr> <td><asp:Label id="Label1" runat="server" text='<%# DataBinder.Eval(Container.DataItem, "Name") %>'>Label</asp:Label></td> <td><asp:Label id="Label2" runat="server" text='<%# DataBinder.Eval(Container.DataItem, "Value") %>'>Label</asp:Label></td> </tr> </table> </itemtemplate> </asp:datalist> I would like to do the following.... private void Page_Load(object sender, System.EventArgs e) { if (!Page.IsPostBack) { string sql = "select Name, Value from table1 where id = 1"; conn.Open(); DataSet ds1 = new DataSet(); SqlDataAdapter da1 = new SqlDataAdapter(sql, conn); da1.Fill(ds1, "data1"); DataList1.DataSource = ds1; DataList1.DataMember = "data1"; DataList1.DataBind(); sql = "select City, State from table2 where id = 1"; DataSet ds2 = new DataSet(); SqlDataAdapter da2 = new SqlDataAdapter(sql, conn); da2.Fill(ds2, "data2"); DataList2.DataSource = ds2; DataList2.DataMember = "data2"; DataList2.DataBind(); } } <asp:datalist id="DataList1", runat="server" > <itemtemplate> <table> <tr> <td><asp:Label id="Label1" runat="server" text='<%# DataBinder.Eval(Container.DataItem, "Name") %>'>Label</asp:Label></td> <td><asp:Label id="Label2" runat="server" text='<%# DataBinder.Eval(Container.DataItem, "Value") %>'>Label</asp:Label></td> </tr> </table> </itemtemplate> </asp:datalist> <asp:datalist id="DataList2", runat="server" > <itemtemplate> <table> <tr> <td><asp:Label id="Label3" runat="server" text='<%# DataBinder.Eval(Container.DataItem, "City") %>'>Label</asp:Label></td> <td><asp:Label id="Label4" runat="server" text='<%# DataBinder.Eval(Container.DataItem, "State") %>'>Label</asp:Label></td> </tr> </table> </itemtemplate> </asp:datalist> The second datalist is always empty. Step through the code using the debugger; set a break point at this step:
DataList2.DataSource = ds2; and lookup if ds2 is not nothing and if so lookup the rows.count in the DataTable. Show quoteHide quote "Robert Sheppard" wrote: > I have multiple DataList controls on the same page and I want to bind each > of them to different datasets. > > Is this possible? > > I am currently using the following syntax to bind my first > dataset. > > private void Page_Load(object sender, System.EventArgs e) > { > if (!Page.IsPostBack) > { > string sql = "select * from table1 where id = 1"; > conn.Open(); > DataSet ds = new DataSet(); > SqlDataAdapter da = new SqlDataAdapter(sql, conn); > da.Fill(ds, "data1"); > DataList1.DataSource = ds; > DataList1.DataMember = "data1"; > DataList1.DataBind(); > } > } > > <asp:datalist id="DataList1", runat="server" > > <itemtemplate> > <table> > <tr> > <td><asp:Label id="Label1" runat="server" text='<%# > DataBinder.Eval(Container.DataItem, "Name") %>'>Label</asp:Label></td> > <td><asp:Label id="Label2" runat="server" text='<%# > DataBinder.Eval(Container.DataItem, "Value") %>'>Label</asp:Label></td> > </tr> > </table> > </itemtemplate> > </asp:datalist> > > > > I would like to do the following.... > > > private void Page_Load(object sender, System.EventArgs e) > { > if (!Page.IsPostBack) > { > string sql = "select Name, Value from table1 where id = 1"; > conn.Open(); > DataSet ds1 = new DataSet(); > SqlDataAdapter da1 = new SqlDataAdapter(sql, conn); > da1.Fill(ds1, "data1"); > DataList1.DataSource = ds1; > DataList1.DataMember = "data1"; > DataList1.DataBind(); > > sql = "select City, State from table2 where id = 1"; > DataSet ds2 = new DataSet(); > SqlDataAdapter da2 = new SqlDataAdapter(sql, conn); > da2.Fill(ds2, "data2"); > DataList2.DataSource = ds2; > DataList2.DataMember = "data2"; > DataList2.DataBind(); > } > } > > <asp:datalist id="DataList1", runat="server" > > <itemtemplate> > <table> > <tr> > <td><asp:Label id="Label1" runat="server" text='<%# > DataBinder.Eval(Container.DataItem, "Name") %>'>Label</asp:Label></td> > <td><asp:Label id="Label2" runat="server" text='<%# > DataBinder.Eval(Container.DataItem, "Value") %>'>Label</asp:Label></td> > </tr> > </table> > </itemtemplate> > </asp:datalist> > > <asp:datalist id="DataList2", runat="server" > > <itemtemplate> > <table> > <tr> > <td><asp:Label id="Label3" runat="server" text='<%# > DataBinder.Eval(Container.DataItem, "City") %>'>Label</asp:Label></td> > <td><asp:Label id="Label4" runat="server" text='<%# > DataBinder.Eval(Container.DataItem, "State") %>'>Label</asp:Label></td> > </tr> > </table> > </itemtemplate> > </asp:datalist> > > > The second datalist is always empty. > > >
Other interesting topics
ASP.NET 2.0 Easier than ASP? Gimmie a Break!
checking checkboxes per javascript in asp.net 2.0 treeview TabStrip - create tabs dynamicly on client-side Trouble with the DataList Control Compare Validator for Date AND Time hyperlink in a Calendar Control ASP.Net 2.0 Table not expanding 100% .NET 2.0 ImageButton control not submitting like form button ReportViewer Question Can I read the relevant CSS class? |
|||||||||||||||||||||||