How to use data binding records in inline if operations

here is my problem:

I have a relay on my asp.net (VB):

<asp:Repeater ID="Repeater1" runat="server">    
<ItemTemplate>
  <asp:Label ID="Label1" runat="server" Text='<%# Eval("Question_Number") %>' /> 
  <%#Eval("Question_Desc")%>

Now, what I want to do is check the value that I did not use, called "Question_Type", which can be = 1, 2 or 3 depending on whether it is multiple choice, short answer, etc.

I tried this:

<%  
if Eval("Question_type") = 1 then

Response.Write(" <asp:RadioButton runat=""server"">test1</asp:RadioButton>")
Response.Write(" <asp:RadioButton runat=""server"">test2</asp:RadioButton>")
Response.Write(" <asp:RadioButton runat=""server"">test3</asp:RadioButton>")

end if
%>

and I get this error:

Data binding methods such as Eval (), XPath (), and Bind () can only be used in the context of database management.

HOW can I use this value in an if statement?

+3
source share
1 answer

ItemDataBound .

, :

<asp:Repeater ID="Repeater1" runat="server" OnItemDataBound="HandleQuestionType">
    <ItemTemplate>
        <asp:Label ID="Label1" runat="server" Text='<%# Eval("Question_Number") %>' />
        <%#Eval("Question_Desc")%>
        <asp:PlaceHolder ID="phQuestions" runat="server" />
    </ItemTemplate>
</asp:Repeater>

:

protected void HandleQuestionType(object sender, RepeaterItemEventArgs e)
{
    // Execute the following logic for Items and Alternating Items.
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
        var question = e.Item.DataItem as Question;
        var placeHolder = e.Item.FindControl("phQuestions") as PlaceHolder;

        if(question != null && placeHolder != null)
        {
            if(question.Question_Type == QuestionTypeEnum.MultipleChoice)
            {
                var radioList = new RadioButtonList
                                    {
                                        DataTextField = "Answer",
                                        DataValueField = "ID",
                                        DataSource = GetPossibleAnswers()
                                    };

                radioList.DataBind();

                placeHolder.Controls.Add(radioList);
            }
        }
    }
}
+1

Source: https://habr.com/ru/post/1736364/


All Articles