【问题标题】:How do I set the Selected Value of a DropDownList inside an EditItemTemplate inside a DetailsView using the codebehind?如何使用代码隐藏在 DetailsView 内的 EditItemTemplate 内设置 DropDownList 的选定值?
【发布时间】:2016-07-08 13:42:47
【问题描述】:

我的 ASP.NET (VB) 页面上有一个详细信息视图,其中列出了客户的详细信息(他们的姓名、地址等)。

“国家/地区”选项是一个下拉列表,从数据库表中提取。查看详细信息视图时,这一切都很好,您可以看到他们所在的国家/地区。

但是,当我单击 DetailsView 上的 Edit 按钮时,下拉列表会正确显示 db 表中的所有可用国家,但它没有选择值,因此不是选择其当前设置的国家,而是默认值到列表的首位。这是错误的,因为它不是它们当前设置的值。

使用代码隐藏(VB),我应该如何将下拉列表的选定值设置为他们当前设置的国家名称?在代码隐藏中,我已经读取了他们当前的国家名称并将其存储在一个名为strCurrentCountryName的字符串变量中

例如。

Response.Write "strCurrentCountryName: " & strCurrentCountryName

将显示:

strCurrentCountryName: United Kingdom

所以我现在需要将他们的strCurrentCountryName 值与下拉列表中的一个国家/地区名称相匹配,并将其设置为他们的 SelectedValue。

我的代码在前面(列出了数据库中的所有国家/地区):

<asp:TemplateField HeaderText="Country" SortExpression="address5">
<EditItemTemplate>
    <asp:SqlDataSource ID="SqlDataSourcecountryList" runat="server" ConnectionString="<%$ ConnectionStrings:PDConnectionString %>"
    ProviderName="<%$ ConnectionStrings:PDConnectionString.ProviderName %>"
    SelectCommand="SELECT country_name_short, country_name_long FROM country_list WHERE deleted = 0 ORDER BY country_name_long">
    </asp:SqlDataSource>
    <asp:DropDownList ID="DropDownListCountry" runat="server" CssClass="form-control" 
    DataSourceID="SqlDataSourcecountryList" DataTextField="country_name_long" DataValueField="country_name_short">
    </asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
    <asp:Label ID="Label1" runat="server" Text='<%# Bind("address5") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>

在代码隐藏中,我尝试了这个(在Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load 内):

DropDownListCountry.DataBind()
DropDownListCountry.Items.FindByText(strCurrentCountryName).Selected = True

这会给出以下错误消息:

'DropDownListCountry' is not declared. It may be inaccessible due to its protection level

我尝试使用这两行代码并将下拉列表放置在 EditItemTemplate 之外,并且效果很好:

DropDownListCountry.DataBind()
DropDownListCountry.Items.FindByText(strCurrentCountryName).Selected = True

果然,它确实正确地将他们的 Selected Value 设置为正确的国家/地区。

在我的代码隐藏 (vb) 中,我是否需要先以某种方式声明 DropDownListCountry

当放置在我的 asp:TemplateField 的 EditItemTemplate 中时,如何使这项工作?

【问题讨论】:

    标签: asp.net vb.net


    【解决方案1】:

    由于下拉菜单在模板内部,它不会在页面代码的任何地方静态声明,因此无法直接通过DropDownListCountry 引用它。考虑一下 - 如果可能,并且您有一个带有这些的网格视图模板,请下拉您要引用的哪一行?

    所以实际上这个下拉列表会根据它所在的容器模板分配一个不同的 ID。所以您需要在数据绑定时在模板中找到它,然后按照您的方式设置所选值:

    Dim ddl As DropDownList = CType(detailsViewId.FindControl("DropDownListCountry"), DropDownList)
    ddl.Items.FindByText(strCurrentCountryName).Selected = True
    

    重要的是确保在创建下拉控件后运行此代码。最合适的地方是在数据绑定发生之后。为此,请订阅DataBound 事件:

    <asp:DropDownList ID="DropDownList1" runat="server" OnDataBound="DropDownList1_DataBound">
    

    并在事件处理程序中运行代码:

    Protected Sub DropDownList1_DataBound(ByVal sender As Object, ByVal e As System.EventArgs)
        ' Code above goes here 
    End Sub
    

    【讨论】:

    • 嗨,使用这个(我删除了 = 符号之前的 ddl):Dim ddl As DropDownList = CType(DetailsViewCompany.FindControl("DropDownListCountry"), DropDownList) 产生 Object reference not set to an instance of an object 的错误
    • @Rich,你什么时候运行这段代码?即在页面生命周期的哪一部分?
    • Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load 内部(如果我的 DDL 不在 EditItemTemplate 中,这又可以工作)- 也许我的放置是错误的??
    • @Rich,是的,那是错误的。数据绑定尚未发生,因此下拉菜单不存在。您需要处理详情视图的DataBound 事件并将此代码放在那里
    • 我不熟悉那种处理方式,我得再google一下。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-08
    • 2012-12-28
    • 1970-01-01
    • 1970-01-01
    • 2012-06-05
    相关资源
    最近更新 更多