2012-02-29 115 views
0

我正在开发一个简单的内联网建议框系统,让员工能够通过它提交他们的想法。现在,对于系统管理员,我列出了所有提交的建议,其中显示了员工姓名,用户名,部门,建议标题,建议描述以及添加显示状态的一列。对于状态栏,它将显示一个DropDownList,其中包含可能的选项,如Accepted,Rejected ...等如何保持DropDownList中的选定值始终显示?

这里我有以下问题;当管理员选择其中一个状态时,它将被更改,但刷新页面后,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> 

UPDATE:

我的代码隐藏:

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

    } 

**那么如何解决这个问题,让状态提交的建议的所有时间显示?

回答

1

请尝试下面的代码为该字段。这种变化是的SelectedValue = '<%#绑定( “StatusColumnName”)%>'

<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; 
} 
+0

我得到了以下错误:DataBinding:'System.Data.DataRowView'不包含名称'状态'的属性。 描述:执行当前Web请求期间发生未处理的异常。请查看堆栈跟踪以获取有关该错误的更多信息以及源代码的位置。 异常详细信息:System.Web.HttpException:DataBinding:'System.Data.DataRowView'不包含名称为'Status'的属性。 – 2012-02-29 11:21:11

+0

你能否给我提供代码片段? – 2012-02-29 11:58:00

+0

我用你的代码,因为它是,我得到了以下错误:名称'DT'不存在于当前的上下文中。 – 2012-02-29 12:12:34

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隐藏字段的值。查找隐藏字段和下拉列表两者。

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