网格视图中的asp.NET下拉列表 - 获取所选值

时间:2012-06-25 10:42:08

标签: c# asp.net

我有一个状态下拉列表,我在网格视图中使用,用户可以在网格视图中编辑行时选择所需的列表项。问题是当前实现没有检索选定的值,而只是检索默认/加载的值。

这是网格视图的定义:

    <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" >

这就是列的定义,其中下拉列表将出现在编辑中:

                <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>

这是处理更新方案的代码:

    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();
    }

一切正常,但所选的值不是加载的值而不是用户选择的值。因此问题是从列表中获取所选值。需要改变什么,为什么?

        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();
    }

编辑:添加我的填充方法:

        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()
    }

更新#2:我试图在类中填充一个静态变量,该变量适用于所选索引。然后在按下更新时使用。但是,这仍然是下拉列表的原始值而不是选定的值。

这是方法:

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

4 个答案:

答案 0 :(得分:0)

您是否在DropDownLoadEdit中重新填充列表,无论转换是否为回发?如果是这样,列表将在读取其值之前重新填充,并在您的方法有机会读取值之前设置为默认值。

答案 1 :(得分:0)

问题可能是回帖。

从下拉列表中选择项目后,页面返回后,您选择的值将消失。

过来这个问题。将页面内的下拉列表绑定回发,或者使用Ajax更新面板。

答案 2 :(得分:0)

@Ryan,你做过这个吗? “ 的AutoPostBack = “真”

<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>

答案 3 :(得分:0)

<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>