gpt4 book ai didi

c# - listView DataPager 和自定义 userControl

转载 作者:行者123 更新时间:2023-11-30 16:58:54 25 4
gpt4 key购买 nike

我要用 25 个问题进行测试,所以我为问题创建了一个用户控件,并创建了一个 ListView 页面,其中仅包含问题用户控件

这是问题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 prerender 方法中绑定(bind)数据吗,如果我不使用用户控件,它工作正常,但如果我使用用户控件,问题将不会出现。好吧,我追踪了问题,我发现用户控件在调用 lvQuestions_ItemDataBound 方法之前被初始化如果我想继续使用用户控件,我该如何解决这个问题

最佳答案

在 ListView DataPager1_PreRender 中使用数据绑定(bind)的好主意,这将使对数据库的数据查询最少。至于您的问题,我可以看到您可以将问题用户控件更改为如下所示:

  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();
}
}

这会起作用。

关于c# - listView DataPager 和自定义 userControl,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24552096/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com