更新ASP数据列表中的ASP标签

本文关键字:ASP 标签 列表 数据 更新 | 更新日期: 2023-09-27 18:28:58

我有一个带有嵌套GridView的ASP数据列表。我试图为网格视图有6行以上的每个列表项显示ASP标签,并为网格视图具有<6排。

这是数据列表:

<asp:DataList runat="server" id="listResponses" DataKeyField="QuestionID" OnItemDataBound="listResponses_ItemDataBound" Width="100%">
                <ItemTemplate>
                    <div class="question_header">
                        <p><strong><asp:Label ID="lblOrder" runat="server" Text='<%# Container.ItemIndex  + 1 %>'></asp:Label>. <%# DataBinder.Eval(Container.DataItem, "QuestionText") %></strong></p>
                    </div> <!-- end question_header -->
                    <asp:GridView runat="server" ID="gridResponses" DataKeyNames="AnswerID" AutoGenerateColumns="False" CssClass="responses" AlternatingRowStyle-BackColor="#f3f4f8">
                        <Columns>
                            <asp:BoundField DataField="AnswerTitle" HeaderText="Answer Title" HeaderStyle-ForeColor="#717171" ItemStyle-Width="250px"></asp:BoundField>
                            <asp:BoundField DataField="Responses" HeaderText="Response Count" HeaderStyle-ForeColor="#717171" HeaderStyle-Width="100px" />
                            <asp:TemplateField>
                                <ItemTemplate>
                                    <div class="pbcontainer">
                                        <div class="progressbar"></div>
                                        <asp:HiddenField ID="hiddenValue" runat="server" Value='<%# Eval("Responses") %>' />
                                     </div>
                                </ItemTemplate>
                            </asp:TemplateField>
                        </Columns>
                    </asp:GridView>
                    <a><asp:Label runat="server" ID="lblShowResponses" Visible="false"></asp:Label></a>  
                </ItemTemplate>
            </asp:DataList>

我尝试更新的标签是lblShowResponses

填充数据列表的方法:

// populate datalist.
DT = GetData.GetQuestionNameDataList(qid);
listResponses.DataSource = DT;
listResponses.DataBind(); 

网格视图填充如下:

    protected void listResponses_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        GridView gridResponses = (GridView)e.Item.FindControl("gridResponses");
        BindGrid(gridResponses, (int)listResponses.DataKeys[e.Item.ItemIndex], DT.Rows[e.Item.ItemIndex][2].ToString());
    }
    // Get the question ID from the datalist and parse the parameters to BindGrid
    protected void listResponses_ItemDataBound(object sender, DataListItemEventArgs e)
    {
         GridView gridResponses = (GridView)e.Item.FindControl("gridResponses");
         BindGrid(gridResponses, (int)listResponses.DataKeys[e.Item.ItemIndex], DT.Rows[e.Item.ItemIndex][2].ToString());
    }
    private void BindGrid(GridView GridView, int questionId, string questionType)
    {
        // get the answerID and title for the current question.
        DataTable answersDataTable = new DataTable();
        answersDataTable = GetData.GetAnswerResponses(questionId);
        DataTable tempResponses = new DataTable();
        // checkbox question type - loop through each answer and obtain the number of responses.       
        for (int answer = 0; answer < answersDataTable.Rows.Count; answer++)
        {
            // populate tempaory datatable and replace DT with the response count.
            string answerID = answersDataTable.Rows[answer][0].ToString();
            tempResponses = GetData.getIndividualQuestionResponses(questionId, answerID);
            answersDataTable.Rows[answer][2] = tempResponses.Rows[0][0];
        }
        if (GridView.Rows.Count > 6)
        {
            for (int x = 6; x < GridView.Rows.Count; x++)
            {
                GridView.Rows[x].Visible = false;
            }
            // I want to populate the label here!!!!!!
       }
   }

当网格视图包含6行以上时,如何更新/填充标签lblShowResponses

更新ASP数据列表中的ASP标签

不确定这是否会对您有所帮助,请将DataListItemEventArgs param"e"传递给BindGrid方法,并在您想要填充标签的位置使用

(e.Item.FindControl("lblShowResponses") as Label).Text = "Test";
(e.Item.FindControl("lblShowResponses") as Label).Visible = true;