为什么列表视图和数据分页器中未正确启用页码

发布于 2024-10-14 09:38:22 字数 10059 浏览 5 评论 0原文

我正在使用 listview 和 datapager 进行显示。我使用两级列表视图,其中内部列表视图项目可以是 6 种类型。我使用我自己的数据源。我只需要每页显示 1 个内部列表视图项。它正确显示每页,但页码设置不正确,例如,当单击第二页(其他也一样)时,第二页码没有禁用,而第一页码仍然禁用。这是代码:

   <div style="margin: 10px;">
        <asp:ListView ID="rptExercise2" runat="server" 
            ItemPlaceholderID="itemPlaceHolder1" OnItemDataBound="ExerciseBound" OnPagePropertiesChanging="ChangeExercise">
            <LayoutTemplate>      
                <asp:PlaceHolder ID="itemPlaceHolder1" runat="server" />
            </LayoutTemplate>

            <ItemTemplate>
            <h2><asp:Label ID="lbName" runat="server" /></h2>
            <asp:ListView ID="rptContent2" runat="server" ItemPlaceholderID="itemPlaceHolder2" OnItemDataBound="ContentBound">
                <LayoutTemplate>      
                    <asp:PlaceHolder ID="itemPlaceHolder2" runat="server" />
                </LayoutTemplate>

                <ItemTemplate>
                    <div style="padding: 5px;" >                               
                        <div>
                        <asp:Label ID="lbID" runat="server" Text='<%# Eval("ID") %>' Visible="false" />

                        <asp:Label ID="lbWrapper" runat="server" Text='<%# Eval("Wrapper") %>' Visible="false" />

                        <asp:Label ID="lbAudioQuestion" runat="server" Visible="false" />

                        <div style="text-align: right;"><asp:Label ID="lbAudioAnswer" runat="server" Visible="false" /></div>

                        <div style="text-align: right;"><asp:TextBox ID="tbAnswer" runat="server" TextMode="MultiLine" CssClass="tb" Width="50%" Height="60" Visible="false" /></div>

                        <div style="margin-left: 20px;"><asp:RadioButtonList ID="rblChoice" runat="server" Visible="false" /></div>                               
                        </div>                                
                    </div>
                </ItemTemplate>
            </asp:ListView>

            <asp:Panel ID="pnFeedback" runat="server" Visible="false" CssClass="Feedback">
                <div style="margin: 10px;">
                    <div style="float: left;">
                    Score: <br /><asp:TextBox ID="tbScore" runat="server" CssClass="tb" Width="40" /> / <asp:Label ID="lbScore" runat="server" /></div>
                    </div>
                    <asp:Label ID="lbResultID" runat="server" Visible="false" />
                    <div style="float: left;">
                    Feedback<br />
                    <asp:TextBox ID="tbFeedback" runat="server" TextMode="MultiLine" CssClass="tb" Width="200" Height="60" />
                    </div>

                    <div style="float: left;">
                    Recording<br />
                    <asp:Label ID="lbFeedbackAudio" runat="server" />
                    </div>
                    <br style="clear: both;" />
                </div>
            </asp:Panel>

            </ItemTemplate>
            <EmptyItemTemplate>
                <div>
                    Sorry, no exercise is found for this lesson.
                </div>
            </EmptyItemTemplate>

        </asp:ListView>


        <asp:DataPager ID="DataPager" runat="server" PagedControlID="rptExercise2" 
            PageSize="1">
            <Fields>
                <asp:NextPreviousPagerField ButtonType="Button" ShowFirstPageButton="True" 
                    ShowNextPageButton="False" ShowPreviousPageButton="False" />
                <asp:NumericPagerField ButtonCount="10" ButtonType="Button" />
                <asp:NextPreviousPagerField ButtonType="Button" ShowLastPageButton="True" 
                    ShowNextPageButton="False" ShowPreviousPageButton="False" />
            </Fields>
        </asp:DataPager>  

