根据另一个下拉列表下拉列表

时间:2013-11-18 16:04:46

标签: asp.net gridview drop-down-menu

我遇到了两个相互关联的下拉列表的问题。 dlJobName是系统中当前作业的列表。 dlStage是基于所选dlJobName值的可用阶段列表。我两个都有autopostback = True。第三个下拉列表包含值列表。 gridview使用这三个选项来产生结果。我在使用dlStage下拉列表时遇到问题。我收到一个错误: 'dlStage'有一个SelectedValue,它是无效的,因为它在项目列表中不存在。 参数名称:值

我遇到的问题是编写此代码以防止此错误的最佳方法是什么。似乎正在发生的事情是我试图保留变量中选择的最后一个值,这导致了我的问题。这是代码:

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

namespace AnnoTracker
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        public static class MyVariables
        {
            public static int PI = 0;
            public static string JN = "";
            public static string ST = "";
            public static string AT = "";
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (MyVariables.AT == "")
                {
                    dlAnnoType.SelectedValue = "Agency Error";
                    dlAnnoType.DataBind();
                }
                if (MyVariables.AT != "")
                {
                    dlAnnoType.SelectedValue = MyVariables.AT;
                    dlAnnoType.DataBind();
                }
                if (MyVariables.JN == "")
                {
                    dlJobName.SelectedIndex = 0;
                    dlJobName.DataBind();
                }
                if (MyVariables.JN != "")
                {
                    dlJobName.SelectedValue = MyVariables.JN;
                    dlJobName.DataBind();
                }
                if (MyVariables.ST == "")
                {
                    dlStage.SelectedIndex = 0;
                    dlStage.DataBind();
                }
                if (MyVariables.JN != "")
                {
                    dlStage.SelectedValue = MyVariables.ST;
                    dlStage.DataBind();
                }
                dlStage.SelectedIndex = 0;
                dlStage.DataBind();

                BindData();

                if (MyVariables.PI == 0)
                {
                    gvSummary.PageIndex = 0;
                    gvSummary.DataBind();
                }
                if (MyVariables.PI != 0)
                {
                    gvSummary.PageIndex = MyVariables.PI;
                    gvSummary.DataBind();
                }
            }
        }

        protected void EditSummary(object sender, GridViewEditEventArgs e)
        {
            gvSummary.EditIndex = e.NewEditIndex;
            string _custName = gvSummary.DataKeys[e.NewEditIndex].Value.ToString();
            BindData();
        }

        protected void CancelEdit(object sender, GridViewCancelEditEventArgs e)
        {
            gvSummary.EditIndex = -1;
            BindData();
        }

        protected void gvSummary_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvSummary.PageIndex = e.NewPageIndex;
            MyVariables.PI = e.NewPageIndex;
            BindData();
        }


        protected void RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow && gvSummary.EditIndex == e.Row.RowIndex)
            {

                DropDownList dlBU = (DropDownList)e.Row.FindControl("dlBU");

                string _custName = gvSummary.DataKeys[e.Row.RowIndex].Values[1].ToString();
                string BUquery = "select distinct Unit from vw_BU where Business='" + _custName + "'";
                SqlCommand BUcmd = new SqlCommand(BUquery);
                dlBU.DataSource = GetData(BUcmd);
                dlBU.DataTextField = "Unit";
                dlBU.DataValueField = "Unit";
                dlBU.DataBind();
                dlBU.Items.FindByValue((e.Row.FindControl("lblBU") as Label).Text).Selected = true;

                DropDownList dlPA = (DropDownList)e.Row.FindControl("dlPA");

                string _PAcustName = gvSummary.DataKeys[e.Row.RowIndex].Values[1].ToString();
                string PAquery = "select PA from PA where Business='" + _PAcustName + "' order by PA";
                SqlCommand PAcmd = new SqlCommand(PAquery);
                dlPA.DataSource = GetData(PAcmd);
                dlPA.DataTextField = "PA";
                dlPA.DataValueField = "PA";
                dlPA.DataBind();
                dlPA.Items.FindByValue((e.Row.FindControl("lblPA") as Label).Text).Selected = true;

                DropDownList dlET = (DropDownList)e.Row.FindControl("dlET");

                string ETquery = "select distinct ErrorType from ErrorType order by ErrorType";
                SqlCommand ETcmd = new SqlCommand(ETquery);
                dlET.DataSource = GetData(ETcmd);
                dlET.DataTextField = "ErrorType";
                dlET.DataValueField = "ErrorType";
                dlET.DataBind();
                dlET.Items.FindByValue((e.Row.FindControl("lblET") as Label).Text).Selected = true;

                DropDownList dlAA = (DropDownList)e.Row.FindControl("dlAA");

                string AAquery = "select distinct AAA from ActualAgencyError";
                SqlCommand AAcmd = new SqlCommand(AAquery);
                dlAA.DataSource = GetData(AAcmd);
                dlAA.DataTextField = "AAA";
                dlAA.DataValueField = "AAA";
                dlAA.DataBind();
                dlAA.Items.FindByValue((e.Row.FindControl("lblAA") as Label).Text).Selected = true;

                DropDownList dlSupport = (DropDownList)e.Row.FindControl("dlSupport");

                string SupQuery = "select distinct Support from Support";
                SqlCommand Supcmd = new SqlCommand(SupQuery);
                dlSupport.DataSource = GetData(Supcmd);
                dlSupport.DataTextField = "Support";
                dlSupport.DataValueField = "Support";
                dlSupport.DataBind();
                dlSupport.Items.FindByValue((e.Row.FindControl("lblSupport") as Label).Text).Selected = true;

            }
        }

        protected void UpdateSummary(object sender, GridViewUpdateEventArgs e)
        {
            MyVariables.AT = dlAnnoType.SelectedValue;
            //dlAnnoType.DataBind();
            MyVariables.JN = dlJobName.SelectedValue;
            //dlJobName.DataBind();
            MyVariables.ST = dlStage.SelectedValue;
            //dlStage.DataBind();
            MyVariables.PI = gvSummary.PageIndex;
            //gvSummary.DataBind();

            string BU = (gvSummary.Rows[e.RowIndex].FindControl("dlBU") as DropDownList).SelectedItem.Value;
            string ET = (gvSummary.Rows[e.RowIndex].FindControl("dlET") as DropDownList).SelectedItem.Value;
            string AA = (gvSummary.Rows[e.RowIndex].FindControl("dlAA") as DropDownList).SelectedItem.Value;
            string PA = (gvSummary.Rows[e.RowIndex].FindControl("dlPA") as DropDownList).SelectedValue;
            string AnnotationNumber = gvSummary.DataKeys[e.RowIndex].Value.ToString();
            string sgkComments = (gvSummary.Rows[e.RowIndex].FindControl("tbsgkComm") as TextBox).Text;
            string Support = (gvSummary.Rows[e.RowIndex].FindControl("dlSupport") as DropDownList).SelectedValue;
            string SupportName = (gvSummary.Rows[e.RowIndex].FindControl("tbSupportName") as TextBox).Text;
            string BusImpact = (gvSummary.Rows[e.RowIndex].FindControl("tbBusImpact") as TextBox).Text;

            string strConnString = ConfigurationManager.ConnectionStrings["SRM_MetricConnectionString"].ConnectionString;
            using (SqlConnection con = new SqlConnection(strConnString))
            {
                string query = "update vw_GridviewSource set [BusinessUnit] = @BU, [ErrorType] = @ET, [sgkComments] = @sgk, [ActualAgencyError] = @AA, [PA] = @PA, [Support] = @Support, [SupportName] = @SupportName, [BusImpact] = @BusImpact where [AnnotationNumber] = @AnnoNum";
                using (SqlCommand cmd = new SqlCommand(query))
                {
                    cmd.Connection = con;
                    cmd.Parameters.AddWithValue("@BU", BU);
                    cmd.Parameters.AddWithValue("@AnnoNum", AnnotationNumber);
                    cmd.Parameters.AddWithValue("@ET", ET);
                    cmd.Parameters.AddWithValue("@AA", AA);
                    cmd.Parameters.AddWithValue("@sgk", sgkComments);
                    cmd.Parameters.AddWithValue("@PA", PA);
                    cmd.Parameters.AddWithValue("@Support", Support);
                    cmd.Parameters.AddWithValue("@SupportName", SupportName);
                    cmd.Parameters.AddWithValue("@BusImpact", BusImpact);

                    con.Open();
                    cmd.ExecuteNonQuery();
                    con.Close();
                    Response.Redirect(Request.Url.AbsoluteUri);
                }
            }
            gvSummary.PageIndex = MyVariables.PI;
            gvSummary.DataBind();

            BindData();
        }

        private void BindData()
        {
            gvSummary.PageIndex = MyVariables.PI;
            gvSummary.DataBind();

            if (MyVariables.JN != "" && MyVariables.ST != "" && MyVariables.AT != "")
            {
                dlJobName.SelectedValue = MyVariables.JN;
                dlJobName.DataBind();
                dlStage.SelectedValue = MyVariables.ST;
                dlStage.DataBind();
                dlAnnoType.SelectedValue = MyVariables.AT;
                dlAnnoType.DataBind();
            }
            gvSummary.PageIndex = MyVariables.PI;
            gvSummary.DataBind();

            String conString = System.Configuration.ConfigurationManager.ConnectionStrings["SRM_MetricConnectionString"].ConnectionString;
            string query = "select [Page_ID],[AnnotationNumber],[AnnotationBy],[PA],[AnnotationType],[BusinessUnit] as Unit,[ErrorType],[ActualAgencyError],AnnotationComments,[sgkComments],[ActualAgencyError],Support,SupportName, BusImpact,Cust from vw_GridviewSource order by [Page_ID]";

            SqlCommand cmd = new SqlCommand();

            if (dlJobName.SelectedValue != "" & dlStage.SelectedValue != "")
            {
                query = "select [Page_ID],[AnnotationNumber],[AnnotationBy],[PA],[AnnotationType],[BusinessUnit] as Unit,[ErrorType],[ActualAgencyError],AnnotationComments,[sgkComments],[ActualAgencyError],Support,SupportName, BusImpact,Cust from vw_GridviewSource where Name = '" + dlJobName.SelectedValue + "' and AnnotationDate = '" + dlStage.SelectedValue + "' order by [Page_ID]";
                //cmd.Parameters.AddWithValue("@Name", dlJobName.SelectedValue);
                //cmd.Parameters.AddWithValue("@Stage", dlStage.SelectedValue);
            }


            if (dlAnnoType.SelectedValue != "" && (dlJobName.SelectedValue.Length < 2 && dlStage.SelectedValue.Length < 2))
            {
                query = "select [Page_ID],[AnnotationNumber],[AnnotationBy],[PA],[AnnotationType],[BusinessUnit] as Unit,[ErrorType],[ActualAgencyError],AnnotationComments,[sgkComments],[ActualAgencyError],Support,SupportName, BusImpact,Cust from vw_GridviewSource where AnnotationType = '" + dlAnnoType.SelectedValue + "' order by [Page_ID]";
            }

            if (dlAnnoType.SelectedValue != "" && (dlJobName.SelectedValue != "" && dlStage.SelectedValue != ""))
            {
                query = "select [Page_ID],[AnnotationNumber],[AnnotationBy],[PA],[AnnotationType],[BusinessUnit] as Unit,[ErrorType],[ActualAgencyError],AnnotationComments,[sgkComments],[ActualAgencyError],Support,SupportName, BusImpact,Cust from vw_GridviewSource where AnnotationType = '" + dlAnnoType.SelectedValue + "' and Name = '" + dlJobName.SelectedValue + "' and AnnotationDate = '" + dlStage.SelectedValue + "' order by [Page_ID]";
            }

            cmd.CommandText = query;
            SqlConnection con = new SqlConnection(conString);
            cmd.CommandType = CommandType.Text;
            cmd.Connection = con;
            SqlDataAdapter sda = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            sda.Fill(ds);
            gvSummary.DataSource = ds;
            gvSummary.PageIndex = MyVariables.PI;
            gvSummary.DataBind();

        }
        private DataTable GetData(SqlCommand cmd)
        {
            string strConnString = ConfigurationManager.ConnectionStrings["SRM_MetricConnectionString"].ConnectionString;
            using (SqlConnection con = new SqlConnection(strConnString))
            {
                using (SqlDataAdapter sda = new SqlDataAdapter())
                {
                    cmd.Connection = con;
                    sda.SelectCommand = cmd;
                    using (DataTable dt = new DataTable())
                    {
                        sda.Fill(dt);
                        return dt;
                    }
                }
            }
        }

        protected void dlJobName_SelectedIndexChanged(object sender, EventArgs e)
        {
            MyVariables.JN = dlJobName.SelectedValue;
            dlJobName.DataBind();
            MyVariables.ST = dlStage.SelectedValue;
            dlStage.DataBind();
            MyVariables.AT = dlAnnoType.SelectedValue;
            dlAnnoType.DataBind();

            BindData();
            MyVariables.PI = gvSummary.PageIndex;
            gvSummary.DataBind();
        }
        protected void dlStage_SelectedIndexChanged(object sender, EventArgs e)
        {
            MyVariables.JN = dlJobName.SelectedValue;
            //dlStage.DataBind();
            MyVariables.ST = dlStage.SelectedValue;
            MyVariables.AT = dlAnnoType.SelectedValue;
            gvSummary.DataBind();
            BindData();
        }
        protected void dlAnnoType_SelectedIndexChanged(object sender, EventArgs e)
        {
            MyVariables.AT = dlAnnoType.SelectedValue;
            MyVariables.JN = dlJobName.SelectedValue;
            //dlStage.DataBind();
            MyVariables.ST = dlStage.SelectedValue;
            gvSummary.DataBind();
            BindData();
        }


    }
}

