views:

294

answers:

1

Hi everyone, 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:RadioButton>")

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

A: 

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);
            }
        }
    }
}
Josh
Thank you, I was able to do that. Now can I bug you furthermore and ask how I would approach binding each radiobutton (if it is shown, IE Question_Type =1) to a different table that retrieves a list of all possible values
AllStar11
I would recomend using a Placeholder in your repeater, and during your ItemDataBind event you could spin up a new RadioButtonList, add it to the placeholder's controls collection, and then set its datasource to the data of your choice. I will work up and example and post it shortly.
Josh
Hey, thanks a lot. I appreciate you taking the time.
AllStar11
You'll notice I used an Enum instead of an Int in my example, and custom objects. You might be using data tables, but should be able to get the general idea.
Josh
Thanks again. I've modified your example but maintained the same concept. I am having a problem updating the placeholder that I have with the updated version in the HandleQuestionType function. I know that all of the radio buttons and list populate correctly by debugging, however, it seems like the placeholder is not being updated. Any suggestions?
AllStar11
Make sure you have the runat="server" tag in there, and that you are for sure adding the RadioButtonList to the controls collection of the placeholder. If you want you can follow my profile to my blogger account and profile, shoot me an email with your source and I can see what is going on.
Josh