更新ASP Datalist中的ASP标签

时间:2012-03-24 16:43:53

标签: c# asp.net gridview label datalist

我有一个带有嵌套GridView的ASP Datalist。 我试图为每个列表项显示一个ASP标签,其中gridview有超过6行,并且对于gridview具有< 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(); 

并且gridview填充如下:

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

   }

当gridview包含超过6行时,如何更新/填充标签 lblShowResponses

1 个答案:

答案 0 :(得分:0)

不确定这是否对您有所帮助,将DataListItemEventArgs参数“e”传递给BindGrid方法以及要填充标签的位置并使用

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