如何在Gridview C#中获取下拉列表的选定值

时间:2017-09-26 13:19:25

标签: c# asp.net gridview

我有一个GridView显示几列。当我单击GridView中的编辑按钮时,它会显示一列的DropDownList,其中包含要选择的值列表。

我需要选择一个值并单击Update按钮以使用最新选择的值更新数据库。

<asp:GridView ID="GrdAttributesView" runat="server" AllowPaging="True" 
        AllowSorting="True" AutoGenerateColumns="False" CssClass="well" 
        EmptyDataText="No Records to display." 
        OnPageIndexChanging="GrdAttributesView_PageIndexChanging" 
        OnRowCancelingEdit="GrdAttributesView_RowCancelingEdit" 
        OnRowEditing="GrdAttributesView_RowEditing" 
        OnRowUpdated="GrdAttributesView_RowUpdated" 
        OnRowUpdating="GrdAttributesView_RowUpdating" 
        OnSelectedIndexChanged="GrdAttributesView_SelectedIndexChanged" 
        OnSorting="GrdAttributesView_Sorting" 
        OnRowDeleting="GrdAttributesView_RowDeleting" 
        OnRowDeleted="GrdAttributesView_RowDeleted" 
        OnRowDataBound="GrdAttributesView_RowDataBound" PagerSettings-Mode="Numeric" 
        PagerStyle-Font-Bold="true" PageSize="20" ShowHeaderWhenEmpty="True">
    <Columns>
        <%-- <asp:TemplateField HeaderText="Globalized Indicator">
            <ItemTemplate>
                <asp:CheckBox ID="Chk_GlobalizedIndicatorGrid" runat="server" Width="55px" OnCheckedChanged="Chk_GlobalizedIndicatorGrid_CheckedChanged" Text='<%# Eval("GlobalizedInd") %>' Checked ='<%# Eval("GlobalizedInd") == DBNull.Value ? false : Convert.ToBoolean(Eval("GlobalizedInd")) %>' Enabled="false"/>
            </ItemTemplate>
        </asp:TemplateField>--%>
        <asp:CommandField HeaderText="Edit Record" ItemStyle-Width="150px" ShowEditButton="True" ShowHeader="True" ShowDeleteButton="true">
        <ItemStyle Width="100px" />
        </asp:CommandField>
        <asp:BoundField HeaderText="Added Date" DataField="AddedDate" ItemStyle-Width="700px" ReadOnly="true" ControlStyle-Width="700px">
            <ControlStyle Width="200px"></ControlStyle>

            <ItemStyle Width="200px"></ItemStyle>
        </asp:BoundField>
        <asp:BoundField HeaderText="Created By" DataField="CreatedBy" ItemStyle-Width="700px" ReadOnly="true" ControlStyle-Width="700px">
            <ControlStyle Width="100px"></ControlStyle>
            <ItemStyle Width="100px"></ItemStyle>
        </asp:BoundField>
        <asp:BoundField HeaderText="Ticket Number" DataField="TicketNumber" ItemStyle-Width="200px" ReadOnly="true" ControlStyle-Width="700px">
            <ControlStyle Width="200px"></ControlStyle>
            <ItemStyle Width="200px"></ItemStyle>
        </asp:BoundField>
        <asp:BoundField HeaderText="Ticket URL" DataField="TIcketURL" ItemStyle-Width="200px" ReadOnly="true" ControlStyle-Width="700px">
            <ControlStyle Width="600px"></ControlStyle>
            <ItemStyle Width="600px"></ItemStyle>
        </asp:BoundField>
        <asp:TemplateField HeaderText="Hours Spent" ItemStyle-Width="100px" ControlStyle-Width="700px">
            <ControlStyle Width="100px"></ControlStyle>
            <ItemStyle Width="100px"></ItemStyle>
            <ItemTemplate>
                <asp:Label ID="LblHoursSpent" runat="server" Text='<%# Eval("HoursSpent")%>'></asp:Label>

            </ItemTemplate>
            <EditItemTemplate>
                 <asp:Label ID="LblHoursSpent" runat="server" Text='<%# Eval("HoursSpent")%>' Visible="false"></asp:Label>

                <asp:DropDownList ID="CmbHoursSpent" runat="server" >
                </asp:DropDownList>
            </EditItemTemplate>
        </asp:TemplateField>

        <asp:TemplateField HeaderText="Minutes Spent" ItemStyle-Width="100px" ControlStyle-Width="700px">
            <ControlStyle Width="120px"></ControlStyle>
            <ItemStyle Width="120px"></ItemStyle>
            <ItemTemplate>
                <asp:Label ID="LblMinutesSpent" runat="server" Text='<%# Eval("MinutesSpent")%>'></asp:Label>

            </ItemTemplate>
            <EditItemTemplate>
                 <asp:Label ID="LblMinutesSpent" runat="server" Text='<%# Eval("MinutesSpent")%>' Visible="false"></asp:Label>
                <%--<asp:DropDownList ID="CmbHoursSpent" AppendDataBoundItems="true" DataTextField="NumberofHoursSpent" DataValueField="HoursSpent" AutoPostBack="false" runat="server" OnSelectedIndexChanged="CmbHoursSpent_SelectedIndexChanged" enabled="true">
                </asp:DropDownList>--%>
                <asp:DropDownList ID="CmbMinutesSpent" runat="server">
                </asp:DropDownList>
            </EditItemTemplate>
        </asp:TemplateField>
    </Columns>
    <PagerStyle Font-Bold="True" />
</asp:GridView>    

背后的代码如下:

protected void GrdAttributesView_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow && GrdAttributesView.EditIndex == e.Row.RowIndex)
    {

        if ((e.Row.RowState & DataControlRowState.Edit) > 0)
        {
            DropDownList ddlHours = (DropDownList)e.Row.FindControl("CmbHoursSpent");
            ddlHours.DataSource = GenerateHoursList();
            ddlHours.DataTextField = "NumberofHoursSpent";
            ddlHours.DataValueField = "HoursSpent";
            ddlHours.DataBind();
            ddlHours.Items.FindByValue((e.Row.FindControl("LblHoursSpent") as Label).Text).Selected = true;

            DropDownList ddlMinutes = (DropDownList)e.Row.FindControl("CmbMinutesSpent");
            ddlMinutes.DataSource = GenerateMinutesList();
            ddlMinutes.DataTextField = "NumberofMinutesSpent";
            ddlMinutes.DataValueField = "MinutesSpent";
            ddlMinutes.DataBind();
            ddlMinutes.Items.FindByValue((e.Row.FindControl("LblMinutesSpent") as Label).Text).Selected = true;

        }
    }}
protected void GrdAttributesView_RowUpdating(object sender, GridViewUpdateEventArgs e)
{

    DropDownList ddlprice = (DropDownList)GrdAttributesView.Rows[e.RowIndex].FindControl("CmbHoursSpent");
    String value = ddlprice.SelectedValue;

    string MinutesSpent = (GrdAttributesView.Rows[e.RowIndex].FindControl("CmbMinutesSpent") as DropDownList).SelectedItem.Value;
}    

理想情况下,在RowUpdating方法中,我应该从DropDown获取新值。但即使我选择了不同的值,我也不明白为什么它会给出旧值。

我做了很多阅读,我看到不同的人使用相同的方法来完成它。但不知怎的,它对我不起作用。

有人可以帮忙吗?

0 个答案:

没有答案