2017-05-03 119 views
0

我有,這是獲得在不同頁面上使用的控制,但一個特定的頁面上,我有一個下拉菜單顯示,讓用戶改變在某一個領域數據庫。下拉有兩個項目(「出勤天」和「不聽講日」),和我想要的選項是在數據庫中當訪問控制被加載。填充從數據庫下拉與選定的值(隱藏列)

這裏是我的網格視圖頂部:

<asp:GridView runat="server" DataSourceID="sdsLookups" ID="gvValues" 
AutoGenerateColumns="False" Width="760px" OnDataBound ="GridView_DataBound"> 

這裏是在網格視圖實際下拉。在正確的頁面上,正確的值從數據庫中加載:

<asp:TemplateField HeaderText="Is Attendance?"> 
    <ItemTemplate> 
     <asp:DropDownList id="ddlAttendanceStatus" AutoPostBack="True" runat ="server" SelectedValue='<%# Eval("rules") %>' > 
      <asp:ListItem>Attendance Day</asp:ListItem> 
      <asp:ListItem>Not Attendance Day</asp:ListItem> 
     </asp:DropDownList> 
    </ItemTemplate> 
</asp:TemplateField> 

這是我用來當前隱藏列的代碼。我也試圖檢查,看看,如果該項目爲空或不是,但我還沒有得到這又工作:

protected void GridView_DataBound(object sender, EventArgs e) 
{ 
    if (this.LookupType == "Day Status" ? true : false) 
    { 
     gvValues.Columns[12].Visible = true; 
     GridViewRow row = (sender as Control).Parent.Parent as GridViewRow; 
     DropDownList rules = (row.FindControl("ddlAttendanceStatus") as DropDownList); 
     ListItem item = rules.Items.FindByText("Attendance Day"); 
     if (item != null) 
      rules.Items.FindByText("Attendance Day").Selected = true; 
    } 
} 

目前,該列僅出現在正確的頁面上,但是當我去使用此控件的其他頁面時,我得到一個錯誤,指出:

ArgumentOutOfRangeException: 'ddlAttendanceStatus' has a SelectedValue which is invalid because it does not exist in the list of items. Parameter name: value 

我知道爲什麼我得到這個錯誤,但我不知道如何解決這個問題。如果我不在正確的頁面上,是否有辦法完全忽略此字段?或者我應該採取不同的路線?

請讓我知道如果我需要發佈更多的信息。

預先感謝您。

回答

0

問題是這樣的一段代碼aspx頁面上:SelectedValue='<%# Eval("rules") %>'。在其他頁面,如果rules是不存在或不是Attendance DayNot Attendance Day你會得到錯誤以外的值。您可以通過在OnRowDataBound事件設置SelectedValue解決這個問題。

首先改變GridView控件的ASPX到

<asp:GridView runat="server" DataSourceID="sdsLookups" ID="gvValues" 
    AutoGenerateColumns="False" Width="760px" OnDataBound="GridView_DataBound" 
    OnRowDataBound="gvValues_RowDataBound"> 

然後在後面的代碼

protected void gvValues_RowDataBound(object sender, GridViewRowEventArgs e) 
{ 
    //check if the row is a datarow 
    if (e.Row.RowType == DataControlRowType.DataRow) 
    { 
     //cast the row back to a datarowview 
     DataRowView row = e.Row.DataItem as DataRowView; 

     //find the dropdownlist with findcontrol 
     DropDownList rules = e.Row.FindControl("ddlAttendanceStatus") as DropDownList; 

     if (row["myColumn"].ToString() == "Day Status") 
     { 
      //set the correct selectedvalue 
      rules.SelectedValue = "Not Attendance Day"; 

      //or 
      rules.SelectedValue = "1"; 
     } 
    } 
} 

最後作爲一種選擇,我會建議你使用鍵/值作爲listItems中。這使得使用長字符串更容易。

<asp:DropDownList ID="ddlAttendanceStatus" AutoPostBack="True" runat="server"> 
    <asp:ListItem Text="Attendance Day" Value="0"></asp:ListItem> 
    <asp:ListItem Text="Not Attendance Day" Value="1"></asp:ListItem> 
</asp:DropDownList> 

更新

開始從GridView控件本身去除DataSourceID,並將其移動到後面的代碼。

if (!Page.IsPostBack) 
{ 
    GridView1.DataSource = yourSource; 
    GridView1.DataBind(); 
} 

而且你對DropDownList的AutoPostBack設置爲true,但似乎並沒有處理回發。嘗試刪除,如果沒有發生在那裏。編輯和更新網格中的數據請看this tutorial。它涵蓋了所有的基礎知識。

+0

謝謝你的幫助。我能夠獲得正確的值,但我遇到了另一個問題。在網格視圖中,我需要能夠更改下拉列表的值,然後單擊更新按鈕以更改數據庫中的值。但是,通過此解決方案,當我更改下拉值時,屏幕將刷新並將其更改回存儲在數據庫中的值。 –

+0

更新了我的答案。 – VDWWD

+0

這工作。謝謝! –

相關問題