如何保持DropDownList中的选定值始终显示?

时间:2012-02-29 10:47:47

标签: c# asp.net

我正在开发一个简单的Intranet建议箱系统,让员工能够通过它提交他们的想法。现在,对于系统管理员,我列出所有提交的建议,并显示员工姓名,用户名,部门,建议标题,建议说明,并添加一列显示状态。对于Status列,它将显示一个DropDownList,其中包含可能的选项,如Accepted,Rejected ...等

这里我有以下问题;当管理员选择其中一个状态时,它将被更改,但刷新页面后,DropDownList将再次显示“选择”选项。我想要的是不断显示所选值而不是选择选项。

我的ASP.NET:

<asp:GridView ID="GridView1" runat="server" AllowPaging="True" 
                        AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="ID" 
                        width="950px" CssClass="mGrid"
                        AlternatingRowStyle-CssClass="alt" 
                        RowStyle-HorizontalAlign="Center" 
                        DataSourceID="SqlDataSource1" 
                        OnRowDataBound="GridView1_RowDataBound" >
            <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
            <HeaderStyle Font-Bold = "true" ForeColor="Black" Height="20px"/> 
            <Columns>
                <asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False" 
                    ReadOnly="True" SortExpression="ID" />
                <asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title" />
                <asp:BoundField DataField="Description" HeaderText="Description" 
                    SortExpression="Description" />
                <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
                <asp:BoundField DataField="Username" HeaderText="Username" 
                    SortExpression="Username" />
                <asp:BoundField DataField="DivisionShortcut" HeaderText="Division" 
                    SortExpression="DivisionShortcut" />
                <asp:TemplateField HeaderText="Status">
                    <ItemTemplate>
                        <asp:DropDownList ID="DropDownList" runat="server" DataSourceID="SqlDataSource2"
                                          Font-Bold="True" ForeColor="#006666" AppendDataBoundItems="false" 
                                          DataTextField="Status" DataValueField="ID" AutoPostBack="true" 
                                          OnDataBound="DropDownList_DataBound" OnSelectedIndexChanged ="DropDownList_SelectedIndexChanged">
                        </asp:DropDownList>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
            ConnectionString="<%$ ConnectionStrings:testConnectionString %>" 
            SelectCommand="SELECT     dbo.SafetySuggestionsLog.ID, dbo.SafetySuggestionsLog.Title, dbo.SafetySuggestionsLog.Description, dbo.employee.Name, dbo.SafetySuggestionsLog.Username, 
                      dbo.Divisions.DivisionShortcut
FROM         dbo.employee INNER JOIN
                      dbo.SafetySuggestionsLog ON dbo.employee.Username = dbo.SafetySuggestionsLog.Username INNER JOIN
                      dbo.Divisions ON dbo.employee.DivisionCode = dbo.Divisions.SapCode"
                      FilterExpression="[DivisionShortcut] like '{0}%'">

                      <FilterParameters>
                        <asp:ControlParameter ControlID="ddlDivision" Name="DivisionShortcut" 
                                                 PropertyName="SelectedValue" Type="String" />
                    </FilterParameters>
        </asp:SqlDataSource>

        <%--For the DropDownList--%>
        <asp:SqlDataSource ID="SqlDataSource2" runat="server"
                            ConnectionString="<%$ ConnectionStrings:testConnectionString %>" 
                            SelectCommand="SELECT * FROM [SafetySuggestionsStatus]">
        </asp:SqlDataSource>

更新:

