c#中带有OnItemDataBound属性的访问控制id

本文关键字:访问控制 id 属性 OnItemDataBound | 更新日期: 2023-09-27 18:17:45

我有这个嵌套的数据列表。

编辑

<asp:DataList ID="mydatalist" DataKeyField="sid" ItemStyle-CssClass="lft_c_down"  runat="server">
   <ItemTemplate>
    <div class="wholeC">
    <div class="ctop">
    <div class="lft_l">
    <div class="lft_l_top">
    <asp:Image ID="Image1" runat="server" ImageUrl='<%#DataBinder.Eval(Container.DataItem,"ipath")%>' Height="245px" Width="297px" />
    <br/>
    </div>
    </div>
    <div class="lft_r">
    <asp:Label ID="lbl_sid" Text='<%#DataBinder.Eval(Container.DataItem,"sid") %>' runat="server" Visible="false" />
    <b class="products" >Product Name:</b>
    <asp:Label ID="lbl2" Text='<%#DataBinder.Eval(Container.DataItem,"ipath") %>' runat="server" />
    <br/>
    <b class="products">Brand:</b>
    <asp:Label ID="lbl1" Text='<%#DataBinder.Eval(Container.DataItem,"brand") %>' runat="server" />
    <br/>
    </div>
    </div>
    <div class="cdown">
    <asp:TextBox ID="tb_cmt" runat="server" Height="35px" Width="500px" placeholder="comment.."  />
    <asp:Button ID="Button1"  runat="server" Text="Comment" backcolor="black" BorderStyle="None" Font-Names="Consolas" Font-Overline="False" 
    ForeColor="White" Height="34px" Width="108px" OnClick="cmt_Click" />

<asp:DataList ID = "dl_cmt" runat="server">
    <ItemStyle CssClass="coment" />
    <ItemTemplate>
    <asp:Label ID="lblcmt" runat="server" Text='<%#Eval("ecomment")%>' />
    <asp:Label ID="lblDate" style=" color:brown; font-family:Cursive; font-size:x-small; " runat="server" Text='<%#Eval("my_date","on {0}") %>' />
    </ItemTemplate>
    </asp:DataList>
    <%--<asp:LinkButton ID="lb_showMore" runat="server">Show More</asp:LinkButton>--%>
    </div>
    </div>
    </ItemTemplate>
    </asp:DataList>

它使用属性onItemDataBound,但是由于它的使用,我无法访问这个数据列表的id。

public void cmtdatabound(object sender, DataListItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Item)
    {
        DataList dl = e.Item.FindControl("dl_cmt") as DataList;
        string str = gstr;
        sq.connection();
        SqlCommand cmd = new SqlCommand("select top 4 * from comment where sid='" + str + "' order by my_date desc", sq.con);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        dl.DataSource = ds;
        dl.DataBind();
    }
}

这里dl为null,这意味着id不可访问。我如何在这里访问数据的id

c#中带有OnItemDataBound属性的访问控制id

您可以像这样使用sender参数DataList:

 DataList dl = sender as DataList;