访问另一个数据绑定控件内的数据列表事件在嵌套数据列表中查找控件

发布于 2024-12-12 07:23:04 字数 944 浏览 1 评论 0原文

我在另一个 DataList 中有一个 DataList。我想访问子 DataList“dlQuestion”事件、ItemDataBound 事件。另外,我试图在子数据列表中找到控件 LableControl“lblQuestion”。我该怎么做?这是标记:

<asp:DataList ID="dlSection" runat="server" Width="100%">
   <ItemTemplate>
       <div>
         <asp:Label ID="lblSection" runat="server" Text='<%# Eval("Section") %>'></asp:Label>
         <asp:HiddenField ID="hfSectionId" runat="server" Value='<%# Eval("SectionId") %>' />
        </div>
        <asp:DataList ID="dlQuestion" runat="server"  >
        <ItemTemplate>
           <asp:Label ID="lblQuestion" runat="server" Text='<%# Eval("Question") %>'></asp:Label></td>
           <asp:HiddenField ID="hfQuestionId" runat="server" Value='<%# Eval("QuestionId") %>' />
        </ItemTemplate>
       </asp:DataList>
    </ItemTemplate>
</asp:DataList>

I have a DataList inside another DataList. I want to access the child DataList "dlQuestion" events, ItemDataBound event. Also, I'm tring to find the control LableControl "lblQuestion" in the child datalist. How do I do that? Here's the mark-up:

<asp:DataList ID="dlSection" runat="server" Width="100%">
   <ItemTemplate>
       <div>
         <asp:Label ID="lblSection" runat="server" Text='<%# Eval("Section") %>'></asp:Label>
         <asp:HiddenField ID="hfSectionId" runat="server" Value='<%# Eval("SectionId") %>' />
        </div>
        <asp:DataList ID="dlQuestion" runat="server"  >
        <ItemTemplate>
           <asp:Label ID="lblQuestion" runat="server" Text='<%# Eval("Question") %>'></asp:Label></td>
           <asp:HiddenField ID="hfQuestionId" runat="server" Value='<%# Eval("QuestionId") %>' />
        </ItemTemplate>
       </asp:DataList>
    </ItemTemplate>
</asp:DataList>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

伤痕我心 2024-12-19 07:23:04

您需要处理 dlQuestion DataList 的 ItemDataBound 事件并在该事件处理程序中获取 lblQuestion Label:

标记:

<asp:DataList ID="dlSection" runat="server" Width="100%">
     <ItemTemplate>
          <div>
               <asp:Label ID="lblSection" runat="server" Text='<%# Eval("Section") %>'></asp:Label>
               <asp:HiddenField ID="hfSectionId" runat="server" Value='<%# Eval("SectionId") %>' />
          </div>
          <asp:DataList ID="dlQuestion" runat="server" OnItemDataBound="dlQuestion_ItemDataBound">
               <ItemTemplate>
                    <asp:Label ID="lblQuestion" runat="server" Text='<%# Eval("Question") %>'></asp:Label></td>
                    <asp:HiddenField ID="hfQuestionId" runat="server" Value='<%# Eval("QuestionId") %>' />
               </ItemTemplate>
          </asp:DataList>
     </ItemTemplate>
</asp:DataList>

代码隐藏:

protected void dlQuestion_ItemDataBound(object sender, DataListItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
        var lblQuestion = e.Item.FindControl("lblQuestion") as Label;
        if (lblQuestion != null)
        {
            lblQuestion.ForeColor = Color.Red;
        }
    }
}

You need to handle ItemDataBound event of the dlQuestion DataList and get lblQuestion Label in that event handler:

Markup:

<asp:DataList ID="dlSection" runat="server" Width="100%">
     <ItemTemplate>
          <div>
               <asp:Label ID="lblSection" runat="server" Text='<%# Eval("Section") %>'></asp:Label>
               <asp:HiddenField ID="hfSectionId" runat="server" Value='<%# Eval("SectionId") %>' />
          </div>
          <asp:DataList ID="dlQuestion" runat="server" OnItemDataBound="dlQuestion_ItemDataBound">
               <ItemTemplate>
                    <asp:Label ID="lblQuestion" runat="server" Text='<%# Eval("Question") %>'></asp:Label></td>
                    <asp:HiddenField ID="hfQuestionId" runat="server" Value='<%# Eval("QuestionId") %>' />
               </ItemTemplate>
          </asp:DataList>
     </ItemTemplate>
</asp:DataList>

Code-behind:

protected void dlQuestion_ItemDataBound(object sender, DataListItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
        var lblQuestion = e.Item.FindControl("lblQuestion") as Label;
        if (lblQuestion != null)
        {
            lblQuestion.ForeColor = Color.Red;
        }
    }
}
2024-12-19 07:23:04

这是在子数据列表中查找标签控件的一种方法...

 //here I am finding item(DataList) of child Datalist

 DataList dlSubChild = (DataList)childItem.FindControl("dlSubChild");
 foreach (DataListItem subChildItem in dlSubChild.Items)
 {

       //here I am finding item(TextBox) of sub child Datalist
       TextBox txtName = (TextBox)subChildItem.FindControl("txtName");



      //set literal(litName) text

     litName.Text = string.Format("{0}{1}", "Welcome ", txtName.Text);

}

我希望它能帮助您...

This is one way finding label control in child datalist...

 //here I am finding item(DataList) of child Datalist

 DataList dlSubChild = (DataList)childItem.FindControl("dlSubChild");
 foreach (DataListItem subChildItem in dlSubChild.Items)
 {

       //here I am finding item(TextBox) of sub child Datalist
       TextBox txtName = (TextBox)subChildItem.FindControl("txtName");



      //set literal(litName) text

     litName.Text = string.Format("{0}{1}", "Welcome ", txtName.Text);

}

i hope it will helps you ...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文