1

テーブル アダプタの行を初期化することはできますか? 私が作成した次のコードがあります。コントロール名の重複からリファクタリングされ、FindControl を使用してコントロールを再帰的に検索するようになりました。

private int _category;
private int _max;
private string _queryString;
private int _pageStart;
private const int PageSize = 4;
private void PopulatePage(string s)

{
    using (var projectTableAdapter = new ProjectTableAdapter())
    {
        _max = s == "category" ? projectTableAdapter.GetDataByCategory(_category).Count : PageSize;
        double totalPages = Math.Ceiling((float) _max/PageSize);

        int start = (_pageStart - 1) * PageSize;
        var end = _pageStart * PageSize;

        if (end > _max) end = _max;

        for (int i = start; i < end; i++)
        {
            int tmp = i - start;
            int c = tmp + 1;

            var image = FindControl(string.Format("ctl00$Body$ControlImage{0}", c)) as Image;
            var title = FindControl(string.Format("ctl00$Body$ControlTitle{0}", c)) as Literal;
            var summary = FindControl(string.Format("ctl00$Body$ControlSummary{0}", c)) as Literal;
            var link = FindControl(string.Format("ctl00$Body$ControlLink{0}", c)) as HyperLink;
            var listitem = FindControl(string.Format("ctl00$Body$ControlListItem{0}", c)) as HtmlGenericControl;

            switch (s)
            {
                case "category":
                    DataConnection.ProjectRow projectRowCategory =
                        projectTableAdapter.GetDataByCategory_Active_RankOrder(_category)[i];

                    if (image != null) image.ImageUrl = projectRowCategory.Image;
                    if (title != null) title.Text = projectRowCategory.Name;
                    if (summary != null) summary.Text = projectRowCategory.Summary;

                    if (projectRowCategory.Description.Length > 0)
                    {
                        if (link != null)
                        {
                            link.NavigateUrl = string.Format("/experience/details/?id={0}",
                                                             projectRowCategory.ProjectID);
                            link.Visible = true;
                        }
                    }
                    break;
                case "random":
                        DataConnection.ProjectRow projectRowRandom = projectTableAdapter.GetDataByRandom()[i];

                        if (image != null) image.ImageUrl = projectRowRandom.Image;
                        if (title != null) title.Text = projectRowRandom.Name;
                        if (summary != null) summary.Text = projectRowRandom.Summary;

                        if (projectRowRandom.Description.Length > 0)
                        {
                            if (link != null)
                            {
                                link.NavigateUrl = string.Format("/experience/details/?id={0}",
                                                                 projectRowRandom.ProjectID);
                                link.Visible = true;
                            }
                        }
                    break;
            }

            if (listitem != null) listitem.Visible = true;
        }

        if ((Request.QueryString["p"] == null) || (Request.QueryString["p"] == "1"))
        {
            if (_max < PageSize)
            {
                ControlPage.Text = "";
            }
            else
            {
                ControlPage.Text = "<< prev | <a href = \"?s=" + _queryString + "&p=2\">next >></a>";
            }
        }
        else
        {
            int next = _pageStart + 1;
            int prev = _pageStart - 1;
            if (next > totalPages)
            {
                ControlPage.Text = string.Format(@"<a href = ""?s={0}&p={1}""><< prev</a> | next >></a>",
                                                 _queryString, prev);
            }
            else
            {
                ControlPage.Text =
                    string.Format(
                        @"<a href = ""?s={0}&p={1}""><< prev</a> | <a href = ""?s={0}&p={2}"">next >></a>",
                        _queryString, prev, next);
            }
        }
    }
}

SWITCH ステートメント内のコードの重複をリファクタリングして削除したいと考えています。だから私は次のようなことができます:

var projectRow = new DataConnection.ProjectRow();

            switch (s)
            {
                case "category":
                        projectRow = projectTableAdapter.GetDataByCategory_Active_RankOrder(_category)[i];
                    break;
                case "random":
                        projectRow = projectTableAdapter.GetDataByRandom()[i];
                    break;
            }

            if (image != null) image.ImageUrl = projectRow.Image;
            if (title != null) title.Text = projectRow.Name;
            if (summary != null) summary.Text = projectRow.Summary;

            if (projectRow.Description.Length > 0)
            {
                if (link != null)
                {
                    link.NavigateUrl = string.Format("/experience/details/?id={0}",
                                                     projectRow.ProjectID);
                    link.Visible = true;
                }
            }

            if (listitem != null) listitem.Visible = true;

ProjectRow は DataConnection の内部コンストラクターであるため、そのコードは使用できません。それは実行可能だと思います...シンコ・デ・マヨの後、私の脳は休暇を取ったようです。

アップデート:

以下の方法で動作しました。行初期化子を「null」に設定するのを忘れ、「new」を使用しませんでした。情報もありがとう、ヘンク。

DataConnection.ProjectRow projectRow = null;

            switch (s)
            {
                case "category":
                    projectRow = projectTableAdapter.GetDataByCategory_Active_RankOrder(_category)[i];
                    break;
                case "random":
                    projectRow = projectTableAdapter.GetDataByRandom()[i];
                    break;
            }

            if (image != null) if (projectRow != null) image.ImageUrl = projectRow.Image;
            if (title != null) if (projectRow != null) title.Text = projectRow.Name;
            if (summary != null) if (projectRow != null) summary.Text = projectRow.Summary;

            if (projectRow != null && projectRow.Description.Length > 0)
            {
                if (link != null)
                {
                    link.NavigateUrl = string.Format("/experience/details/?id={0}",
                                                     projectRow.ProjectID);
                    link.Visible = true;
                }
            }

            if (listitem != null) listitem.Visible = true;
4

0 に答える 0