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 的内部构造函数,因此该代码是不可行的。我认为这应该是可行的……我的大脑似乎在 Cinco de Mayo 之后才去度假。

更新:

使用以下方法使其工作。忘记将行初始值设定项设置为“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