简体   繁体   中英

Dropdown list in grid view: can't get selected value

At one point I was able to select from a static DDL in my grid view and then update the grid view based upon the selection I made. At some point, this stopped working and I am unable to resurrect it.

Below is the (simplified) code where everything works except getting the selected value from the DDL - it always returns the first list item. Any idea where I went wrong?

  protected void gv_Pending_RowDataBound(object sender, GridViewRowEventArgs e)
        { 
            if (e.Row.RowType == DataControlRowType.DataRow && (e.Row.RowState & DataControlRowState.Edit) == DataControlRowState.Edit)
            {
                ((DropDownList)e.Row.FindControl("ddl_Pending")).Enabled = true;        
            }

        }

    protected void gv_Pending_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            int index = Convert.ToInt32(e.RowIndex);
            GridViewRow row = gv_Pending.Rows[index];

            string adm_appr = ((DropDownList)row.FindControl("ddl_Pending")).SelectedValue.ToString();
        }

    <asp:GridView ID="gv_Pending" runat="server" 
        OnRowDataBound="gv_Pending_RowDataBound" OnRowUpdating="gv_Pending_RowUpdating" OnRowEditing="gv_Pending_RowEditing" 
        OnRowCancelingEdit="gv_Pending_RowCancelingEdit" OnRowCommand="gv_Pending_RowCommand" 
        ClientIDMode="Static" >
        <Columns>
            <asp:CommandField ShowCancelButton="true" ShowEditButton="true" />
            <asp:TemplateField HeaderText="Approval" Visible="true">
                <ItemTemplate>
                    <asp:DropDownList ID="ddl_Pending" runat="server" OnSelectedIndexChanged="ddlPending_SelectedIndexChanged"
                        Enabled="false" >
                        <asp:ListItem >Select One</asp:ListItem>
                        <asp:ListItem >Standard Privilege</asp:ListItem>
                        <asp:ListItem >Admin Privilege</asp:ListItem>
                        <asp:ListItem >Deny Request</asp:ListItem>
                        <asp:ListItem >Remove Account</asp:ListItem>
                    </asp:DropDownList>
               </ItemTemplate>
          </asp:TemplateField>
          <asp:BoundField HeaderText="Field 1" DataField="Field 1 data" ReadOnly="true" />
          <asp:BoundField HeaderText="Field 2" DataField="Field 2 data" ReadOnly="true" />
        </Columns>
    </asp:GridView>

For others who may come after me, it was a load sequence issue.

In particular, I was calling the grid view load event on page_load, postback. This reset the DDL and, naturally, wiped out the selection made during the row updating event. By removing the grid view load event call from page_load, the DDL selection made during the row updating event persisted as expected.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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