首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用GridView的DropDownList of DataSource更新DataSource

使用GridView的DropDownList of DataSource更新DataSource
EN

Stack Overflow用户
提问于 2011-12-07 02:48:50
回答 1查看 1.8K关注 0票数 0

我正在使用V2005 C#。

我的.aspx页面中有一个.aspx,我能够使用.aspx中的DropDownList控件更新数据库。

DDL用于我的性别专栏:

代码语言:javascript
复制
    <asp:TemplateField HeaderText="Gender" SortExpression="Gender">
        <EditItemTemplate>
            <asp:DropDownList ID="DropDownList2" runat="server" SelectedValue='<%# Bind("Gender") %>'>
                <asp:ListItem>M</asp:ListItem>  
                <asp:ListItem>F</asp:ListItem>
            </asp:DropDownList>
        </EditItemTemplate>
        <ItemTemplate>
            <asp:Label ID="Label1" runat="server" Text='<%# Bind("Gender") %>'></asp:Label>
        </ItemTemplate>
    </asp:TemplateField>

在我的性别列的DDL EditItemTemplate中,我使用了硬编码的值,它可以工作。

但是,我尝试在另一列上实现DDL。这一次,我使用了一个SqlDataSource,它从另一个表中选择数据,而不是使用固定值,但是它们给了我一个错误:'DropDownList3' has a SelectedValue which is invalid because it does not exist in the list of items.Parameter name: value

我也试图实现SelectedValue='<%# Bind("MemberType") %>',但它没有工作。

下面是我的MemberType DDL EditItemTemplate的代码:

代码语言:javascript
复制
    <asp:TemplateField HeaderText="MemberType" SortExpression="MemberType">
        <EditItemTemplate>
            <asp:DropDownList ID="DropDownList3" runat="server" DataSourceID="SqlDataSource2"
                DataTextField="MemberType" DataValueField="MemberType" SelectedValue='<%# Bind("MemberType") %>'>
            </asp:DropDownList>
        </EditItemTemplate>
        <ItemTemplate>
            <asp:Label ID="Label2" runat="server" Text='<%# Bind("MemberType") %>'></asp:Label>
        </ItemTemplate>
    </asp:TemplateField>

有人知道这是怎么回事吗?

谢谢

EN

回答 1

Stack Overflow用户

发布于 2011-12-07 03:44:41

Well...maybe --这是一个愚蠢的问题,但SqlDatasource SELECT查询中返回的列表中的值实际上是值吗?一个常见的问题是,如果查询中没有空值,那么它就不在列表中,所以一旦切换到编辑模式,就会抛出一个错误,因为默认值为null。

我的库中有一个Misc函数,它接受DropDownList引用并添加一个空行。

编辑以添加一些代码:

我们使用Telerik控件,因此这是针对RadComboBox的,但是由于该控件只是扩展了DropDownList (我认为,它们可能扩展了TextBox.)添加空项的语法应该与下面的类似。

代码语言:javascript
复制
// If there is already an empty row, we will not add an aditional empty row
        RadComboBoxItem  emptyItem = null;
        emptyItem = rcbObject.FindItemByValue(null) ?? rcbObject.FindItemByValue("");

        if (emptyItem !=null)
            return;

        // Add a Empty(null) item to the dropdown list
        RadComboBoxItem item_null = new RadComboBoxItem();

        item_null.Text = "";
        item_null.Value = null;
        rcbObject.Items.Insert(0, item_null);

希望这能有所帮助!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8409815

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档