在 RowDatabound vb.net GridView 中使用 DataBind 时出错
Error while using DataBind in RowDatabound vb.net GridView
请看下面的代码。我试图在 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>
该错误表明您试图在不支持数据绑定的 属性 中使用 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
属性 已在代码隐藏中设置为 column/field 名称,您还应将其设置到具有相同名称的标记中,即 CODE_NAME
.
如果您想在相应行的每个下拉列表中显示默认选择值,Eval
部分应该放在 SelectedValue
属性 中。
ASP.Net TemplateField 的 ItemTemplate 中带有 DropDownList 的 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
只删除条件
If e.Row.RowState = DataControlRowState.Edit Then
请看下面的代码。我试图在 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>
该错误表明您试图在不支持数据绑定的 属性 中使用 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
属性 已在代码隐藏中设置为 column/field 名称,您还应将其设置到具有相同名称的标记中,即 CODE_NAME
.
如果您想在相应行的每个下拉列表中显示默认选择值,Eval
部分应该放在 SelectedValue
属性 中。
ASP.Net TemplateField 的 ItemTemplate 中带有 DropDownList 的 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
只删除条件
If e.Row.RowState = DataControlRowState.Edit Then