public partial class MyPracClass : System.Web.UI.Page
{
    // Current page number
    int i = 0;

    protected classLessons les;
    protected classExercises exc;
    protected List<classExercises> exercises;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request["LessonID"] == null)
        {
            return;
        }

        classLessons les = new classLessons(Convert.ToInt32(Request["LessonID"]));
        exc = new classExercises();
        exercises = exc.GetExercisesByLessonID(les.ID);

        if (!IsPostBack)
        {
            lbLessonName.Text = les.Name;
            lbDescription.Text = les.Description;
            //SetPageNumber();

            rptExercise2.DataSource = exercises;
            rptExercise2.DataBind();
        }
    }

    protected void ExerciseBound(object sender, ListViewItemEventArgs e)
    {
        if (e.Item.ItemType == ListViewItemType.DataItem)
        {
            ListViewDataItem item = (ListViewDataItem)e.Item;

            Label lbName = (Label)item.FindControl("lbName");
            string excName = "Exercise " + (i+1) + ": " + exercises[i].Subject;
            lbName.Text = excName;

            Label lbFeedbackAudio = (Label)item.FindControl("lbFeedbackAudio");
            classContent ct = new classContent();
            string test = "exercise" + exercises[i].ID + "student" + Session["StudentID"] + "feedback";
            lbFeedbackAudio.Text = ct.GetPlayingString("exercise" + exercises[i].ID + "student" + Session["StudentID"] + "feedback");

            ListView rptContent2 = (ListView)item.FindControl("rptContent2");
            Panel pnFeedback = (Panel)item.FindControl("pnFeedback");

            if (exercises[i] != null && rptContent2 != null)
            {
                classContent cct = new classContent();

                rptContent2.DataSource = cct.GetContentByExerciseID(exercises[i].ID);
                rptContent2.DataBind();
            }

            var result = new classResults();

            result.GetResultByExerciseID(Convert.ToInt32(exercises[i].ID));

            if (result.ID > 0)
            {
                pnFeedback.Visible = true;
                Label lbID = (Label)item.FindControl("lbResultID");
                Label lbScore = (Label)item.FindControl("lbScore");
                lbScore.Text = exercises[i].Score.ToString();
                TextBox tbScore = (TextBox)item.FindControl("tbScore");
                TextBox tbFeedback = (TextBox)item.FindControl("tbFeedback");

                lbID.Text = result.ID.ToString();
                tbScore.Text = result.Score.ToString();
                tbFeedback.Text = result.Feedback;
            }
        }
    }

    protected void ChangeExercise(object sender, PagePropertiesChangingEventArgs e)
    {
        // Get current page number
        i = e.StartRowIndex / e.MaximumRows;

        rptExercise2.DataSource = exercises;
        rptExercise2.DataBind();
    }

    //protected void DataPager_PreRender(object sender, EventArgs e)
    //{
    //    // Get current page number
    //    //i = e.StartRowIndex / e.MaximumRows;

    //    rptExercise2.DataSource = exercises;
    //    rptExercise2.DataBind();
    //}

    protected void ContentBound(object sender, ListViewItemEventArgs e)
    {
        if (e.Item.ItemType == ListViewItemType.DataItem)
        {
            ListViewDataItem dataItem = (ListViewDataItem)e.Item;
            var qry = (classContent)dataItem.DataItem;

            Label lbAudioQuestion = (Label)e.Item.FindControl("lbAudioQuestion");
            Label lbAudioAnswer = (Label)e.Item.FindControl("lbAudioAnswer");
            Label lbWrapper = (Label)e.Item.FindControl("lbWrapper");
            TextBox tbAnswer = (TextBox)e.Item.FindControl("tbAnswer");
            RadioButtonList rblChoice = (RadioButtonList)e.Item.FindControl("rblChoice");

            switch (qry.Type)
            {
                case classContent.ContentType.AudioQuestion:
                    lbAudioQuestion.Visible = true;
                    lbAudioQuestion.Text = qry.GetPlayingString(qry.Wrapper);
                    break;
                case classContent.ContentType.TextQuestion:
                    lbWrapper.Visible = true;
                    break;
                case classContent.ContentType.VideoQuestion:
                    lbWrapper.Visible = true;
                    break;
                case classContent.ContentType.AudioAnswer:
                    lbAudioAnswer.Visible = true;
                    lbAudioAnswer.Text = qry.GetRecordingString("content" + qry.ID + "student" + Session["StudentID"]);
                    break;
                case classContent.ContentType.TextAnswer:
                    tbAnswer.Visible = true;
                    break;
                case classContent.ContentType.ChoiceAnswer:
                    rblChoice.DataSource = qry.GetChoiceOptions(qry.ID);
                    rblChoice.DataTextField = "Value";
                    rblChoice.DataValueField = "IsAnswer";
                    rblChoice.DataBind();
                    rblChoice.Visible = true;
                    break;
                default: break; ;
            }

            classAnswers ans = new classAnswers();

            var contentAnswer = ans.GetAnswerByContentID(qry.ID);

            if (contentAnswer.ID > 0)
            {
                switch (qry.Type)
                {
                    case classContent.ContentType.TextAnswer:
                        tbAnswer.Text = contentAnswer.Answer;
                        break;
                    case classContent.ContentType.ChoiceAnswer:
                        rblChoice.SelectedIndex = Convert.ToInt32(contentAnswer.Answer);
                        break;
                    default: break; ;
                }
            }

        }

    }

