如何从gridview中的下拉列表中设置所选值? [英] How to set the selected value from a dropdownlist in a gridview?
问题描述
您好
在我的网页上,当我从gridview编辑一行时,会使用下拉列表来选择一个值。现在默认选择的值是数据源提供的列表中的第一个值。
我遇到的问题是我无法为DDL设置所选值。
在我的vb代码中,我尝试按如下方式设置所选值:
Hello
On my webpage when I edit a row from the gridview, a dropdownlist is used to select a value. Now the default selected value is the first value from the list provided by the datasource.
The problem I have is that I can't set the selected value for the DDL.
In my vb code I try to set the selected value as follows:
Protected Sub GridView1_RowEditing(ByVal sender As Object, ByVal e As GridViewEditEventArgs) Handles GridView1.RowEditing
Dim row As GridViewRow
Dim lstdrop As DropDownList
row = GridView1.Rows.Item(e.NewEditIndex())
lstdrop = row.FindControl("DropDownListOwner")
lstdrop.SelectedIndex = 3
End Sub
这里是我的asp代码的一部分:
Here is a part of my asp code:
<EditItemTemplate>
<asp:DropDownList ID="DropDownListOwner" runat="server" DataSourceID="SqlDataOwner" DataValueField="User_name" DataTextField="User_name" AutoPostBack="true">
</asp:DropDownList>
</EditItemTemplate>
单击行上的编辑时会出现以下错误:
System.NullReferenceException:未将对象引用设置为实例一个对象
When clicking edit on a row the following error occurs:
System.NullReferenceException: Object reference not set to an instance of an object
推荐答案
我认为这可能会有所帮助。
尝试将以下值添加到EditItemTemplate DropDownList项目。
SelectedValue ='<%#Bind(value)%>'AppendDataBoundItems = true
I think this might help.
Try adding the following values to the EditItemTemplate DropDownList item.
SelectedValue='<%#Bind("value")%>' AppendDataBoundItems=true
不确定是否重要,但尝试删除 NewEditIndex
Not sure if it matters, but try removing the parenthesis on NewEditIndex
这篇关于如何从gridview中的下拉列表中设置所选值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!