|
code
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Trouble with the 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. Call conn.Close() after the Fill() method. When you are using the Fill()
method to obtain the records, it is not necessary to call the Open() method. If you would like to see a working example that I used, let me know. Good Luck! Show quoteHide quote "Robert Sheppard" <shep***@cox.net> wrote in message news:%23p%23AtXJOGHA.1028@TK2MSFTNGP11.phx.gbl... >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. > >
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 Compare Validator for Date AND Time asp wizard 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? |
|||||||||||||||||||||||