繁体   English   中英

使用ObjectDataSource的下拉列表的C#selectedvalue

[英]C# selectedvalue of dropdownlist with ObjectDataSource

如果下拉列表在Gridview中,并且在单击EDIT按钮时由objectdatasource填充下拉列表,如何设置下拉列表的选定值?

我的应用程序会发生什么,dropdownlist会被填充,但第一项始终显示为selectedvalue。

在datagrid视图中,行数据绑定事件执行此操作

 if (e.Row.RowType == DataControlRowType.DataRow)
        {
            DataRowView dRowView1 = (DataRowView)e.Row.DataItem;
            if ((e.Row.RowState= DataControlRowState.Edit) > 0)
            {

              DropDownList YourdropDown = (DropDownList)e.Row.FindControl("YourdropDown") as DropDownList;
                if (YourdropDown!=null){
              YourdropDown.SelectedValue = dRowView1["ID"].ToString();
                 }
            }
         }

标记:

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

码:

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();
            }

        }
    }

可怜的例子,但我认为它显示正确的方向:)

我今天遇到了类似的问题。 我的问题是我需要将名称列表中的选定值绑定到下拉列表。 但我必须将一个“选定值”传递给obj数据源以填充下拉列表。

你不能绑定“selectedValue”并使用它将数据传递给将填充该下拉列表的函数。

这是我的解决方案:

             <asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="ObjectDataSource1" DataTextField="Text" DataValueField="Value" ToolTip='<%#Eval("ID") %>' SelectedValue='<%# Bind("ManagerID") %>'>
                            </asp:DropDownList>
                            <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" SelectMethod="getallIDs" TypeName="MyClass" OldValuesParameterFormatString="original_{0}">
                                <SelectParameters>
                                    <asp:ControlParameter ControlID="DropDownList1" Name="ID" PropertyName="ToolTip" Type="Int32" />
                                </SelectParameters>
                            </asp:ObjectDataSource>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM