如何在内联 if 语句中使用数据绑定记录

发布于 2024-08-24 05:48:05 字数 830 浏览 15 评论 0原文

这是我的问题:

我的 asp.net (VB) 上有一个中继器:

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

现在我想做的是,检查一个我没有使用过的值,称为“Question_Type”,它可能是 = 1、2 或 3取决于它是否是多项选择、简答题等。

我已经尝试过:

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

并且收到此错误:

数据绑定方法(例如 Eval()、XPath() 和 Bind() 只能在数据绑定控件的上下文中使用) 。

我如何在 if 语句中使用这个值???

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

表情可笑 2024-08-31 05:48:05

您将需要处理 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);
            }
        }
    }
}

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);
            }
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文