I am using listview and datapager for display. I use two level of listview, where inner listview item can be 6 types. I use my own datasource. I only need to display 1 inner listviewitem per page. It display each page correctly, but page number is not set right, eg, when 2nd page (other as well) is click, 2nd page number is not disabled while 1st page number is still disabled. Here is the code:

   <div style="margin: 10px;">
        <asp:ListView ID="rptExercise2" runat="server" 
            ItemPlaceholderID="itemPlaceHolder1" OnItemDataBound="ExerciseBound" OnPagePropertiesChanging="ChangeExercise">
            <LayoutTemplate>      
                <asp:PlaceHolder ID="itemPlaceHolder1" runat="server" />
            </LayoutTemplate>

            <ItemTemplate>
            <h2><asp:Label ID="lbName" runat="server" /></h2>
            <asp:ListView ID="rptContent2" runat="server" ItemPlaceholderID="itemPlaceHolder2" OnItemDataBound="ContentBound">
                <LayoutTemplate>      
                    <asp:PlaceHolder ID="itemPlaceHolder2" runat="server" />
                </LayoutTemplate>

                <ItemTemplate>
                    <div style="padding: 5px;" >                               
                        <div>
                        <asp:Label ID="lbID" runat="server" Text='<%# Eval("ID") %>' Visible="false" />

                        <asp:Label ID="lbWrapper" runat="server" Text='<%# Eval("Wrapper") %>' Visible="false" />

                        <asp:Label ID="lbAudioQuestion" runat="server" Visible="false" />

                        <div style="text-align: right;"><asp:Label ID="lbAudioAnswer" runat="server" Visible="false" /></div>

                        <div style="text-align: right;"><asp:TextBox ID="tbAnswer" runat="server" TextMode="MultiLine" CssClass="tb" Width="50%" Height="60" Visible="false" /></div>

                        <div style="margin-left: 20px;"><asp:RadioButtonList ID="rblChoice" runat="server" Visible="false" /></div>                               
                        </div>                                
                    </div>
                </ItemTemplate>
            </asp:ListView>

            <asp:Panel ID="pnFeedback" runat="server" Visible="false" CssClass="Feedback">
                <div style="margin: 10px;">
                    <div style="float: left;">
                    Score: <br /><asp:TextBox ID="tbScore" runat="server" CssClass="tb" Width="40" /> / <asp:Label ID="lbScore" runat="server" /></div>
                    </div>
                    <asp:Label ID="lbResultID" runat="server" Visible="false" />
                    <div style="float: left;">
                    Feedback<br />
                    <asp:TextBox ID="tbFeedback" runat="server" TextMode="MultiLine" CssClass="tb" Width="200" Height="60" />
                    </div>

                    <div style="float: left;">
                    Recording<br />
                    <asp:Label ID="lbFeedbackAudio" runat="server" />
                    </div>
                    <br style="clear: both;" />
                </div>
            </asp:Panel>

            </ItemTemplate>
            <EmptyItemTemplate>
                <div>
                    Sorry, no exercise is found for this lesson.
                </div>
            </EmptyItemTemplate>

        </asp:ListView>


        <asp:DataPager ID="DataPager" runat="server" PagedControlID="rptExercise2" 
            PageSize="1">
            <Fields>
                <asp:NextPreviousPagerField ButtonType="Button" ShowFirstPageButton="True" 
                    ShowNextPageButton="False" ShowPreviousPageButton="False" />
                <asp:NumericPagerField ButtonCount="10" ButtonType="Button" />
                <asp:NextPreviousPagerField ButtonType="Button" ShowLastPageButton="True" 
                    ShowNextPageButton="False" ShowPreviousPageButton="False" />
            </Fields>
        </asp:DataPager>  

