listView DataPager和自定义userControl
本文关键字:userControl 自定义 DataPager listView | 更新日期: 2023-09-27 18:11:13
我有一个25个问题的测试,所以我创建了一个用户控件的问题,我创建了一个页面列表视图,只包含问题的用户控件
这是问题usercontrol的cs
public partial class ucScoringQuestion : System.Web.UI.UserControl
{
public int CheckedIndex { get { return rbAnswers.SelectedIndex; } }
public ScoringQuestion scoringQuestion { get; set; }
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (scoringQuestion != null)
{
foreach (string item in scoringQuestion.GetAnswersAsList())
{
rbAnswers.Items.Add(new ListItem(item));
}
lblQuestion.Text = scoringQuestion.GetQuestionText();
}
}
}
}
这是页面的asp
<div style="margin: 0 auto; width: 900px;">
<asp:ListView ID="lvQuestions" runat="server" OnItemDataBound="lvQuestions_ItemDataBound">
<ItemTemplate>
<uc1:ucScoringQuestion runat="server" ID="ucScoringQuestion" />
</ItemTemplate>
</asp:ListView>
<asp:DataPager ID="DataPager1" runat="server" PagedControlID="lvQuestions" PageSize="5" OnDataBinding="DataPager1_DataBinding" ViewStateMode="Enabled" OnPreRender="DataPager1_PreRender" OnInit="DataPager1_Init" OnLoad="DataPager1_Load">
<Fields>
<asp:NextPreviousPagerField ShowNextPageButton="true" ShowPreviousPageButton="false" ShowFirstPageButton="false" ButtonType="Link" FirstPageText='Next' />
</Fields>
</asp:DataPager>
</div>
这是c#的页面
private int QuestionNumber = 0;
protected void Page_Load(object sender, EventArgs e)
{
//if (!IsPostBack)
//{
// RuwadEntitiesConnection ctx = new RuwadEntitiesConnection();
// lvQuestions.DataSource = ctx.ScoringQuestions.Where(x => x.QuestionStageID == 2).ToList();
// lvQuestions.DataBind();
//}
}
protected void lvQuestions_ItemDataBound(object sender, ListViewItemEventArgs e)
{
if (e.Item.ItemType == ListViewItemType.DataItem || e.Item.ItemType == ListViewItemType.InsertItem)
{
ucScoringQuestion ucQuestion = (ucScoringQuestion)e.Item.FindControl("ucScoringQuestion");
ucQuestion.scoringQuestion = (ScoringQuestion)e.Item.DataItem;
string pre = MyContext.CurrentLanguage == Languages.Arabic ? "<strong>س. </strong>" : "<strong>Q. </strong>";
ucQuestion.spnQuestionNumber.InnerHtml = pre + (++QuestionNumber).ToString() + " ";
}
}
protected void DataPager1_PreRender(object sender, EventArgs e)
{
RuwadEntitiesConnection ctx = new RuwadEntitiesConnection();
lvQuestions.DataSource = ctx.ScoringQuestions.Where(x => x.QuestionStageID == 2).ToList();
lvQuestions.DataBind();
}
我通常在datapager preender方法中绑定数据,如果我不使用用户控件,它工作得很好,但如果我使用usercontrol,问题就不会出现。好吧,我跟踪了这个问题,我发现在调用lvQuestions_ItemDataBound方法之前初始化了usercontrol如果我想继续使用用户控件
使用listview DataPager1_PreRender中的数据绑定,这将使对数据库的数据查询最小化。至于你的问题,我可以看到你可以把问题用户控件改成这样:
public int CheckedIndex { get { return rbAnswers.SelectedIndex; } }
public ScoringQuestion scoringQuestion { get; set; }
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
Reinitialize();
}
public void Reinitialize()
{
if (scoringQuestion != null)
{
foreach (string item in scoringQuestion.GetAnswersAsList())
{
rbAnswers.Items.Add(new ListItem(item));
}
lblQuestion.Text = scoringQuestion.GetQuestionText();
}
}
并像这样调用lvQuestions_ItemDataBound方法中的Reinitialize():
protected void lvQuestions_ItemDataBound(object sender, ListViewItemEventArgs e)
{
if (e.Item.ItemType == ListViewItemType.DataItem || e.Item.ItemType == ListViewItemType.InsertItem)
{
ucScoringQuestion ucQuestion = (ucScoringQuestion)e.Item.FindControl("ucScoringQuestion");
ucQuestion.scoringQuestion = (ScoringQuestion)e.Item.DataItem;
string pre = MyContext.CurrentLanguage == Languages.Arabic ? "<strong>س. </strong>" : "<strong>Q. </strong>";
ucQuestion.spnQuestionNumber.InnerHtml = pre + (++QuestionNumber).ToString() + " ";
ucQuestion.Reinitialize();
}
}