ASP:ItemTemplate中的DropDownList:为什么允许SelectedValue属性? [英] ASP:DropDownList in ItemTemplate: Why is SelectedValue attribute allowed?

查看:292
本文介绍了ASP:ItemTemplate中的DropDownList:为什么允许SelectedValue属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这段代码

<asp:DropDownList runat="server" ID="testdropdown" SelectedValue="2">
    <asp:ListItem Text="1" Value="1"></asp:ListItem>
    <asp:ListItem Text="2" Value="2"></asp:ListItem>
    <asp:ListItem Text="3" Value="3"></asp:ListItem>
</asp:DropDownList>

产生此错误:


SelectedValue属性不能以声明方式设置

The 'SelectedValue' property cannot be set declaratively.

然而,这是一个合法且通常的使用编辑模板的数据绑定GridViews。 SelectedValue 属性当然似乎是在这里声明性地设置。

Yet, this is a legal and commonly used edit template for databound GridViews. The SelectedValue attribute certainly appears to be declaratively set here.

<EditItemTemplate>
    <asp:DropDownList runat="server" 
        ID="GenreDropDownList"
        DataSourceID="GenreDataSource" 
        DataValueField="GenreId"
        DataTextField="Name"
        SelectedValue='<%# Bind("Genre.GenreId") %>'>
    </asp:DropDownList>
</EditItemTemplate>

问题是:当您允许声明性地设置这些情况时,有什么区别?你不是在哪?

The question is: what is the difference between the cases when you are allowed to set it declaratively and those in which you are not? The error message implies that it's never allowed.

推荐答案

这意味着您无法通过设计器进行设置。

It means you cannot set it through the designer.

正确的方法是:

<asp:DropDownList runat="server" ID="testdropdown">
    <asp:ListItem Text="1" Value="1"></asp:ListItem>
    <asp:ListItem Text="2" Value="2" Selected></asp:ListItem>
    <asp:ListItem Text="3" Value="3"></asp:ListItem>
</asp:DropDownList>

绑定方法的工作原因是因为在设计模式下但在运行时未选择该值控件绑定到数据源

The reason the bound method works is because the value isn't selected in design mode but at runtime after the control is bound to a datasource

DropDownList.SelectedValue方法意在在运行时应用,因此无法将其设置为装饰性的错误

The DropDownList.SelectedValue method is meant to be applied at runtime hence the error about not being able to set it 'decoratively'

这篇关于ASP:ItemTemplate中的DropDownList:为什么允许SelectedValue属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