与ObjectDataSource的DropDownList中的C#的SelectedValue [英] C# selectedvalue of dropdownlist with ObjectDataSource

查看:142
本文介绍了与ObjectDataSource的DropDownList中的C#的SelectedValue的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何设置的DropDownList的SelectedValue的,如果DropDownList的是
的GridView内的DropDownList被填充的ObjectDataSource单击编辑
按钮时?

How do i set selectedvalue of dropdownlist if dropdownlist is within Gridview and dropdownlist is populated by objectdatasource when EDIT button is clicked?

发生在我的应用是DropDownList中被填充
,而第一项始终显示为SelectedValue的。

What happens to my app is dropdownlist gets populated but the first item always shows up as selectedvalue.

推荐答案

标记:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="Id" DataSourceID="ObjectDataSource1" OnRowDataBound="GridView1_RowDataBound">
        <Columns>
            <asp:CommandField ShowEditButton="True" />
            <asp:CommandField ShowSelectButton="True" />
            <asp:BoundField DataField="Id" HeaderText="Id" SortExpression="Id" />
            <asp:BoundField DataField="Firstname" HeaderText="Firstname" SortExpression="Firstname" />
            <asp:BoundField DataField="Lastname" HeaderText="Lastname" SortExpression="Lastname" />
            <asp:TemplateField HeaderText="Age">
                   <ItemTemplate>
                       <%#Eval("Age") %>
                   </ItemTemplate>
                <EditItemTemplate>
                    <asp:DropDownList runat="server" ID="ddlAge">
                        <Items>
                            <asp:ListItem Text="10" Value="10"></asp:ListItem>
                            <asp:ListItem Text="20" Value="20"></asp:ListItem>
                            <asp:ListItem Text="30" Value="30"></asp:ListItem>
                            <asp:ListItem Text="40" Value="40"></asp:ListItem>
                            <asp:ListItem Text="50" Value="50"></asp:ListItem>
                            <asp:ListItem Text="60" Value="60"></asp:ListItem>
                            <asp:ListItem Text="70" Value="70"></asp:ListItem>
                            <asp:ListItem Text="80" Value="80"></asp:ListItem>
                            <asp:ListItem Text="90" Value="90"></asp:ListItem>
                        </Items>
                    </asp:DropDownList>
                </EditItemTemplate>
            </asp:TemplateField>
        </Columns>

    </asp:GridView>
    <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" SelectMethod="GetAll" TypeName="OdsSelectedItem.App_Data.StudentsBll"></asp:ObjectDataSource>



代码:

Code:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Student item = e.Row.DataItem as Student;
            if (item != null)
            {
                var ddl = e.Row.FindControl("ddlAge") as DropDownList;
                if (ddl == null) return;
                ddl.SelectedValue = item.Age.ToString();
            }

        }
    }



坏榜样,但我认为它显示了正确的方向:)

poor example, but i think it shows into the right direction :)

这篇关于与ObjectDataSource的DropDownList中的C#的SelectedValue的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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