2013-07-22 27 views
0

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

我收到以下錯誤:

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

this的問題,我才知道,#應增加,但當我添加爲:

它不接受這(在整個聲明下面顯示藍線)。

請告訴我我在哪裏犯錯。

請幫幫我。

我使用的是vb.net,但在c#中的回答也很有幫助。

+0

@KevinKunderman我已經在我的問題提到這一點,我haqve refered這個問題 –

+0

你能對整個電網 –

回答

1

你可以嘗試設置基於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" %>" /> 
+0

感謝名單添加標記,這是最簡單的方法:) –

+0

但是,這始終顯示我單行文本框,無論什麼是文字。例如。如果它的多行,它顯示我單行+多行,如果它的圖像,它顯示我單行+圖像上傳 –

+0

奇怪,張貼GridView的標記,以及填充它的代碼。 – Riv

1

像錯誤說,你不能有一個Eval外綁定控件的數據,所以我建議你動態插入控制成PlaceHolder控制,如下所示:

標記:

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

代碼隱藏:

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 

注:LabelType在上面的代碼是你Eval("LabelType").ToString()在做什麼的字符串表示。