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

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

问题描述

这件code的

<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.

然而,这是为数据绑定GridView的法律和常用编辑的模板。在的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.

推荐答案

标记中使用的SelectedValue ='&LT;%#32%>语法(请注意下面的例子):

in markup use SelectedValue='<%# "32" %> syntax .(note the following example ):

 <asp:DropDownList  ID="ddlField" SelectedValue='<%# "32" %>' 
   runat="server"      DataTextField="Name" DataValueField="ID"  >
  </asp:DropDownList>

或code-背后刚过数据绑定(例如):

or in code-behind just after DataBinding .(example):

ddlField.DataSource = Fields.SelectAll();
        ddlField.DataBind();           
        ddlField.SelectedValue = "32";

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

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