【问题标题】:How can fetch multiple table dynamically with table category (Header) and table content using asp.net c# webformsc# - 如何使用asp.net c# webforms动态获取具有表格类别(标题)和表格内容的多个表格
【发布时间】:2021-10-17 23:35:11
【问题描述】:

我正在尝试获取表类别,​​并且基于该表类别,该类别的多个详细信息必须在以下类别下自动获取,但不幸的是,在该单个类别下创建了多个表。

首先,我在 DB 上创建了 tbl_category(ID{PK}, catName) 表。然后创建 tbl_categoryDe​​tails(ID{PK}, catId{FK}, catDetails)。所以我需要单个类别下的多个 categoryDe​​tails 字段。

那么我必须逐个获取这个 catName 及其详细信息。我已经附上了静态表,我需要这种方式来获取表数据Static Table Image

但不幸的是,我以这种方式获取数据My Dynamic Table Image。 categoryName 获取多次,但我想要一次。我正在使用中继器控件从数据库中获取数据。请帮我解决这个问题。

我的 Forms.aspx 代码在这里:

                <asp:Repeater ID="rptr_form" runat="server">
                    <ItemTemplate>
                         <section class="about">
                            <div class="container">
                                <div class="row no-gutters d-flex justify-content-center">
                                    <div class="flex-column justify-content-center about-content minwidth">
                                        <div class="section-title">
                                            <div class="mycontent-center">
                                                <h2 style="margin-bottom: -5px;" data-toggle="collapse" role="button" href="#collapseExample19" aria-expanded="false" aria-controls="collapseExample19"><%#Eval("form_categoryname") %>  &nbsp <i class="fa fa-chevron-circle-down" aria-hidden="true"></i></h2>
                                                <br />
                                                <div class="content-style collapse tbl-scroll" id="collapseExample19">
                                                    <table class="table table-striped">
                                                        <thead>
                                                            <tr>
                                                                <th scope="col"></th>
                                                                <th scope="col">No</th>
                                                                <th scope="col">Bangla</th>
                                                                <th scope="col">English</th>
                                                            </tr>
                                                        </thead>
                                                        <tbody>
                                                            <tr class="table-info">
                                                                <td><%#Eval("form_details") %></td>
                                                                <td><%#Eval("form_no") %></td>
                                                                <td><a href="<%# "../" + Eval("form_bengali_path") %>" target="_blank">
                                                                    <img src="assets/image/pdf.jpg" style="height: 30px; width: 48px;" /></a></td>
                                                                <td class="center"><a href="<%# "../" + Eval("form_english_path") %>" target="_blank">
                                                                    <img src="assets/image/pdf.jpg" style="height: 30px; width: 48px;" /></a>
                                                                </td>
                                                            </tr>
                                                        </tbody>

                                                    </table>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </section>
                    </ItemTemplate>
                </asp:Repeater>

我的 Forms.aspx.cs 代码在这里:

protected void AllFormLoad()
        {
            try
            {
                using (SqlConnection con = new SqlConnection(strcon))
                {
                    SqlCommand cmd = new SqlCommand("spFormDetailsFetch", con);
                    cmd.CommandType = CommandType.StoredProcedure;
                    con.Open();
                    SqlDataAdapter sda = new SqlDataAdapter(cmd);
                    DataTable dt = new DataTable();
                    sda.Fill(dt);
                    rptr_form.DataSource = dt;
                    rptr_form.DataBind();
                }
            }
            catch (Exception ex)
            {
                Response.Write("<script>alert('" + ex.Message + "');</script>");
            }
        }

Sql 查询

select D.ID, C.form_categoryname, D.form_details, D.form_no, D.form_bengali_path, D.form_english_path from tbl_form_details D inner join tbl_form_category C on D.form_categoryId = C.ID order by form_categoryname