public partial class MyPracClass : System.Web.UI.Page
{
    // Current page number
    int i = 0;

    protected classLessons les;
    protected classExercises exc;
    protected List<classExercises> exercises;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request["LessonID"] == null)
        {
            return;
        }

        classLessons les = new classLessons(Convert.ToInt32(Request["LessonID"]));
        exc = new classExercises();
        exercises = exc.GetExercisesByLessonID(les.ID);

        if (!IsPostBack)
        {
            lbLessonName.Text = les.Name;
            lbDescription.Text = les.Description;
            //SetPageNumber();

            rptExercise2.DataSource = exercises;
            rptExercise2.DataBind();
        }
    }

    protected void ExerciseBound(object sender, ListViewItemEventArgs e)
    {
        if (e.Item.ItemType == ListViewItemType.DataItem)
        {
            ListViewDataItem item = (ListViewDataItem)e.Item;

            Label lbName = (Label)item.FindControl("lbName");
            string excName = "Exercise " + (i+1) + ": " + exercises[i].Subject;
            lbName.Text = excName;

            Label lbFeedbackAudio = (Label)item.FindControl("lbFeedbackAudio");
            classContent ct = new classContent();
            string test = "exercise" + exercises[i].ID + "student" + Session["StudentID"] + "feedback";
            lbFeedbackAudio.Text = ct.GetPlayingString("exercise" + exercises[i].ID + "student" + Session["StudentID"] + "feedback");

            ListView rptContent2 = (ListView)item.FindControl("rptContent2");
            Panel pnFeedback = (Panel)item.FindControl("pnFeedback");

            if (exercises[i] != null && rptContent2 != null)
            {
                classContent cct = new classContent();

                rptContent2.DataSource = cct.GetContentByExerciseID(exercises[i].ID);
                rptContent2.DataBind();
            }

            var result = new classResults();

            result.GetResultByExerciseID(Convert.ToInt32(exercises[i].ID));

            if (result.ID > 0)
            {
                pnFeedback.Visible = true;
                Label lbID = (Label)item.FindControl("lbResultID");
                Label lbScore = (Label)item.FindControl("lbScore");
                lbScore.Text = exercises[i].Score.ToString();
                TextBox tbScore = (TextBox)item.FindControl("tbScore");
                TextBox tbFeedback = (TextBox)item.FindControl("tbFeedback");

                lbID.Text = result.ID.ToString();
                tbScore.Text = result.Score.ToString();
                tbFeedback.Text = result.Feedback;
            }
        }
    }

    protected void ChangeExercise(object sender, PagePropertiesChangingEventArgs e)
    {
        // Get current page number
        i = e.StartRowIndex / e.MaximumRows;

        rptExercise2.DataSource = exercises;
        rptExercise2.DataBind();
    }

    //protected void DataPager_PreRender(object sender, EventArgs e)
    //{
    //    // Get current page number
    //    //i = e.StartRowIndex / e.MaximumRows;

    //    rptExercise2.DataSource = exercises;
    //    rptExercise2.DataBind();
    //}

    protected void ContentBound(object sender, ListViewItemEventArgs e)
    {
        if (e.Item.ItemType == ListViewItemType.DataItem)
        {
            ListViewDataItem dataItem = (ListViewDataItem)e.Item;
            var qry = (classContent)dataItem.DataItem;

            Label lbAudioQuestion = (Label)e.Item.FindControl("lbAudioQuestion");
            Label lbAudioAnswer = (Label)e.Item.FindControl("lbAudioAnswer");
            Label lbWrapper = (Label)e.Item.FindControl("lbWrapper");
            TextBox tbAnswer = (TextBox)e.Item.FindControl("tbAnswer");
            RadioButtonList rblChoice = (RadioButtonList)e.Item.FindControl("rblChoice");

            switch (qry.Type)
            {
                case classContent.ContentType.AudioQuestion:
                    lbAudioQuestion.Visible = true;
                    lbAudioQuestion.Text = qry.GetPlayingString(qry.Wrapper);
                    break;
                case classContent.ContentType.TextQuestion:
                    lbWrapper.Visible = true;
                    break;
                case classContent.ContentType.VideoQuestion:
                    lbWrapper.Visible = true;
                    break;
                case classContent.ContentType.AudioAnswer:
                    lbAudioAnswer.Visible = true;
                    lbAudioAnswer.Text = qry.GetRecordingString("content" + qry.ID + "student" + Session["StudentID"]);
                    break;
                case classContent.ContentType.TextAnswer:
                    tbAnswer.Visible = true;
                    break;
                case classContent.ContentType.ChoiceAnswer:
                    rblChoice.DataSource = qry.GetChoiceOptions(qry.ID);
                    rblChoice.DataTextField = "Value";
                    rblChoice.DataValueField = "IsAnswer";
                    rblChoice.DataBind();
                    rblChoice.Visible = true;
                    break;
                default: break; ;
            }

            classAnswers ans = new classAnswers();

            var contentAnswer = ans.GetAnswerByContentID(qry.ID);

            if (contentAnswer.ID > 0)
            {
                switch (qry.Type)
                {
                    case classContent.ContentType.TextAnswer:
                        tbAnswer.Text = contentAnswer.Answer;
                        break;
                    case classContent.ContentType.ChoiceAnswer:
                        rblChoice.SelectedIndex = Convert.ToInt32(contentAnswer.Answer);
                        break;
                    default: break; ;
                }
            }

        }

    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧城烟雨 2024-10-21 09:38:22

本文告诉您有关使用 DataPager 控件可能需要的一切 https://web.archive.org/web/20201202213937/https://www.4guysfromrolla.com/articles/011310-1.aspx(请参阅文章的最后部分)

该文章清楚地解释了如何在分页区域应用/修改单个页码。所以我认为你只需要编写一个 CSS 类来禁用当前页码的按钮/链接并启用其余页码。

希望这有帮助...

This article tells you everything you might need about utilizing a DataPager control https://web.archive.org/web/20201202213937/https://www.4guysfromrolla.com/articles/011310-1.aspx (see the last section of the article)

The article clearly explains about applying/modifying individual page numbers in the paging area. So I think you will simply have to write a CSS class to disable the current page number's button/link and enable the remaining page numbers.

Hope this helps...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文