我的守则 - 背后:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Cells[0].Text = i.ToString();
            i++;

            DataTable dt = new DataTable();
            DropDownList ddStatus = (DropDownList)e.Row.Cells[6].FindControl("DropDownList");
            ddStatus.DataTextField = "Status";
            ddStatus.DataValueField = "ID";
            ddStatus.DataSource = dt;//this datatable should be filled with all the possible values for the status
            ddStatus.DataBind();

        }
    }

    DataTable GetStatusTable()
    {
        SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM [SafetySuggestionsStatus]", "Data Source=localhost\\sqlexpress;Initial Catalog=psspdbTest;Integrated Security=True");
        DataTable dt = new DataTable();
        da.Fill(dt);
        return dt;
    }


    protected void DropDownList_DataBound(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ((DropDownList)sender).Items.Insert(0, new ListItem("--Select--", ""));
        }
    }

    protected void DropDownList_SelectedIndexChanged(object sender, EventArgs e)
    {
        DropDownList ddl = (DropDownList)sender;
        int suggestionStatus = int.Parse(ddl.SelectedValue);
        GridViewRow row = (GridViewRow)ddl.NamingContainer;
        string strID = GridView1.DataKeys[row.RowIndex]["ID"].ToString();
        int ID = Int32.Parse(strID);
        //For inserting the status in the database
        string connString = "Data Source=localhost\\sqlexpress;Initial Catalog=psspdbTest;Integrated Security=True";
        string updateCommand = "UPDATE SafetySuggestionsLog  SET [StatusID] = @StatusID WHERE [ID] = @ID";
        using (SqlConnection conn = new SqlConnection(connString))
        {
            conn.Open();
            using (SqlCommand cmd = new SqlCommand(updateCommand, conn))
            {
                cmd.Parameters.Clear();
                cmd.Parameters.AddWithValue("@StatusID", suggestionStatus);
                cmd.Parameters.AddWithValue("@ID", ID);
                cmd.ExecuteNonQuery();
            }
            conn.Close();
        }

    }

**那么如何解决这个问题让我们一直显示提交的建议的状态?

2 个答案:

答案 0 :(得分:1)

为该字段尝试以下代码。更改为SelectedValue ='&lt;%#Bind(“StatusColumnName”)%&gt;'

<asp:TemplateField HeaderText="Status">
                    <ItemTemplate>
                        <asp:DropDownList ID="DropDownList" runat="server" 
                                          Font-Bold="True" ForeColor="#006666" AppendDataBoundItems="false" 
                                          AutoPostBack="true" 
                                          OnDataBound="DropDownList_DataBound" OnSelectedIndexChanged ="DropDownList_SelectedIndexChanged" SelectedValue='<%# Bind("StatusColumnName") %>'>
                        </asp:DropDownList>
                    </ItemTemplate>
                </asp:TemplateField>

现在添加以下代码

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow) //skip header row
    {
DataTable dt= GetStatusTable();
        DropDownList ddStatus= (DropDownList)e.Row.Cells[5].FindControl("DropDownList");
        ddStatus.DataTextField="Status";
        ddStatus.DataValueField="ID" ;
ddStatus.DataSource=dt;//this datatable should be filled with all the possible values for the status
ddStatus.DataBind();
    }
}

DataTable GetStatusTable()
{
 SqlDataAdapter da= new SqlDataAdapter("select Status,ID from Status","connectionstring here");
DataTable dt= new DataTable();
da.Fill(dt);
return dt;
}

答案 1 :(得分:0)

 <asp:TemplateField HeaderText="Status">  
                <ItemTemplate>  
                    <asp:DropDownList ID="DropDownList" runat="server"   
                                      Font-Bold="True" ForeColor="#006666" AppendDataBoundItems="false"   
                                      AutoPostBack="true"   
                                     OnSelectedIndexChanged ="DropDownList_SelectedIndexChanged" DataSource="<%#WriteFunctionNameHereWhichtoBindDropDownList%>"                        </asp:DropDownList>

现在,当网格绑定时,下拉列表将被绑定。 现在在GridViewItemDataBound事件

找到DropDownList控件并将SelectedValue设置为来自数据库。永远不要忘记在来自数据库的gridview隐藏字段中绑定值。找到隐藏的字段和下拉列表。

现在我认为你可以做到这一点。