【问题讨论】:

    标签: c# asp.net webforms fetch dynamic-tables


    【解决方案1】:

    听起来你真的需要两个嵌套循环,一个用于类别,一个用于每个类别的详细信息。您应该将提取数据的 SQL 语句添加到您的问题中,以便我们可以确定,但我猜重复是因为连接。

    这不是一个很好的解决方案,但我之前在此方案中所做的是在代码隐藏中使用 ItemDataBound 事件来仅显示每个类别的第一行的标题。因此,您可以在某个地方(可能在隐藏字段中)跟踪类别 ID,并且随着 itemdatabound 循环遍历记录,当类别 ID 更改时,您会显示该项目记录的标题,然后更新类别 ID。然后,只要类别 ID 保持不变,您就会一直隐藏标题。当它再次发生变化时,您知道您已经达到了下一个类别的记录,是时候再次显示标题了。

    下面是一个简化的示例,请注意,您需要在页眉和页脚部分周围包裹一个占位符,以便您可以根据需要隐藏它们。在您的情况下,这将是 tr 上方和下方的所有内容。

    中继码:

    <asp:Repeater ID="Repeater1" runat="server" OnItemDataBound="Repeater1_ItemDataBound">
    <ItemTemplate>
        <asp:PlaceHolder ID="pnlHead" runat="server">
            <section>
                <h2><%#DataBinder.Eval(Container.DataItem, "form_categoryname").ToString()%></h2>
        </asp:PlaceHolder>
        <div>
            <%#DataBinder.Eval(Container.DataItem, "form_details").ToString()%>
            <asp:HiddenField ID="HiddenFieldID" runat="server" Value='<%# DataBinder.Eval(Container.DataItem,"form_categoryname") %>' />
        </div>
        <asp:PlaceHolder ID="pnlFoot" runat="server">
            </section>
        </asp:PlaceHolder>
    </ItemTemplate>
    </asp:Repeater>
    

    页面加载:

        public string CurrentCategory = "";
        
        protected void Page_Load(object sender, EventArgs e)
        {
            using (SqlConnection con = new SqlConnection(DB.GetDBConn()))
            {
                con.Open();
                using (IDataReader rs = DB.GetRS("select D.ID, C.form_categoryname, D.form_details, D.form_no, D.form_bengali_path, D.form_english_path from tbl_form_details D inner join tbl_form_category C on D.form_categoryId = C.ID order by form_categoryname", con))
                {
                    Repeater1.DataSource = rs;
                    Repeater1.DataBind();
    
                }
            }
    
            int idx = 0;
            // where head is visible, show the foot for the previous row
            foreach (RepeaterItem item in Repeater1.Items)
            {
                if (idx > 0)
                {
                    bool headvisible = item.FindControl("pnlHead").Visible;
    
                    if (headvisible)
                    {
                        Repeater1.Items[idx - 1].FindControl("pnlFoot").Visible = true;
                    }
                }
                idx++;
            }
        }
    

    OnItemDataBound:

    protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            HiddenField HiddenFieldID = e.Item.FindControl("HiddenFieldID") as HiddenField;
    
            string itemCategory = HiddenFieldID.Value;
    
            if (itemCategory == CurrentCategory)
            {
                PlaceHolder pnlHead = e.Item.FindControl("pnlHead") as PlaceHolder;
                PlaceHolder pnlFoot = e.Item.FindControl("pnlFoot") as PlaceHolder;
                pnlHead.Visible = false;
                pnlFoot.Visible = false;
            }
            else
            {
                //Now we're in a new category, so show header
                PlaceHolder pnlHead = e.Item.FindControl("pnlHead") as PlaceHolder;
                PlaceHolder pnlFoot = e.Item.FindControl("pnlFoot") as PlaceHolder;
                pnlHead.Visible = true;
                pnlFoot.Visible = false;
    
    
                CurrentCategory = itemCategory;
            }
            
        }
    

    【讨论】:

    • 我已经添加了sql查询,请检查一下。
    • 请给出ItemDataBound的解决方案,如何实现。
    • 确实是您的加入导致了问题。我添加了一些示例代码来演示如何使用该 sql 和 itemdatabound 事件来获得所需的结果。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-26
    • 1970-01-01
    相关资源
    最近更新 更多