0

我有一个 TabContainer 控件,其中包含多个选项卡。根据 TabContainer 的 ActiveIndex 属性,我希望单击按钮以在该选项卡中创建一个 Gridview 并将其绑定到存储过程。目前,以下代码有效,但有很多代码重复

 protected void btnMakeGridView_Click(object sender, EventArgs e)
        {
            string cs = ConfigurationManager.ConnectionStrings["dbcs"].ConnectionString;
            if (TabContainer1.ActiveTabIndex == 0)
            {

                using (SqlConnection con = new SqlConnection(cs))
                {
                    using (SqlCommand cmd = new SqlCommand("spTest0", con))
                    {
                        con.Open();
                        cmd.CommandType = CommandType.Text;
                        SqlDataReader rdr = cmd.ExecuteReader();
                        GridView gv = new GridView();
                        TabContainer1.Tabs[TabContainer1.ActiveTabIndex].Controls.Add(gv);
                        gv.DataSource = rdr;
                        gv.DataBind();

                    }
                }
            }
            else if (TabContainer1.ActiveTabIndex == 1)
            {
                using (SqlConnection con = new SqlConnection(cs))
                {
                    using (SqlCommand cmd = new SqlCommand("spTest1", con))
                    {
                        con.Open();
                        cmd.CommandType = CommandType.Text;
                        SqlDataReader rdr = cmd.ExecuteReader();
                        GridView gv = new GridView();
                        TabContainer1.Tabs[TabContainer1.ActiveTabIndex].Controls.Add(gv);
                        gv.DataSource = rdr;
                        gv.DataBind();

                    }
                }
            }
            else if (TabContainer1.ActiveTabIndex == 2)
            {
                using (SqlConnection con = new SqlConnection(cs))
                {
                    using (SqlCommand cmd = new SqlCommand("spTest2", con))
                    {
                        con.Open();
                        cmd.CommandType = CommandType.Text;
                        SqlDataReader rdr = cmd.ExecuteReader();
                        GridView gv = new GridView();
                        TabContainer1.Tabs[TabContainer1.ActiveTabIndex].Controls.Add(gv);
                        gv.DataSource = rdr;
                        gv.DataBind();

                    }
                }
            }
            else
            {
                Label1.Text = "You must select a tab to create the GridView in";
            }

        }

正如您所看到的,根据 TabContainer 的 ActiveIndex,应该在每个 TabPanel 内创建不同的存储过程。在我的示例中,我只使用一个数据库,因此我不必担心将连接字符串作为参数传递给要连接的数据库。乍一看,我添加了类似的东西

 //pass in the connection string, the stored procedure name and the ActiveTabIndex
        public static void DataAccess(string connectionString,string spName, int activeTabIndex)
        {
            using (SqlConnection con = new SqlConnection(connectionString))
            {
                using (SqlCommand cmd = new SqlCommand(spName, con))
                {
                    con.Open();
                    cmd.CommandType = CommandType.Text;
                    SqlDataReader rdr = cmd.ExecuteReader();
                    GridView gv = new GridView();

                    gv.DataSource = rdr;
                    gv.DataBind();

                }
            }
        }

UDPATE:好的,同时我想出了

 public partial class _Default : System.Web.UI.Page
    {

        string cs = ConfigurationManager.ConnectionStrings["dbcs"].ConnectionString;
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnMakeGridView_Click(object sender, EventArgs e)
        {
            TabContainer1.Tabs[TabContainer1.ActiveTabIndex].Controls.Add(GetData(cs, "spTest", TabContainer1.ActiveTabIndex));
        }
        public static GridView GetData(string connectionString,string spName, int activeIndex)
        {
            using (SqlConnection con = new SqlConnection(connectionString))
            {
                using (SqlCommand cmd = new SqlCommand(spName, con))
                {
                    con.Open();
                    cmd.CommandType = CommandType.StoredProcedure;
                    SqlDataReader rdr = cmd.ExecuteReader();
                    GridView gv = new GridView();
                    gv.ID = "gv" + activeIndex.ToString();
                    gv.DataSource = rdr;
                    gv.DataBind();
                    return gv;
                }

            }
        }

    }
4

0 回答 0