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

时间:2014-08-12 04:28:43

标签: c# asp.net

我有这个嵌套的datalist。

修改

<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。但是如果使用它,我无法访问此datalist的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为空,表示无法访问id。如何在此处访问datalist的id。

1 个答案:

答案 0 :(得分:1)

您可以将发件人参数用作DataList,如下所示:

 DataList dl = sender as DataList;