DropdownList1具有一个无效的SelectedValue,因为它在项列表中不存在

时间:2014-08-12 15:28:53

标签: c# asp.net

任何人都可以查看我的代码并告诉我这个错误可能源于何处?我需要我的表单来操作dropdownlist1中的empid字段,因为我的存储过程需要int值,但是当我将DataValueField更改为EmpID时,我说dropdownList中的SelectedValue不存在于项列表中。我已经尝试将SelectedValue更改为EmpId,但没有运气。有什么想法吗?

的.aspx

    <asp:TemplateField HeaderText="Project Lead">
    <EditItemTemplate>
    <asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="Employee" 
    DataTextField="Project Lead" DataValueField="EmpId" 
    SelectedValue='<%# Bind("Project Lead") %>'>
    </asp:DropDownList>
    <asp:SqlDataSource ID="Employee" runat="server" 
    ConnectionString="<%$ ConnectionStrings:LandscapeServicesConnectionString %>" 

    SelectCommand="select EmpId, (firstname+' '+lastname) as &quot;Project Lead&quot; from  Employees ">
    </asp:SqlDataSource>
    </EditItemTemplate>
    <ItemTemplate>
    <asp:Label ID="Label3" runat="server" Text='<%# Bind("ProjectLead") %>'></asp:Label>
    </ItemTemplate>
    <HeaderStyle ForeColor="Black" Wrap="False" Font-Names="Arial" HorizontalAlign="Center"
    BackColor="Silver" Font-Bold="True" Font-Size="11px" />
    <ItemStyle Font-Bold="False" Font-Names="Arial" ForeColor="Black" Wrap="False"
    HorizontalAlign="Left" Font-Size="9px" />

