Click here to Skip to main content
15,899,126 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a nested Datalist, they are filling from a database, I want the child Datalist will fill with an Id which is from Parent DataList...

I used a hidden field with panel, but it did not work!

Here is my code:

XML
<asp:DataList ID="dtlSubjectBox" runat="server" RepeatColumns="2" RepeatDirection="Horizontal"                                        Width="600" OnItemDataBound="OuterRepItemDataBound">                                                      <ItemTemplate> <table width="295" border="1" cellspacing="0" cellpadding="0" bgcolor="#ffffff">                                                               <tr> <asp:Panel ID="pnlChildView" runat="server">                                                                    <asp:HiddenField ID="hdnCompany" runat="server" Value='<%# DataBinder.Eval(Container.DataItem, "Id")%> ' />                                                            <td height="20" align="center" valign="middle">                                                                            <%# DataBinder.Eval(Container.DataItem, "Title")%>                                                            </td> </asp:Panel> </tr> <tr> <td align="right" valign="top"> <asp:DataList ID="dtlArticleBox" runat="server"> <ItemTemplate> <table width="295" cellspacing="0" cellpadding="0"> <tr> <td height="20" align="right" valign="middle"> * <%# DataBinder.Eval(Container.DataItem, "Title")%> </td> </tr>                                         </table> </ItemTemplate> </asp:DataList> </td> </tr>
</table> </ItemTemplate> </asp:DataList>



Code Behind is:

protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
dtlSubjectBox.DataSource = objSubject.getAllConfirmedDataOrderByIndex();
dtlSubjectBox.DataBind();
}
}

protected void OuterRepItemDataBound(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item)
{
DataList dtlArticleBox = e.Item.FindControl("dtlArticleBox") as DataList;

HiddenField hdnCompany = (HiddenField)e.Item.NamingContainer.Parent.FindControl("hdnCompany");

dtlArticleBox.DataSource = objArticle.GetArticlesOfSubject(Helper.ParseInt(hdnCompany));
dtlArticleBox.DataBind();

}
}
Posted
Updated 9-Apr-14 5:12am
v6

Hi try this,

You have to select two table from Sqlserver one is parent datalist and another one is Child datalist we can make realationship with two table


C#
Import the header  <%@ Import Namespace="System.Data" %>


C#
<asp:datalist id="dtlSubjectBox" runat="server" repeatcolumns="2" repeatdirection="Horizontal" xmlns:asp="#unknown">
              Width="600">
              <itemtemplate>
                  <table width="295" border="1" cellspacing="0" cellpadding="0" bgcolor="#ffffff">
                      <tr>
                          <asp:panel id="pnlChildView" runat="server">
                              <asp:hiddenfield id="hdnCompany" runat="server" value="<%#Eval("Id")%> " />
                              <td height="20" align="center" valign="middle">
                                 <%#Eval("Title")%>
                              </td>
                          </asp:panel>
                      </tr>
                      <tr>
                          <td align="right" valign="top">
                              <asp:datalist id="dtlArticleBox" runat="server" datasource="<%# ((DataRowView)Container.DataItem).Row.GetChildRows("Relation") %>">
                                  <itemtemplate>
                                      <table width="295" cellspacing="0" cellpadding="0">
                                          <tr>
                                              <td height="20" align="right" valign="middle">
                                                  *
                                                 <%# DataBinder.Eval(Container.DataItem, "[\""Title"\"]")%>
                                              </td>
                                          </tr>
                                      </table>
                                  </itemtemplate>
                              </asp:datalist>
                          </td>
                      </tr>
                  </table>
              </itemtemplate>
          </asp:datalist>





and

Code Behind is

C#
 DS= objSubject.getAllConfirmedDataOrderByIndex();

DS.Relations.Add("Relation", DS.Tables[0].Columns["Title"], DS.Tables[1].Columns["Title"]);

                   dtlSubjectBox.DataSource = DS;
                   dtlSubjectBox.DataBind();
 
Share this answer
 
v2
Comments
sheryi26 9-Apr-14 13:26pm    
Dear Murugesan22,
Thanks for your solution, I did all of the solution, but I have a problem about making relationship between 2 tables.
Where can I do that? in view or just in diagram?
It has parsing error in this line:
<asp:datalist id="dtlArticleBox" runat="server" datasource="<%# ((DataRowView)Container.DataItem).Row.GetChildRows("Relation") %>">

Kind Regards in advance for your help
Murugesan22 9-Apr-14 14:04pm    
hi,

did you add header part Import
Murugesan22 9-Apr-14 14:05pm    
i did same code it gives correct result
Dear Murugesan22, yes I added header part, too and I made some changes and it has this error message now...

The relation is not parented to the table to which this DataView points.

Would you please help me about it?

XML
<asp:DataList ID="dtlSubjectBox" runat="server" RepeatColumns="2" RepeatDirection="Horizontal"
                                        Width="600" OnItemDataBound="OuterRepItemDataBound">
                                        <ItemTemplate>
                                            <table width="295" border="1" cellspacing="0" cellpadding="0" bgcolor="#ffffff">
                                                <tr>

                                                        <td height="20" align="center" valign="middle">
                                                            <%# DataBinder.Eval(Container.DataItem, "Title")%>
                                                        </td>

                                                </tr>
                                                <tr>
                                                    <td align="right" valign="top">
                                                        <asp:DataList ID="dtlArticleBox" runat="server">
                                                            <ItemTemplate>
                                                                <table width="295" cellspacing="0" cellpadding="0">
                                                                    <tr>
                                                                        <td height="20" align="right" valign="middle">

                                                                           <%# ((DataRowView)Container.DataItem).Row.GetChildRows("Relation") %>

                                                                        </td>
                                                                    </tr>
                                                                </table>
                                                            </ItemTemplate>
                                                        </asp:DataList>
                                                    </td>
                                                </tr>
                                            </table>
                                        </ItemTemplate>
                                    </asp:DataList>


code behind:

protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{BindData();

}
}

private void BindData()
{

SqlConnection cnn = new SqlConnection("Data Source=SHERY-PC;Initial Catalog=News;User ID=sa;Password=1234;Connection Timeout=30;");
SqlDataAdapter cmd1 = new SqlDataAdapter("SELECT [Id], [Title], [Confirmed], [OrderIndex] FROM NewsSubject WHERE [Confirmed]=1 ORDER BY [OrderIndex]", cnn);

DataSet ds = new DataSet();
cmd1.Fill(ds, "NewsSubject");

SqlDataAdapter cmd2 = new SqlDataAdapter("SELECT [Id], [Title] FROM NewsArticle WHERE ([Confirmed]=1 AND [SubjectId]=@SubjectId) ORDER BY [Id]", cnn);
cmd2.Fill(ds, "NewsArticle");

ds.Relations.Add("myrelation", ds.Tables["NewsSubject"].Columns["Title"], ds.Tables["NewsArticle"].Columns["Title"]);

dtlSubjectBox.DataSource = ds.Tables["NewsSubject"];
dtlSubjectBox.DataBind();
}

protected void OuterRepItemDataBound(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item)
{

DataRowView drv = e.Item.DataItem as DataRowView;

DataList dtlArticleBox = e.Item.FindControl("dtlArticleBox") as DataList;
dtlArticleBox.DataSource = drv.CreateChildView("myrelation");
dtlArticleBox.DataBind();

}

}
 
Share this answer
 
v5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900