简体   繁体   中英

Static DropDownList in DataGrid returns wrong selected value after button click event C# ASP.NET

I want to grab the selected value from the DropDownLists in my DataGrid when a submit button is clicked, but they always return the first option in the dropdown (approve). How can I get the selected value when using static dropdown items like this?

.aspx code:

            <!-- several BoundColumns were here -->

            <asp:TemplateColumn HeaderText="Actions">
                <HeaderStyle CssClass="ProfDataGridHeader" BorderStyle="Solid" BorderWidth="1"></HeaderStyle>
                <ItemStyle Width="45%" CssClass="ProfDataGridRow" BorderStyle="Solid" BorderWidth="1"></ItemStyle>  
                <ItemTemplate>   
                    <asp:DropDownList ID="ddlApprovalStatus" AppendDataBoundItems="True" runat="server" Width="150px" EnableViewState="true" ViewStateMode="Enabled">  
                        <asp:ListItem Value="approve" Text="Approve"></asp:ListItem>
                        <asp:ListItem Value="reject" Text="Reject"></asp:ListItem>
                    </asp:DropDownList>   
                </ItemTemplate>
            </asp:TemplateColumn>



        </Columns>



    </asp:DataGrid>
    <br />
    <asp:Button ID="btnSubmit" Text="Submit" runat="server" CssClass="ally-btn" OnClick="btnSubmit_Click" />

.aspx.cs code:

        protected void btnSubmit_Click(object sender, EventArgs e)
    {
        DropDownList DDLP;
        string acceptStatus;

        debugLabel.Text = "";

        for (int i = 0; i < dgApprovals.Items.Count; i++)
        {

            DDLP = (DropDownList)dgApprovals.Items[i].FindControl("ddlApprovalStatus");
            acceptStatus = DDLP.SelectedValue;


            debugLabel.Text += acceptStatus + ", ";

        }
    }

The debugLabel.Text always ends up being "accept, accept, accept..." even when the DropDownLists have "Reject" selected.

Reproduced and fixed your problem by handling post-back events.

default.aspx.cs

using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;

namespace DropdownClicks
{

    public partial class WebForm1 : System.Web.UI.Page
    {
        static List<string> itemsToInsert = new List<string> { "first", "second", "third" };

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                //only do this binding on page load, otherwise you'll "reset" the grid every time there is a postBack.
                mydg.DataSource = itemsToInsert;
                mydg.DataBind();
            }
        }

        protected void Unnamed_Click(object sender, EventArgs e)
        {
            DropDownList DDLP;
            string acceptStatus;
            string retVal = "";
            for (int i = 0; i < mydg.Items.Count; i++)
            {
                DDLP = (DropDownList)mydg.Items[i].FindControl("ddlApprovalStatus");
                acceptStatus = DDLP.SelectedValue;
                retVal += acceptStatus + ", ";
            }
            lbl_1.Text = retVal;
        }
    }
}

default.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="DropdownClicks.WebForm1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Label Text="text" runat="server" ID="lbl_1" />
        <asp:DataGrid runat="server" ID="mydg" EnableViewState="true">
            <Columns>
                <asp:TemplateColumn HeaderText="Actions">
                <HeaderStyle CssClass="ProfDataGridHeader" BorderStyle="Solid" BorderWidth="1"></HeaderStyle>
                <ItemStyle Width="45%" CssClass="ProfDataGridRow" BorderStyle="Solid" BorderWidth="1"></ItemStyle>  
                <ItemTemplate>   
                    <asp:DropDownList ID="ddlApprovalStatus" AppendDataBoundItems="True" runat="server" Width="150px" EnableViewState="true" ViewStateMode="Enabled">  
                        <asp:ListItem Value="approve" Text="Approve"></asp:ListItem>
                        <asp:ListItem Value="reject" Text="Reject"></asp:ListItem>
                    </asp:DropDownList>   
                </ItemTemplate>
            </asp:TemplateColumn>
            </Columns>
        </asp:DataGrid>
        <asp:Button Text="click me" runat="server" OnClick="Unnamed_Click" EnableViewState="true" />
    </div>
    </form>
</body>
</html>

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