aspx.cs

 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Web;
 using System.Web.UI;
 using System.Web.UI.WebControls;
 using System.Data.SqlClient;
 using System.Data;
 using System.IO;
 using System.Web.UI.HtmlControls;
 using System.Collections;
 using System.Configuration;

 namespace LandscapeServices
 {
 public partial class Update : System.Web.UI.Page
 {
    public string query { get; set; }
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings ["LandscapeServicesConnectionString"].ConnectionString);
    DataSet dt = new DataSet();
    string[] IApps;
    string[] SApps;
    ListBox lbImpactedApps;
    ListBox lbSupportingApps;
    ListBox ListBoxOne;
    ListBox ListBoxTwo;
    ListBox ListBoxThree;
    ListBox ListBoxFour;
    TextBox TextBoxOne;
    TextBox TextBoxTwo;
    DropDownList DropDownListOne;
    DropDownList DropDownListTwo;
    DropDownList DropDownListThree;
    DropDownList DropDownListFour;
    DropDownList DropDownListFive;
    DropDownList DropDownListSeven;
    DropDownList DropDownListEight;
    DropDownList DropDownListNine;
    TextBox Calendar1;
    TextBox Comments;


    protected void Page_Load(object sender, EventArgs e)
    {
        con.Open();

    }

    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        //GridView1.EditIndex = e.NewEditIndex;
        string ImpactedApp = ((System.Web.UI.WebControls.Label)(((System.Web.UI.WebControls.GridView)(sender)).Rows[e.NewEditIndex].FindControl("Label10"))).Text;
        IApps = ImpactedApp.Split(',');

        string SupportingApp = ((System.Web.UI.WebControls.Label)(((System.Web.UI.WebControls.GridView)(sender)).Rows[e.NewEditIndex].FindControl("Label11"))).Text;
        SApps = SupportingApp.Split(',');
    }

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if ((e.Row.RowState & DataControlRowState.Edit) > 0)
            {
                lbImpactedApps = (ListBox)e.Row.FindControl("ListBox1");
                lbSupportingApps = (ListBox)e.Row.FindControl("ListBox2");


                foreach (string lst in IApps)
                {
                    lbImpactedApps.Items.Add(lst.ToString());
                    //lbImpactedApps.Items.FindByText(lst.ToString()).Selected = true;

                }

                lbImpactedApps.DataBind();

                foreach (string lst in SApps)
                {
                    lbSupportingApps.Items.Add(lst.ToString());
                    //lbSupportingApps.Items.FindByText(lst.ToString()).Selected = true;

                }

                lbSupportingApps.DataBind();

            }
        }
    }



    protected void Button1_Click(object sender, EventArgs e)
    {

        foreach (GridViewRow row in GridView1.Rows)
        {
            if (row.RowType == DataControlRowType.DataRow)
            {
                ListBoxOne = row.FindControl("ListBox1") as ListBox;
                ListBoxThree = row.FindControl("ListBox3") as ListBox;
                if (ListBoxOne != null)
                    break;
            }
        }
        for (int i = ListBoxThree.Items.Count - 1; i >= 0; i--)
        {
            if (ListBoxThree.Items[i].Selected == true)
            {
                ListBoxOne.Items.Add(new ListItem(ListBoxThree.Items[i].Text, ListBoxThree.Items[i].Value + ","));
            }
        }

    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        foreach (GridViewRow row in GridView1.Rows)
        {
            if (row.RowType == DataControlRowType.DataRow)
            {
                ListBoxOne = row.FindControl("ListBox1") as ListBox;
                if (ListBoxOne != null)
                    break;
            }
        }

        for (int i = ListBoxOne.Items.Count - 1; i >= 0; i--)
        {
            if (ListBoxOne.Items[i].Selected == true)
            {
                ListBoxOne.Items.Remove(new ListItem(ListBoxOne.Items[i].Text, ListBoxOne.Items[i].Value));
            }
        }


    }


    protected void Button3_Click(object sender, EventArgs e)
    {
        foreach (GridViewRow row in GridView1.Rows)
        {
            if (row.RowType == DataControlRowType.DataRow)
            {
                ListBoxTwo = row.FindControl("ListBox2") as ListBox;
                ListBoxFour = row.FindControl("ListBox4") as ListBox;
                if (ListBoxTwo != null)
                    break;
            }
        }
        for (int i = ListBoxFour.Items.Count - 1; i >= 0; i--)
        {
            if (ListBoxFour.Items[i].Selected == true)
            {
                ListBoxTwo.Items.Add(new ListItem(ListBoxFour.Items[i].Text, ListBoxFour.Items[i].Value + ","));
            }
        }
    }

    protected void Button4_Click(object sender, EventArgs e)
    {
        foreach (GridViewRow row in GridView1.Rows)
        {
            if (row.RowType == DataControlRowType.DataRow)
            {
                ListBoxTwo = row.FindControl("ListBox2") as ListBox;
                if (ListBoxTwo != null)
                    break;
            }
        }

        for (int i = ListBoxTwo.Items.Count - 1; i >= 0; i--)
        {
            if (ListBoxTwo.Items[i].Selected == true)
            {
                ListBoxTwo.Items.Remove(new ListItem(ListBoxTwo.Items[i].Text, ListBoxTwo.Items[i].Value));
            }
        }
    }

    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {

        if (con.ConnectionString == "")
        {
            con.Close();
            con.Dispose();
        }

        foreach (GridViewRow row in GridView1.Rows)
        {
            if (row.RowType == DataControlRowType.DataRow)
            {
                ListBoxOne = row.FindControl("ListBox1") as ListBox;
                ListBoxTwo = row.FindControl("ListBox2") as ListBox;
                TextBoxOne = row.FindControl("txtProjectName") as TextBox;
                TextBoxTwo = row.FindControl("txtArtemisNumber") as TextBox;
                DropDownListOne = row.FindControl("DropDownList1") as DropDownList;
                DropDownListTwo = row.FindControl("DropDownList2") as DropDownList;
                DropDownListThree = row.FindControl("DropDownList3") as DropDownList;
                DropDownListFour = row.FindControl("DropDownList4") as DropDownList;
                DropDownListFive = row.FindControl("DropDownList5") as DropDownList;
                DropDownListSeven = row.FindControl("DropDownList7") as DropDownList;
                DropDownListEight = row.FindControl("DropDownList8") as DropDownList;
                DropDownListNine = row.FindControl("DropDownList9") as DropDownList;
                Calendar1 = row.FindControl("ImplementationDate") as TextBox;
                Comments = row.FindControl("txtComments") as TextBox;

                string ListBox1Values = "";
                for (int i = ListBoxOne.Items.Count - 1; i >= 0; i--)
                {
                    ListBox1Values += ListBoxOne.Items[i].Value;
                }
                if (ListBoxOne.Items.Count == 0)
                {
                    ListBox1Values = "1075";
                }
                ListBox1Values = ListBox1Values.TrimEnd(',');

                string ListBox2Values = "";
                for (int i = ListBoxTwo.Items.Count - 1; i >= 0; i--)
                {
                    ListBox2Values += ListBoxTwo.Items[i].Value;
                }
                if (ListBoxTwo.Items.Count == 0)
                {
                    ListBox2Values = "1075";
                }
                ListBox2Values = ListBox2Values.TrimEnd(',');

                //Execute Stored proc
                query = "EXEC USP_UPDATE_PROJECTS '" + TextBoxOne.Text.ToString() + "','" + 
                    TextBoxTwo.Text.ToString() + "','" + 
                    DropDownListOne.SelectedValue.ToString() + "','" + 
                    DropDownListTwo.SelectedValue.ToString() + "','" + 
                    DropDownListThree.SelectedValue.ToString() + "','" + 
                    DropDownListFour.SelectedValue.ToString() + "','" +
                    DropDownListFive.SelectedValue.ToString() + "','" + 
                    DropDownListEight.SelectedValue.ToString() + "','" + 
                    DropDownListNine.SelectedValue.ToString() + "','" + 
                    ListBox1Values + "','" + 
                    ListBox2Values + "','" + 
                    Calendar1.Text + "','" + 
                    DropDownListSeven.SelectedValue.ToString() + "','" + 
                    Comments.Text.ToString() + "','" + "'";

                //con.Open();
                SqlCommand cmd = new SqlCommand(query, con);
                cmd.ExecuteNonQuery();
            }
        }
    }

}

}

0 个答案:

没有答案