I have a DataList control on my webpage, the rows of which are populated by ItemTemplate. Each row has a check box, and on button click, I am generating as a string which is actually a `sql` query text.
Each result returned by the query from the DB has 10 rows which are to be shown in another DataList, as something like this:
The problem is that I do not know how many checkboxes will be selected by the user, and according the number of results returned will be unknown.
So, if the user selects two checkboxes from the first DataList, the result DataList should have two columns:
I tried to think of a solution for this. Let's say a user selects two check boxes:
foreach checkbox selected by the user
Make a new repeater. Add a new header.
Fetch the data from the database (Each result contains 10 columns) and bind to the repeater.
Add the repeater to a container on the web page.
Now, I remember that in windows forms, such addition of runtime generated controls can be done by something like:
TableLayout.Add(childElements)
But, I am not able to think of something similar in Web Forms. Is this actually the correct approach? How can this kind of dynamic building of DataControl achieved in this case?
Do you want it to render as a table with a varying number of columns? Or is it ok to have separate tables (per selection) sitting side-by-side?
Superguppie.
Please remember to click “Mark as Answer” on the post that helps you. This can be beneficial to other community members reading the thread.
When all you've got is a Hammer, Every Problem looks like a Nail. Michael Swain.
If I would have generated a data control on the fly at runtime, I would have the following fixed format for each datalist (suppose if it would have been possible to generate datalist)
Or in some other case, can I keep a grid on the web page, and keep on adding data to in columns in some kind of above format? Is that possible? How can that be done for this case?
To have all data in a single table (which will make the lines line up nicely) you would have to do a lot of moving data around. If you are ok with the lines being misalligned, as the image suggests, it gets much easier.
In that case, I think I would use a ListView, with a ListView nested in the ItemTemplate. I would collect the results in separate collections and make a collection holding these. The outer ListView would bind to that big collection. The inner ListView would
then bind to a single collection of results.
The inner ListView would build a table in the format you gave. The outer ListView would either build a table with a single row, and the inner ListViews in a td, or make a div with each inner ListView in another div, making the inner divs floating.
Do I still make sense? Can you work with any of this?
Superguppie.
Please remember to click “Mark as Answer” on the post that helps you. This can be beneficial to other community members reading the thread.
When all you've got is a Hammer, Every Problem looks like a Nail. Michael Swain.
The problem is that there is not data coming into any of the grids. I tried putting break-points on the GenerateGrid function, and it does hit there and runs successfully but the Outer DataList is not getting binded. It does not also fall into the OuterDataBound event.
Also, I checked the SQL stored procedure, and executed it manually and passed the following same parameter values, and it gave a result of tow rows something like this successfully:
First you loop the DataList1 and generate sql query text for each checked item in it. Then for each checked loop you can return into a DataSet. Then bind it to a dynamic DataList and add it to page dyanmically.
harpreet31
Member
39 Points
101 Posts
Generate data controls according to user inputs
Feb 29, 2012 04:51 PM|LINK
I have a DataList control on my webpage, the rows of which are populated by ItemTemplate. Each row has a check box, and on button click, I am generating as a string which is actually a `sql` query text.
Each result returned by the query from the DB has 10 rows which are to be shown in another DataList, as something like this:
<Header>
Item1: Result1
Item2: Result2
Item3: Result3
...
Item10: Result10
The problem is that I do not know how many checkboxes will be selected by the user, and according the number of results returned will be unknown.
So, if the user selects two checkboxes from the first DataList, the result DataList should have two columns:
<Header1> <Header2>
Item1: Result1 | Item 1: ResultCol2
Item2: Result2 | Item 2: ResultCol2
Item3: Result3 | Item 3:ResultCol2
...
Item10: Result10 | Item 10: ResultCol2
I tried to think of a solution for this. Let's say a user selects two check boxes:
foreach checkbox selected by the user Make a new repeater. Add a new header. Fetch the data from the database (Each result contains 10 columns) and bind to the repeater. Add the repeater to a container on the web page.Now, I remember that in windows forms, such addition of runtime generated controls can be done by something like:
TableLayout.Add(childElements)
But, I am not able to think of something similar in Web Forms. Is this actually the correct approach? How can this kind of dynamic building of DataControl achieved in this case?
Harpreet
mameenkhn
Contributor
2026 Points
391 Posts
Re: Generate data controls according to user inputs
Feb 29, 2012 05:27 PM|LINK
Following link may help you
http://stackoverflow.com/questions/717539/dynamic-repeater-in-c-net
--------------------------------------------------
Muhammad Amin
محمد امين
harpreet31
Member
39 Points
101 Posts
Re: Generate data controls according to user inputs
Feb 29, 2012 05:58 PM|LINK
Not looking like the same problem there. :-/
Harpreet
mameenkhn
Contributor
2026 Points
391 Posts
Re: Generate data controls according to user inputs
Feb 29, 2012 07:00 PM|LINK
You can easily achieve this by using JQuery Ajax, web method....
When the user click on the checkbox send the id to the web method (or webservice) through JQuery Ajax....
which returns the data...... which you can format using JQuery selecters
--------------------------------------------------
Muhammad Amin
محمد امين
superguppie
All-Star
48225 Points
8679 Posts
Re: Generate data controls according to user inputs
Mar 01, 2012 12:35 PM|LINK
Do you want it to render as a table with a varying number of columns? Or is it ok to have separate tables (per selection) sitting side-by-side?
Please remember to click “Mark as Answer” on the post that helps you. This can be beneficial to other community members reading the thread.
When all you've got is a Hammer, Every Problem looks like a Nail. Michael Swain.
harpreet31
Member
39 Points
101 Posts
Re: Generate data controls according to user inputs
Mar 01, 2012 02:13 PM|LINK
If I would have generated a data control on the fly at runtime, I would have the following fixed format for each datalist (suppose if it would have been possible to generate datalist)
<HeaderTemplate>
<tr>Table Heading<tr>
<tr> <th>ItemNo</th> <th>Item Detail</th><tr>
</HeaderTemplate>
<ItemTemplate>
<tr> <td><%#CallFunc()%></td> <td><%#Eval("Item1")%></td> </tr>
<tr> <td><%#CallFunc()%></td> <td><%#Eval("Item2")%></td> </tr>
<tr> <td><%#CallFunc()%></td> <td><%#Eval("Item3")%></td> </tr>
<tr> <td><%#CallFunc()%></td> <td><%#Eval("Item4")%></td> </tr>
<tr> <td><%#CallFunc()%></td> <td><%#Eval("Item5")%></td> </tr>
<tr> <td><%#CallFunc()%></td> <td><%#Eval("Item6")%></td> </tr>
<tr> <td><%#CallFunc()%></td> <td><%#Eval("Item7")%></td> </tr>
<tr> <td><%#CallFunc()%></td> <td><%#Eval("Item8")%></td> </tr>
<tr> <td><%#CallFunc()%></td> <td><%#Eval("Item9")%></td> </tr>
<tr> <td><%#CallFunc()%></td> <td><%#Eval("Item10")%></td> </tr>
<tr> <td><%#CallFunc()%></td> <td><%#Eval("Item11")%></td> </tr>
</ItemTemplate>
I have the results of queries from DB, which I have to populate in whatever element can be generated in the above form.
It might be more clear by this lame image I made: i.imgur.com/t0elz.png
Harpreet
harpreet31
Member
39 Points
101 Posts
Re: Generate data controls according to user inputs
Mar 01, 2012 02:35 PM|LINK
Or in some other case, can I keep a grid on the web page, and keep on adding data to in columns in some kind of above format? Is that possible? How can that be done for this case?
Harpreet
superguppie
All-Star
48225 Points
8679 Posts
Re: Generate data controls according to user inputs
Mar 01, 2012 02:56 PM|LINK
To have all data in a single table (which will make the lines line up nicely) you would have to do a lot of moving data around. If you are ok with the lines being misalligned, as the image suggests, it gets much easier.
In that case, I think I would use a ListView, with a ListView nested in the ItemTemplate. I would collect the results in separate collections and make a collection holding these. The outer ListView would bind to that big collection. The inner ListView would then bind to a single collection of results.
The inner ListView would build a table in the format you gave. The outer ListView would either build a table with a single row, and the inner ListViews in a td, or make a div with each inner ListView in another div, making the inner divs floating.
Do I still make sense? Can you work with any of this?
Please remember to click “Mark as Answer” on the post that helps you. This can be beneficial to other community members reading the thread.
When all you've got is a Hammer, Every Problem looks like a Nail. Michael Swain.
harpreet31
Member
39 Points
101 Posts
Re: Generate data controls according to user inputs
Mar 01, 2012 07:31 PM|LINK
Hi superguppie,
I tried to do something like the following for my solution.
Adding an outerdata list and an inner datalist. Everytime the outer datalist should get bound, the inner data list is generated.
Here's the code which is giving me a very high time, since there's no data coming into the data lists.
.ASPX code:
<asp:DataList ID="outerDat" runat="server" OnItemDataBound="outerDat_ItemDataBound" RepeatColumns="2" RepeatDirection="Horizontal"> <ItemTemplate> <%--<tr><td> <strong><%#Eval("QuotedVersions") %></strong> </td></tr>--%> <asp:Label ID="lblFirst" runat="server" Text='<%# Eval("QuotedVersions")%>'></asp:Label> <asp:DataList ID="innerDat" runat="server" > <HeaderTemplate> <tr> <th>URL No.</th> <th>URL</th> <th>AVG. RJ value</th><tr> </HeaderTemplate> <ItemTemplate> <tr> <td>URL<%#CallFunc()%></td> <td><asp:LinkButton ID="urlLK1" runat="server" Text='<%#Eval("URL1")%>' /></td> <td> <%#Eval("AvgRJ1")%></td> </tr> <tr> <td>URL<%#CallFunc()%></td> <td><asp:LinkButton ID="urlLK2" runat="server" Text='<%#Eval("URL2")%>' /></td> <td> <%#Eval("AvgRJ2")%></td> </tr> <tr> <td>URL<%#CallFunc()%></td> <td><asp:LinkButton ID="urlLK3" runat="server" Text='<%#Eval("URL3")%>' /></td> <td> <%#Eval("AvgRJ3")%></td> </tr> <tr> <td>URL<%#CallFunc()%></td> <td><asp:LinkButton ID="urlLK4" runat="server" Text='<%#Eval("URL4")%>' /></td> <td> <%#Eval("AvgRJ4")%></td> </tr> <tr> <td>URL<%#CallFunc()%></td> <td><asp:LinkButton ID="urlLK5" runat="server" Text='<%#Eval("URL5")%>' /></td> <td> <%#Eval("AvgRJ5")%></td> </tr> <tr> <td>URL<%#CallFunc()%></td> <td><asp:LinkButton ID="urlLK6" runat="server" Text='<%#Eval("URL6")%>' /></td> <td> <%#Eval("AvgRJ6")%></td> </tr> <tr> <td>URL<%#CallFunc()%></td> <td><asp:LinkButton ID="urlLK7" runat="server" Text='<%#Eval("URL7")%>' /></td> <td> <%#Eval("AvgRJ7")%></td> </tr> <tr> <td>URL<%#CallFunc()%></td> <td><asp:LinkButton ID="urlLK8" runat="server" Text='<%#Eval("URL8")%>' /></td> <td> <%#Eval("AvgRJ8")%></td> </tr> <tr> <td>URL<%#CallFunc()%></td> <td><asp:LinkButton ID="urlLK9" runat="server" Text='<%#Eval("URL9")%>' /></td> <td> <%#Eval("AvgRJ9")%></td> </tr> <tr> <td>URL<%#CallFunc()%></td> <td><asp:LinkButton ID="urlLK10" runat="server" Text='<%#Eval("URL10")%>' /></td> <td> <%#Eval("AvgRJ10")%></td> </tr> <tr> <td>nDCG: </td> <td> </td> <td><%#Eval("nDCG")%></td> </tr> </ItemTemplate> </asp:DataList> </ItemTemplate> </asp:DataList>.ASPX.CS code:
Button_Click { string strat = string.Join("','", strategies.ToArray()); fullquotedv = string.Format("'{0}'", strat); string quot = string.Join("','", queries.ToArray()); quotedvstrat = string.Format("'{0}'", quot); GenerateGrids(); } public void GenerateGrids() { icr = 1; SqlDataAdapter urlfetch = new SqlDataAdapter("fetchfullchurls", ConfigurationManager.ConnectionStrings["cn"].ConnectionString); DataSet urldata = new DataSet(); urlfetch.SelectCommand.CommandType = CommandType.StoredProcedure; urlfetch.SelectCommand.Parameters.Add("@quotedv", SqlDbType.NVarChar, 255).Value = fullquotedv; urlfetch.SelectCommand.Parameters.Add("@strg", SqlDbType.NVarChar, 255).Value = quotedvstrat; urlfetch.Fill(urldata); outerDat.DataSource = urldata; outerDat.DataBind(); } protected void outerDat_ItemDataBound(object sender, DataListItemEventArgs e) { SqlDataAdapter urlfetch = new SqlDataAdapter("fetchfullchurls", ConfigurationManager.ConnectionStrings["cn"].ConnectionString); DataSet urldata = new DataSet(); urlfetch.SelectCommand.CommandType = CommandType.StoredProcedure; urlfetch.SelectCommand.Parameters.Add("@quotedv", SqlDbType.NVarChar, 255).Value = fullquotedv; urlfetch.SelectCommand.Parameters.Add("@strg", SqlDbType.NVarChar, 255).Value = quotedvstrat; urlfetch.Fill(urldata); if (e.Item.ItemType == ListItemType.Item) { ((Label)e.Item.FindControl("lblFirst")).Text = "New Text"; ((Repeater)e.Item.FindControl("innerDat")).DataSource = urldata; ((Repeater)e.Item.FindControl("innerDat")).DataBind(); } }The problem is that there is not data coming into any of the grids. I tried putting break-points on the GenerateGrid function, and it does hit there and runs successfully but the Outer DataList is not getting binded. It does not also fall into the OuterDataBound event.
Also, I checked the SQL stored procedure, and executed it manually and passed the following same parameter values, and it gave a result of tow rows something like this successfully:
SQL result:
Record1----
Record2----
Please suggest what I should.
Harpreet
Qin Dian Tan...
All-Star
113532 Points
12480 Posts
Microsoft
Re: Generate data controls according to user inputs
Mar 02, 2012 06:53 AM|LINK
Hi,
First you loop the DataList1 and generate sql query text for each checked item in it. Then for each checked loop you can return into a DataSet. Then bind it to a dynamic DataList and add it to page dyanmically.
Thanks,
If you have any feedback about my replies, please contactmsdnmg@microsoft.com.
Microsoft One Code Framework