简体   繁体   中英

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") %>' . 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. You can solve this by setting the SelectedValue in the OnRowDataBound event.

First change the GridView on the aspx to

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

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. Try removing that if nothing is happening there. And for editing and updating data inside a Grid take a look at this tutorial . It covers all the basics.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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