我正在開發一個簡單的內聯網建議框系統,讓員工能夠通過它提交他們的想法。現在,對於系統管理員,我列出了所有提交的建議,其中顯示了員工姓名,用戶名,部門,建議標題,建議描述以及添加顯示狀態的一列。對於狀態欄,它將顯示一個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();
}
}
**那麼如何解決這個問題,讓狀態提交的建議的所有時間顯示?
我得到了以下錯誤:DataBinding:'System.Data.DataRowView'不包含名稱'狀態'的屬性。 描述:執行當前Web請求期間發生未處理的異常。請查看堆棧跟蹤以獲取有關該錯誤的更多信息以及源代碼的位置。 異常詳細信息:System.Web.HttpException:DataBinding:'System.Data.DataRowView'不包含名稱爲'Status'的屬性。 – 2012-02-29 11:21:11
你能否給我提供代碼片段? – 2012-02-29 11:58:00
我用你的代碼,因爲它是,我得到了以下錯誤:名稱'DT'不存在於當前的上下文中。 – 2012-02-29 12:12:34