1 个答案:

答案 0 :(得分:0)

我在你的page_load事件代码中发现了两个问题,我在这里进行了修改:

protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (MyVariables.AT == "")
                {
                     dlAnnoType.DataBind();
                    dlAnnoType.SelectedValue = "Agency Error";

                }
                if (MyVariables.AT != "")
                {
                    dlAnnoType.DataBind();
                    dlAnnoType.SelectedValue = MyVariables.AT;
                }
                if (MyVariables.JN == "")
                {
                     dlJobName.DataBind();
                    dlJobName.SelectedIndex = 0;
                }
                if (MyVariables.JN != "")
                {
                   dlJobName.DataBind();                   
                   dlJobName.SelectedValue = MyVariables.JN;
                }
                if (MyVariables.ST == "")
                {
                    dlStage.DataBind();
                    dlStage.SelectedIndex = 0;
                }
                dlStage.DataBind();
                //dlStage.SelectedIndex = 0;

                if (MyVariables.ST != "")
                {
                    //dlStage.DataBind();
                    dlStage.SelectedValue = MyVariables.ST;
                }


                BindData();

                if (MyVariables.PI == 0)
                {
                 gvSummary.DataBind();   
                 gvSummary.PageIndex = 0;

                }
                if (MyVariables.PI != 0)
                {
                    gvSummary.DataBind();
                    gvSummary.PageIndex = MyVariables.PI;

                }
            }
        }