简体   繁体   English

.net 中继器与子中继器

[英].net repeater with child repeater

I have the following asp.net repeater with a child repeater我有以下带有子中继器的asp.net中继器

<asp:Repeater ID="MainAccordianRepeater" runat="server">
    <ItemTemplate>
    <div class="panel panel-default">
        <div class="panel-heading">
        <h4 class="panel-title">
            <a data-toggle="collapse" data-parent="#accordion" class="mainAccordLink" href="#collapse<%# Eval("catId").ToString() %>">
            <%# Eval("catDesc").ToString() %>
            </a>
            <input type="button" value="<%# Eval("btnTxt").ToString() %>" onclick="Confirm('<%# Eval("statusFunction").ToString() %>root','<%# Eval("catId").ToString() %>')" class="<%# Eval("statusFunction").ToString() %> btn" id="rootbutton<%# Eval("catId").ToString() %>" />
        </h4>
        </div>
        <div id="collapse<%# Eval("catId").ToString() %>" class="panel-collapse collapse">
        <ul id="childUl<%# Eval("catId").ToString() %>" class="childRepeater_DataBinding">
            <asp:Repeater ID="childRepeater" runat="server" OnDataBinding="ChildCats_DataBinding" />

            </asp:Repeater>
        </ul>
        </div>
    </div>
    </ItemTemplate>
</asp:Repeater>

and this is the code that should populate the repeater and it's child这是应该填充中继器的代码,它是子代

protected void GetRootCategories()
{
using (SqlConnection con = new SqlConnection(connStr))
{
    using (SqlCommand cmd = new SqlCommand("spR_GetRootCategories", con))
    {
    cmd.CommandType = CommandType.StoredProcedure;
    con.Open();
    SqlDataReader reader = cmd.ExecuteReader();
    MainAccordianRepeater.DataSource = reader;
    MainAccordianRepeater.DataBind();
    con.Close();
    }
}
}
protected void ChildCats_DataBinding(object sender, System.EventArgs e)
{
Repeater rep = (Repeater)(sender);

int parentCatId = (int)(Eval("catId"));
using (SqlConnection con = new SqlConnection(connStr))
{
    using (SqlCommand cmd = new SqlCommand("spR_GetChildCategories", con))
    {
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.Parameters.AddWithValue("@ParentId", parentCatId);
    con.Open();
    SqlDataReader rdr = cmd.ExecuteReader();
    //rep.DataSource = rdr;
    //rep.DataBind();
    con.Close();
    }
}
}

the problem is when ever I uncomment these two lines问题是当我取消注释这两行时

    //rep.DataSource = rdr;
    //rep.DataBind();

it crashes with the error它因错误而崩溃

System.InvalidOperationException: Timeout expired.  The timeout period elapsed prior to obtaining a connection from the pool. 

can anyone help me out谁能帮我吗

I have modified the code to this我已将代码修改为此

protected void ChildCats_DataBinding(object sender, System.EventArgs e)
{
    Repeater rep = (Repeater)(sender);

    int parentCatId = (int)(Eval("catId"));
    DataSet ChildCatsDs = new DataSet();
    using (SqlConnection con = new SqlConnection(connStr))
    {
        using (SqlCommand cmd = new SqlCommand("spR_GetChildCategories", con))
        {
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@ParentId", parentCatId);
            con.Open();
            SqlDataAdapter cCats = new SqlDataAdapter();
            cCats.SelectCommand = cmd;
            cCats.Fill(ChildCatsDs);
            //SqlDataReader rdr = cmd.ExecuteReader();

            rep.DataSource = ChildCatsDs;
            rep.DataBind();
            con.Close();
        }
    }
}

but it just gives the exact same outcome但它只是给出了完全相同的结果

I have just modified it again to this我刚刚再次修改为这个

protected void ChildCats_DataBinding(object sender, System.EventArgs e)
{
    Repeater rep = (Repeater)(sender);

    int parentCatId = (int)(Eval("catId"));
    DataSet ChildCatsDs = new DataSet();
    using (SqlConnection con = new SqlConnection(connStr))
    {
        using (SqlCommand cmd = new SqlCommand("spR_GetChildCategories", con))
        {
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@ParentId", parentCatId);
            con.Open();
            SqlDataAdapter cCats = new SqlDataAdapter();
            cCats.SelectCommand = cmd;
            cCats.Fill(ChildCatsDs);
            //SqlDataReader rdr = cmd.ExecuteReader();

            //rep.DataSource = ChildCatsDs;
            //rep.DataBind();
            con.Close();
        }
    }
    DataTable dt = new DataTable();
    dt.Columns.Add("catId");
    dt.Columns.Add("parentId");
    dt.Columns.Add("catDesc");
    dt.Columns.Add("status");
    dt.Columns.Add("statusFunction");
    dt.Columns.Add("btnTxt");
    dt.Columns.Add("disabled");
    DataRow dr = dt.NewRow();
    dr["catId"] = 83;
    dr["parentId"] = 72;
    dr["catDesc"] = "orange";
    dr["status"] = "0";
    dr["statusFunction"] = "enable";
    dr["btnTxt"] = "activate";
    dr["disabled"] = "t";
    dt.Rows.Add(dr);
    rep.DataSource = dt;
    rep.DataBind();
}

and the page just times out并且页面刚刚超时

you should bind the child repeater based on ItemDataBoundEvent of parent repeater .您应该根据父中继器的 ItemDataBoundEvent 绑定子中继器。 Try like given below尝试如下所示

protected void MainAccordianRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    try
    {

        Repeater rep = e.Item.FindControl("childRepeater") as Repeater;

        int parentCatId = (int)(Eval("catId"));
        using (SqlConnection con = new SqlConnection(connStr))
        {
            using (SqlCommand cmd = new SqlCommand("spR_GetChildCategories", con))
            {
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@ParentId", parentCatId);
                con.Open();
                SqlDataReader rdr = cmd.ExecuteReader();
                rep.DataSource = rdr;
                rep.DataBind();
                con.Close();
            }
        }
    }
    catch (Exception ex)
    {

    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM