C# 数据绑定到网格时出错

C# 数据绑定到网格时出错,c#,asp.net,.net,vb.net,visual-studio-2008,C#,Asp.net,.net,Vb.net,Visual Studio 2008,我在gridview中有以下代码: <% If Eval("LabelType").ToString() = "Singleline" Then%> <asp:TextBox ID="txtSingleLine" runat="server" ></asp:TextBox> <% End If%> <% If Eval("LabelType").ToString() = "Multiline" Then%> <asp:Te

我在gridview中有以下代码:

 <% If Eval("LabelType").ToString() = "Singleline" Then%>  <asp:TextBox ID="txtSingleLine" runat="server" ></asp:TextBox> <% End If%>
 <% If Eval("LabelType").ToString() = "Multiline" Then%>  <asp:TextBox ID="txtMultiline" runat="server"  TextMode="MultiLine" ></asp:TextBox> <% End If%>                                            
  <% If Eval("LabelType").ToString() = "Image" Then%>  <asp:FileUpload ID="FileUpload1" runat="server" /> <% End If%>
问题我知道应该添加#,但当我添加为时:

它不接受这一点(在整个声明下方显示蓝线)

请告诉我哪里出错了

请帮帮我


我正在使用vb.net,但c#中的答案也很有用。

您可以尝试根据LabelType的值设置每个控件的可见性,如下所示:

<asp:TextBox ID="txtSingleLine" runat="server" Visible="<%# Eval("LabelType").ToString() == "Singleline" %>"></asp:TextBox>
<asp:TextBox ID="txtMultiline" runat="server"  TextMode="MultiLine"  Visible="<%# Eval("LabelType").ToString() == "Multiline" %>" ></asp:TextBox>
<asp:FileUpload ID="FileUpload1" runat="server"  Visible="<%# Eval("LabelType").ToString() == "Image" %>" />

如错误所述,您不能将
Eval
置于数据绑定控件之外,因此我建议您将控件动态插入到
占位符
控件中,如下所示:

标记:

<asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>

注意:
LabelType
在上面的代码中是您在
Eval(“LabelType”)中所做操作的字符串表示形式。ToString()

@KevinKunderman我在我的问题中已经提到了这一点,我已经提到了这个问题,您可以为整个gridThanx添加标记吗,这是最简单的方法:)但是,这总是显示单行文本框,无论文本是什么。如果它是多行的,它向我显示单行+多行,如果它的图像,它向我显示单行+图像上传,发布gridview的标记,以及填充它的代码。是的,完成了,这是我的错误。谢谢,有用的回答:)
If LabelType = "Singleline" Then
    ' Create textbox and add to placeholder
    Dim textbox = New TextBox()
    textbox.ID = "txtSingleLine"
    PlaceHolder1.Controls.Add(textbox)
Else If LabelType = "Multiline" Then
    ' Create textbox with multi-line text mode and add to placeholder
    Dim multilinetextbox = New TextBox()
    multilinetextbox.ID = "txtMultiline"
    PlaceHolder1.Controls.Add(multilinetextbox)
Else If LabelType = "Image" Then
    ' Create file upload and add to placeholder
    Dim fileupload = New FileUpload()
    fileupload.ID = "FileUpload1"
    PlaceHolder1.Controls.Add(fileupload)
End If