简体   繁体   中英

asp.NET Drop Down List within a Grid View - Getting the selected value

I have a status drop down list which I am using within a grid view, the users can select a list item they want when editing a row within the grid view. The problem is that the current implementation is not retrieving the selected value, but is only retrieving the default/loaded value.

This is the defination of the grid view:

    <asp:GridView ID="applicationGrid" runat="server"  Width="95%"
        AutoGenerateEditButton="True" 
        AutoGenerateColumns="False" 
        ShowFooter="True"
        CellSpacing="10" 
        HeaderStyle-HorizontalAlign="Left" 
        ItemStyle-HorizontalAlign="Left"
        OnRowUpdating="applicationGrid_RowUpdating" 
        OnRowEditing="applicationGrid_RowEditing" 
        OnRowCancelingEdit="applicationGrid_RowCancelingEdit"
        AutoPostBack="true" >

And this is the defination of the Column where the dropdown list will appear on edit:

                <asp:TemplateField HeaderText="Status">
                <ItemTemplate>   
                    <asp:Label ID="StatusDescription" runat="server" Text='<%# Eval("STATUS_DESCRIPTION") %>'></asp:Label>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:DropDownList ID="StatusDescriptionList" runat="server" DataTextField="status_description"
                            DataValueField="application_status_code" OnLoad="DropDownLoadEdit">
                       <asp:ListItem Text="Status:" Value="default"></asp:ListItem>
                    </asp:DropDownList>
                </EditItemTemplate>
            </asp:TemplateField>

This is the code behind which is handling the update scenario:

    protected void applicationGrid_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        GridViewRow row = applicationGrid.Rows[e.RowIndex];
        applicationGrid.EditIndex = -1;

        Label applicationCodeLabel = row.FindControl("AppID") as Label;
        TextBox applicationNameTextBox = row.FindControl("AppNameEdit") as TextBox;
        TextBox applicationURLTextBox = row.FindControl("AppURLEdit") as TextBox;
        DropDownList applicationStatusDropDownList = row.FindControl("StatusDescriptionList") as DropDownList;

        int applicationCode = Convert.ToInt32(applicationCodeLabel.Text);
        string applicationName = applicationNameTextBox.Text;
        string applicationURL = applicationURLTextBox.Text;
        int applicationStatus = Convert.ToInt32(applicationStatusDropDownList.SelectedValue.ToString());
        //string applicationStatus2 = applicationStatusDropDownList.SelectedItem.Value;
        //string applicationStatus3 = applicationStatusDropDownList.SelectedItem.Text;

        application.UpdateApplication(applicationCode, applicationName, applicationURL);
        PopulateApplications();
    }

All is working, but the selected value is not the one which the is loaded and not the one which the user selects. Therefore the problem is getting the selected value from the list. What needs to be changed, and why?

        protected void applicationGrid_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        GridViewRow row = applicationGrid.Rows[e.RowIndex];
        applicationGrid.EditIndex = -1;

        Label applicationCodeLabel = row.FindControl("AppID") as Label;
        TextBox applicationNameTextBox = row.FindControl("AppNameEdit") as TextBox;
        TextBox applicationURLTextBox = row.FindControl("AppURLEdit") as TextBox;
        DropDownList applicationStatusDropDownList = row.FindControl("StatusDescriptionList") as DropDownList;

        int applicationCode = Convert.ToInt32(applicationCodeLabel.Text);
        string applicationName = applicationNameTextBox.Text;
        string applicationURL = applicationURLTextBox.Text;
        int applicationStatus = Convert.ToInt32(applicationStatusDropDownList.SelectedValue.ToString());
        //string applicationStatus2 = applicationStatusDropDownList.SelectedItem.Value;
        //string applicationStatus3 = applicationStatusDropDownList.SelectedItem.Text;

        application.UpdateApplication(applicationCode, applicationName, applicationURL);
        PopulateApplications();
    }

EDIT: Adding my Populate Methods:

        protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            try
            {
                PopulateApplications();
            }
            catch (Exception exception)
            {
                throw exception;
            }
        }
    }

    private void PopulateApplications()
    {
        DataTable reader = application.GetApplicationList();
        applicationGrid.DataSource = reader;
        applicationGrid.DataBind();
        applicationGrid.AllowSorting = true;
    }

    protected void DropDownLoadEdit(object sender, EventArgs e)
    {        
            DataTable statusTable = application.GetStatusList();

            DropDownList dropdown = sender as DropDownList;
            dropdown.DataSource = statusTable;
            dropdown.DataTextField = "status_description";
            dropdown.DataValueField = "application_status_code";
            dropdown.DataBind()
    }

Update #2: I am trying to fill up a static variable in the class which is for the selected index. This will then be used when update is pressed. However, this is still getting the original value of the drop down list and not the selected one.

This is the method:

    protected void StatusDescriptionList_SelectedIndexChanged(object sender, System.EventArgs e)
    {
        DropDownList ddl = (DropDownList)sender;
        selectedValue = Convert.ToInt32(ddl.SelectedValue.ToString());
    }

Are you repopulating the list in DropDownLoadEdit , regardless of whether the transition is a postback or not? If so, the list will be repopulated before its value is read, and set to the default before your method has a chance to read the value.

The issue may be of the post back.

Once the item is selected from the drop down the page get post back the value you selected get vanished.

To over come this issue. Bind the drop down list inside page is post back or else make use of a Ajax update panel.

@Ryan, Have you done this ' AutopostBack="True"

<asp:DropDownList ID="StatusDescriptionList" runat="server" AutopostBack="True" DataTextField="status_description"
                            DataValueField="application_status_code" OnLoad="DropDownLoadEdit">
                       <asp:ListItem Text="Status:" Value="default"></asp:ListItem>
                    </asp:DropDownList>
<asp:DropDownList ID="StatusDescriptionList" runat="server" DataTextField="status_description"
                        DataValueField="application_status_code" SelectedValue="application_status_code" AutopostBack="True" OnLoad="DropDownLoadEdit">
                   <asp:ListItem Text="Status:" Value="default"></asp:ListItem>
                </asp:DropDownList>

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