0

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

这是网格视图的定义:

    <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

4 回答 4

0

问题可能是回邮。

从下拉菜单中选择项目后,页面将回发您选择的值消失。

来解决这个问题。绑定页面内的下拉列表是回发或使用 Ajax 更新面板。

于 2012-06-25T11:07:39.383 回答
0

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

于 2012-06-25T10:57:49.930 回答
0

@Ryan,你有没有做过这个' 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>
于 2013-01-12T06:12:55.287 回答
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>
于 2013-12-22T06:37:36.553 回答