i have a to make a test with 25 question so i created a user control for Question and i create a page with list view that contain only the question user control
this is the cs of the question usercontrol
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();
}
}
}
}
and this is the asp of the page
<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>
and this is the c# of the page
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();
}
will i usually bind data in the datapager prerender method, if i am not using a user control it works fine, but if i use usercontrol the questions won't appear . wel i traced the problem i found that the usercontrol is intialised before the invoking lvQuestions_ItemDataBound method how i can fix that if i want to keep using the user control
Good thinking using the databinding in listview DataPager1_PreRender that would make the data query to database minimum. As for your question I can see that you could change the question user control to be be like this:
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();
}
}
and call the Reinitialize() in lvQuestions_ItemDataBound method like this:
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();
}
}
and this will work.