如何从父Datalist向子Datalist发送参数 [英] How can I send a parameter from parent Datalist to child Datalist

查看:134
本文介绍了如何从父Datalist向子Datalist发送参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个嵌套的Datalist,他们正在填充数据库,我希望孩子Datalist将填充来自Parent DataList的Id ...



我使用了带面板的隐藏字段,但它不起作用!



这是我的代码:



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:

<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>







代码背后是:



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();



}

}




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();

}
}

推荐答案

嗨试试这个,



您必须从Sqlserver中选择两个表,一个是父级datalist,另一个是Child datalist,我们可以使用两个表进行重新关联





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


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







<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

 DS= objSubject.getAllConfirmedDataOrderByIndex();

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

                   dtlSubjectBox.DataSource = DS;
                   dtlSubjectBox.DataBind();


亲爱的Murugesan22,是的我也添加了标题部分,我做了一些更改,现在有这个错误消息...



该关系不是这个DataView指向的表的父级。



你能帮我解决一下吗?



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?

<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>





代码落后:



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; Passwor d = 1234;连接超时= 30;);

SqlDataAdapter cmd1 =新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 =新的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.Ite m.DataItem as DataRowView;



DataList dtlArticleBox = e.Item.FindControl(\"dtlArticleBox\") as DataList;

dtlArticleBox.DataSource = drv.CreateChildView(\"myrelation\");

dtlArticleBox.DataBind();



}



}



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();

}

}


这篇关于如何从父Datalist向子Datalist发送参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