在下拉列表OnRowEditing上设置值

时间:2014-07-01 10:07:50

标签: c# asp.net gridview

我在gridview中有一个下拉列表。我希望DDL中的值在编辑模式下与在视图模式下相同,当gridview进入编辑模式时,DDL设置为第一个值的值为默认值。

我尝试在填充和绑定gridview之前获取值以进行编辑模式,但之后我得到null(nFolderId)。当我在绑定后尝试nFolderId片段时,我得到默认值(意味着第一个值)。

那么如何才能让DDL值从查看模式进入编辑模式?

protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
    GridView1.EditIndex = e.NewEditIndex;
    GridViewRow row = GridView1.Rows[e.NewEditIndex];
    var nFolderId = Convert.ToInt32(((DropDownList)row.FindControl("DropDownListFolders1")).SelectedValue);
    FillGridView();
    GridView1.DataBind();
}

我的aspx:

<asp:GridView ID="GridView1" runat="server" OnRowEditing="GridView1_RowEditing" DataKeyNames="strPositionId,tmVaRPosition,nFoldersId"
            AllowSorting="True" AutoGenerateColumns="False" SkinID="gridviewGridlinesSkin" OnRowCancelingEdit="GridView1_CancelingEdit"
            OnRowUpdated="GridView1_RowUpdated" OnRowUpdating="GridView1_RowUpdating" OnRowDeleted="GridView1_RowDeleted" OnRowDeleting="GridView1_RowDeleting"
            EmptyDataText="No positions found" OnRowDataBound="GridView1_RowDatabound">
            <Columns>
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:ImageButton ID="ibEdit" runat="server" AlternateText="Edit" CommandName="Edit"
                            ImageUrl="~/Images/icon_edit.gif" />
                        <asp:ImageButton ID="ibDelete" runat="server" AlternateText="Delete" CommandName="Delete"
                            ImageUrl="~/Images/icon_delete.gif" OnClientClick='<%# Eval("strPositionName", "return confirm(\"Are you sure you want to delete this entry {0}?\");") %>' />
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:ImageButton ID="ibUpdate" runat="server" AlternateText="Update" CommandName="Update" ImageUrl="~/Images/icon_ok.gif" />
                        <asp:ImageButton ID="ibCancel" runat="server" AlternateText="Cancel" CommandName="Cancel" ImageUrl="~/Images/icon_cancel.gif" />
                    </EditItemTemplate>
                    <ItemTemplate>
                         <asp:Label ID="Label3" runat="server" Text='<%# Eval("strFolderName") %>'></asp:Label>
                    </ItemTemplate>
                    <ItemStyle Wrap="False" />
                </asp:TemplateField>
                <asp:TemplateField>
                    <EditItemTemplate>
                        <BrummerComp:SortableDropDownList ID="DropDownListFolders1" Width="141px" runat="server"
                                SkinID="BCdropdownlistSkin"/>
                    </EditItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>

1 个答案:

答案 0 :(得分:0)

设置SelectedValue的值

<div>
       <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"     DataSourceID="SqlDataSource1"
    onrowdeleting="GridView1_RowDeleting">
    <Columns>
        <asp:BoundField DataField="dd" HeaderText="dd" SortExpression="dd" />
        <asp:TemplateField HeaderText="ddd" SortExpression="ddd">
            <ItemTemplate>
                <asp:Label ID="Label1" runat="server" Text='<%# Bind("ddd") %>'></asp:Label>
            </ItemTemplate>
            <EditItemTemplate>
                <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("ddd") %>'></asp:TextBox>
            </EditItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="cate" SortExpression="cate">
            <EditItemTemplate>
                <asp:DropDownList ID="DropDownList1" runat="server"
                    DataSourceID="SqlDataSource12" DataTextField="cate" DataValueField="id"
                    SelectedValue="<%# Bind('cate') %>">
                </asp:DropDownList>
                <asp:SqlDataSource ID="SqlDataSource12" runat="server"
                    ConnectionString="<%$ ConnectionStrings:testConnectionString %>"
                    SelectCommand="SELECT * FROM [cate]"></asp:SqlDataSource>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Label ID="Label2" runat="server" Text='<%# Bind("cate") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:CommandField ShowEditButton="True" />
    </Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
    ConnectionString="<%$ ConnectionStrings:testConnectionString %>"
    SelectCommand="SELECT * FROM [View_1]"></asp:SqlDataSource>
       <br />
       <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button"
               style="height: 26px" />