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 内に異なるストアド プロシージャを作成する必要があります。この例では、1 つのデータベースのみを使用しているため、接続文字列をデータベースのパラメーターとして接続先として渡すことについて心配する必要はありません。最初の赤面で、次のようなものを追加しました

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