2017-05-03 122 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

这工作。谢谢! –

相关问题