mardi 30 octobre 2018

Get user control was loaded dynamically keep null

the problem is : i loaded a user control in to a PlaceHolder and appear to the front-end successfully with binded data as i need , i am trying to get it to access method but PlaceHolder controls count is equals 0

Exception : plhUserControl.Controls[0]

Question Page :

 <div class="container">
        <div class="content2">
            <div class="content3">
                <asp:PlaceHolder runat="server" ID="plhUserControl" />
                <asp:Button ID="btnNext" Text="Next" CssClass="btn btn-primary nextquestionbutton" runat="server" OnClick="btnNext_Click"  />

            </div>
        </div>
    </div>



private void vFillPage()
{
    OperationsQuestion oOperationsQuestion = new OperationsQuestion();
    var Question = oOperationsQuestion.GetQuestionById(Convert.ToInt32(Request.QueryString["Qid"]));
    //this.QuestionTypeViewState = (int)Question.QuestionType;
    if ((EnumQuestionTypes)Question.QuestionType == EnumQuestionTypes.RadioButtonQuestion)
    {
        plhUserControl.Controls.Add((RadioButtonQuestion)Page.LoadControl("~/UserControls/RadioButtonQuestion.ascx"));
    }
}

protected void btnNext_Click(object sender, EventArgs e)
{

    OperationsQuestion oOperationsQuestion = new OperationsQuestion();
    var UserControl = plhUserControl.Controls[0];

    if (UserControl.GetType()==typeof(RadioButtonQuestion))
    {
        var RadioUserControl = (RadioButtonQuestion)UserControl;
        var list = RadioUserControl.Selected();
        oOperationsQuestion.AddOrUpdateAnswerRadioOrCheckBox(list,
            Convert.ToInt32(Request.QueryString["Qid"]),
            Convert.ToInt32(Request.QueryString["UserId"])
            );
    }
}


protected override void OnInit(EventArgs e)
{
    if (!IsPostBack)
    {
        vFillPage();
    }

    base.OnInit(e);



}

User Control :

<asp:ListView ID="lvRadioButton" runat="server" DataKeyNames="QuestionAnswerId">
    <ItemTemplate>
        <asp:RadioButton ID="rd" CssClass="CheckBoxRadioButton form-control" GroupName="RadioButtonQuestionGroup" onclick="SetUniqueRadioButton('RadioButtonQuestionGroup',this)" Text='<%# Request.QueryString["lang"]=="en"? Eval("QuestionAnswerTextEn"):Eval("QuestionAnswerTextAr") %>' runat="server" />
    </ItemTemplate>
</asp:ListView>




Aucun commentaire:

Enregistrer un commentaire