请参见下面的代码。我正在尝试在GridView控件中进行编辑时填充下拉列表。
Private Function GetSiteSelection() As DataTableReader
''' some code to return DataTableReader
End Function
Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs) Handles GridViewAttachedStation.RowDataBound
If e.Row.RowType = DataControlRowType.DataRow Then
Dim dtrSiteSel As Data.DataTableReader = Nothing
If e.Row.RowState = DataControlRowState.Edit Then
Dim SiteName As DropDownList = DirectCast(e.Row.FindControl("DropDownListType"), DropDownList)
SiteName.DataSource = GetSiteSelection()
SiteName.DataTextField = "CODE_NAME"
SiteName.DataValueField = "CODE_ID"
SiteName.DataBind() <-- Error is here
End If
End If
End Sub
在SiteName.DataBind()处获取错误
Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.
按要求的样机:这适用于简单的文本框,但不适用于下拉菜单。我提到了其他来源,但大多数来源都使用相同的方式-无法正常工作。
<asp:GridView ID="GridView1"
runat="server"
AutoGenerateColumns="False"
Width="100%"
CellPadding="0"
BorderStyle="None"
AllowSorting="true"
OnRowEditing="OnRowEditing">
<AlternatingRowStyle CssClass="tblAtlData"></AlternatingRowStyle>
<RowStyle ForeColor="Black" CssClass="tblData"></RowStyle>
<FooterStyle CssClass="tblHeader"></FooterStyle>
<PagerStyle Font-Bold="True" HorizontalAlign="Left" ForeColor="BlueViolet" CssClass="tblData"></PagerStyle>
<HeaderStyle CssClass="tblHeader" ForeColor="White"></HeaderStyle>
<Columns>
<asp:TemplateField HeaderText="EDIT">
<ItemTemplate>
<asp:LinkButton ID="EditLinkButton" Text="Edit" Font-Bold="true" CommandName="Edit" runat="server"
CommandArgument='<%# Eval("STATION ID") %>' />
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" />
<HeaderStyle Width="5%" />
<EditItemTemplate>
<asp:LinkButton ID="UpdateLinkButton" Text="Update" Font-Bold="true" CommandName="Update" runat="server"
CommandArgument='<%# Eval("STATION ID") %>' />
<asp:LinkButton ID="CancelLinkButton" Text="Cancel" Font-Bold="true" runat="server" OnClick="OnCancel" />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Site Selection Type">
<ItemTemplate>
<asp:Label ID="lblSiteSelection" runat="server" Text='<%# Eval("Site") %>' />
</ItemTemplate>
<ItemStyle Width="10%" CssClass="GeneralText" />
<HeaderStyle Width="10%" />
<EditItemTemplate>
<asp:DropDownList runat="server" ID="DropDownListTypeNameRow" CssClass="GeneralText"
onkeydown="typeAhead()" AutoPostBack="true" DataTextField='<%# Eval("SiteSelectionType") %>' />
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
答案 0 :(得分:0)
该错误表明您尝试在不支持数据绑定的属性中使用DataBinder.Eval
方法。
此DropDownList
设置应该可以解决以下问题:
<asp:DropDownList runat="server" ID="DropDownListTypeNameRow" CssClass="GeneralText"
onkeydown="typeAhead()" AutoPostBack="true"
DataTextField="CODE_NAME" DataValueField="CODE_ID"
SelectedValue='<%# Eval("SiteSelectionType") %>' />
由于DataTextField
属性已经在代码后面设置为列/字段名称,因此您还应该将其设置为具有相同名称的标记,即CODE_NAME
。
如果要在对应行的每个下拉列表中显示默认的选定值,则应将Eval
部分放在SelectedValue
属性中。
答案 1 :(得分:0)
在TemplateField的ItemTemplate中具有DropDownList的ASP.Net GridView。
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnRowDataBound="OnRowDataBound">
<Columns>
<asp:BoundField HeaderText="Name" DataField="ContactName" />
<asp:TemplateField HeaderText = "Country">
<ItemTemplate>
<asp:Label ID="lblCountry" runat="server" Text='<%# Eval("Country") %>' Visible = "false" />
<asp:DropDownList ID="ddlCountries" runat="server">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not IsPostBack Then
GridView1.DataSource = GetData("SELECT ContactName, Country FROM Customers")
GridView1.DataBind()
End If
End Sub
Private Function GetData(query As String) As DataSet
Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Dim cmd As New SqlCommand(query)
Using con As New SqlConnection(conString)
Using sda As New SqlDataAdapter()
cmd.Connection = con
sda.SelectCommand = cmd
Using ds As New DataSet()
sda.Fill(ds)
Return ds
End Using
End Using
End Using
End Function
Protected Sub OnRowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If (e.Row.RowType = DataControlRowType.DataRow) Then
'Find the DropDownList in the Row.
Dim ddlCountries As DropDownList = CType(e.Row.FindControl("ddlCountries"), DropDownList)
ddlCountries.DataSource = GetData("SELECT DISTINCT Country FROM Customers")
ddlCountries.DataTextField = "Country"
ddlCountries.DataValueField = "Country"
ddlCountries.DataBind()
'Add Default Item in the DropDownList.
ddlCountries.Items.Insert(0, New ListItem("Please select"))
'Select the Country of Customer in DropDownList.
Dim country As String = CType(e.Row.FindControl("lblCountry"), Label).Text
ddlCountries.Items.FindByValue(country).Selected = True
End If
End Sub
答案 2 :(得分:0)
只需删除条件
If e.Row.RowState = DataControlRowState.Edit Then