开发者

How to use databind records in inline if statements

开发者 https://www.devze.com 2022-12-22 07:53 出处:网络
here is my problem: I\'ve got a repeater on my asp.net (VB): <asp:Repeater ID=\"Repeater1\" runat=\"server\">

here is my problem:

I've got a repeater 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 a value that I haven't used called "Question_Type" which could be = 1, 2 or 3 depending if it is multiple choice, short answer, etc.

I have 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:Rad开发者_运维知识库ioButton>")

end if
%>

and I get this error:

Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.

HOW can I use this value in a if statement???


You are going to need to handle the ItemDataBound event and manually handle the values there.

Here is how I might approach the problem given this repeater:

<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>

Here is my event handler for getting the possible answers to a radio button list:

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

精彩评论

暂无评论...
验证码 换一张
取 消