简体   繁体   English

使用选定的值(隐藏列)填充数据库的下拉列表

[英]Populating a dropdown from the database with a selected value (hidden column)

I have a control that is getting used on different pages, but on one specific page, I am having a dropdown appear that lets the user change a certain field in the database. 我有一个控件正在不同的页面上使用,但是在一个特定的页面上,我出现了一个下拉菜单,允许用户更改数据库中的某个字段。 The dropdown has two items ("Attendance Day" and "Not Attendance Day"), and I want whatever option is in the database to be loaded when the control is accessed. 下拉菜单有两个项目(“出勤日”和“非出勤日”),我希望在访问控件时加载数据库中的任何选项。

Here is the top part of my Grid View: 这是我的网格视图的顶部:

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

Here is actual dropdown in the Grid View. 这是“网格视图”中的实际下拉列表。 On the correct page, the proper values are getting loaded from the database: 在正确的页面上,从数据库中加载了正确的值:

<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>

Here is the code I'm using to currently hide the column. 这是我当前用于隐藏列的代码。 I'm also attempting to check to see if the item is null or not, but I haven't got that to work yet: 我还试图检查该项目是否为空,但是我还没有开始工作:

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

Currently, the column is only appearing on the correct page, but when I go to other pages that use this control, I get an error that says: 当前,该列仅显示在正确的页面上,但是当我转到使用此控件的其他页面时,出现错误消息:

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

I know why I'm getting this error, but I'm not sure how to work around this. 我知道为什么会收到此错误,但是我不确定如何解决此问题。 Is there a way to ignore this field completely if I'm not on the correct page? 如果我不在正确的页面上,是否可以完全忽略此字段? Or is there a different route that I should be taking? 还是我应该走另一条路?

Please let me know if I need to post more information. 如果需要发布更多信息,请告诉我。

Thank you in advance. 先感谢您。

The problem is this piece of code on the aspx page: SelectedValue='<%# Eval("rules") %>' . 问题是aspx页面上的这段代码: SelectedValue='<%# Eval("rules") %>' In the other pages, if rules is not present or has a value other than Attendance Day or Not Attendance Day you'll get that error. 在其他页面中,如果不存在rulesrules的值不是“ Attendance Day或“ Not Attendance Day ,则将收到该错误。 You can solve this by setting the SelectedValue in the OnRowDataBound event. 您可以通过在OnRowDataBound事件中设置SelectedValue来解决此问题。

First change the GridView on the aspx to 首先将aspx上的GridView更改为

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

And then in code behind 然后在后面的代码中

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";
        }
    }
}

Finally as an option I would recommend you use Key/Values as ListItems. 最后,作为一种选择,我建议您将键/值用作ListItems。 This makes it easier that working with long strings. 这使得处理长字符串更加容易。

<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>

update 更新

Start by removing the DataSourceID from the GridView itself and move it into code behind. 首先从GridView本身删除DataSourceID ,然后将其移动到后面的代码中。

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

And you have AutoPostBack set to True on the DropDownList, but do not seem to handle the PostBack. 并且您在DropDownList上将AutoPostBack设置为True,但似乎不处理PostBack。 Try removing that if nothing is happening there. 如果没有任何反应,请尝试将其删除。 And for editing and updating data inside a Grid take a look at this tutorial . 有关在Grid中编辑和更新数据的信息,请看一下本教程 It covers all the basics. 它涵盖了所有基础知识。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM