为什么在listview和datapager中未正确启用页码

时间:2011-01-22 23:28:18

标签: c# asp.net listview datapager

我正在使用listview和datapager进行显示。我使用两级listview,其中内部listview项可以是6种类型。我使用自己的数据源。我只需要在每页显示1个内部listviewitem。它正确显示每个页面,但页面编号没有设置正确,例如,当点击第2页(其他)时,第2页编号未被禁用,而第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; ;
                }
            }

        }

    }

1 个答案:

答案 0 :(得分:1)

本文将向您介绍使用DataPager控件http://www.4guysfromrolla.com/articles/011310-1.aspx可能需要的所有内容(请参阅本文的最后一部分)

本文清楚地解释了在寻呼区域中应用/修改单个页码的问题。所以我认为你只需编写一个CSS类来禁用当前页码的按钮/链接并启用剩余的页码。

希望这会有所帮助......