在.cs文件中无法访问的另一个数据列表中的日期列表的id

时间:2014-08-11 12:13:30

标签: c# datalist

我有一个datalist控件位于另一个datalist中。我需要访问.cs文件中的一个datalist id。

这是我的两位数据专家。

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

现在这里是.cs文件,我想访问datalist dl_cmt的数据列表。这就是我尝试过的。

 public void show_comment()
{

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



    DataList dl = (DataList)FindControl("dl_cmt");
    dl.DataSource = ds;
    dl.DataBind();

}

修改

我有这个按钮点击事件,我想调用showcomment

 protected void cmt_Click(object sender, EventArgs e)
{

    //DateTime.Now.ToString();
    Button btn = (Button)sender;
    DataListItem dli = (DataListItem)btn.NamingContainer;
    TextBox tx = (TextBox)dli.FindControl("tb_cmt");
    Label lb = (Label)dli.FindControl("lbl_sid");
    string userid = Session["userid"].ToString();

    sq.connection();
    SqlCommand cmd = new SqlCommand("insert into comment(ecomment,sid,my_date,regid_cmt) values(@myecomment,@mysid,@mydate,@reg_id)", sq.con);
    cmd.Parameters.AddWithValue("@myecomment", tx.Text);
    cmd.Parameters.AddWithValue("@mysid", lb.Text);
    cmd.Parameters.AddWithValue("@mydate", DateTime.Now.ToString("h:mm, MMM  dd, yyyy"));
    cmd.Parameters.AddWithValue("@reg_id", userid);
    cmd.ExecuteNonQuery();
    sq.con.Dispose();
    sq.con.Close();
    tx.Text = "";

    //show_comment();



}

修改

这就是gstr获得价值的方式。

public partial class loggedin : System.Web.UI.Page
{
public string gstr;
sql_con sq = new sql_con();
protected void Page_Load(object sender, EventArgs e)
{
    string st = Session["email"].ToString();
    if (Page.IsPostBack)
    {
        hfm mymaster = (hfm)Page.Master;
        lcont lc = mymaster.getlcont();
        lc.myevent += delegate(string st2)
        {
            string str = st2;
            gstr = str;
            showdata();
            bind_menuitem();
            bindratingService();
            bindrating();
            show_comment();
            bindslide();
        };
    }
}

1 个答案:

答案 0 :(得分:2)

你需要在ItemDataBound或ItemCreated之类的东西上做,因为内部DataList是外部列表中的一个项目:

protected void mydatalist_ItemDataBound(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();
    }
}

ASP.NET代码需要包含事件,例如:

OnItemDataBound="mydatalist_ItemDataBound"

<强> 更新

要在按钮点击事件或单独的方法中执行此操作,您应该能够执行此操作:

protected void show_comment(object sender, EventArgs e)
{
    foreach(DataListItem item in mydatalist.Items)
    {
        int index = item.itemindex;

        DataList dl = (DataList) mydatalist.Items[index].FindControl("dl_cmt");

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

更新2

protected void Page_Load(object sender, EventArgs e)
{
    if(String.IsNullOrEmpty(gstr)) Setgstr();
}

private void Setgstr()
{
    //Your code to set gstr
}

更新3

您还可以使用Session来存储变量:

protected void Page_Load(object sender, EventArgs e)
{
    Session["gstr"] = //Your code to set gstr
}

像这样访问它:

string str = Session["gstr"].ToString();