首页 新闻 搜索 专区 学院

ASp.NEt 多层架构,请问不用GrewView、DetailsView等控件,如何把数据呈现在页面

0
悬赏园豆:20 [已解决问题] 解决于 2017-09-28 15:53

1、业务实体:

   public class Article

  {

      private int _articleId;

private string _title;

       private string _articleDetails; 

 

     public int ArticleId

     {

       get { return _articleId; }

       set { _articleId = value; }

     }  

}

2、通用的SQLHelper 方法:

/// <summary>

        /// 执¡ä行D有®D参?SQL语®?句?,ê?并¡é返¤¦Ì回?SqlDataReader

        /// </summary>

        public static SqlDataReader GetReader(string sql, params SqlParameter[] values)

        {

            SqlCommand cmd = new SqlCommand(sql, Connection);

            cmd.Parameters.AddRange(values);

            SqlDataReader reader = cmd.ExecuteReader();

            return reader;

        }

 

3、数据访问层方法:

数据填充方法:

  private static Article FillRecord(SqlDataReader reader)

        {

            Article a = new Article();

 

            a.ArticleId = Convert.ToInt32(reader["articleId"]);

            a.ChannelId = Convert.ToInt32(reader["channelId"]);

            a.Title = Convert.ToString(reader["title"]);

            a.ArticleDetails = Convert.ToString(reader["articleDetails"]);

          return a;    

   }

   

数据访问层代码:

public static Article GetArticleByArticleId(int id)

  {

  string sql = "select * from article where articleId=@articleId";

  try

  {

  using (SqlDataReader reader = SqlHelper.GetReader(sql, new SqlParameter("@articleId", id)))

  {

  if (reader.Read())

  {  return FillRecord(reader);  }

  else

  {  return null;  }

  }

  }

  catch (Exception e)

  {

  Console.WriteLine(e.Message);     throw e;

  }

  }

5、业务逻辑方法:

public static class ArticleManager

    {

        public static Article GetArticlesByArticleId(int id)

        {

            return ArticleService.GetArticleByArticleId(id);

        }

}

5、页面表现层:

 

<div>

        <ul>

            <li> 编号:&nbsp;         </li>

            <li>标题: &nbsp;         </li>

            <li>内容: &nbsp;         </li>

………..

        </ul>

    </div>

6、请问不用ASP.Net的GrewView等控件,如何掉调用  业务逻辑层 。 把数据呈现在页面

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

    {

        protected void Page_Load(object sender, EventArgs e)

        {

 

            Article a = ArticleManager. GetArticlesByArticleId (1);

          

 

 

        }

WesternWind的主页 WesternWind | 初学一级 | 园豆:7
提问于:2011-12-29 17:18
< >
分享
最佳答案
0

用别的控件行吗?比如Label,把相应的属性值赋值给Label的Text

收获园豆:20
yanzhe | 菜鸟二级 |园豆:213 | 2011-12-29 17:40

比如是否可以而类似于JSp,这样来用:

编号:<%=a.ArticleId%>

标题:<%=a.Title%>

WesternWind | 园豆:7 (初学一级) | 2011-12-29 17:44

我用了标签,但是页面没有显示:

 protected void Page_Load(object sender, EventArgs e)
        {

            if (Page.IsPostBack)
            {
                Article a = new Article();
                a = ArticleManager.GetArticlesByArticleId(200);

                Label1.Text = a.ArticleId.ToString();
                Label2.Text = a.ArticleDetails.ToString();
            }

        }

WesternWind | 园豆:7 (初学一级) | 2011-12-29 17:45

@WesternWind: 用Label可以啊,只要确保有值

yanzhe | 园豆:213 (菜鸟二级) | 2011-12-29 18:02

@yanzhe: 

请问 ,对于数据集如何循环显示呢?
  public static IList<Article> GetAllArticles()
  {
  return ArticleService.GetAllArticles();
  }

 

WesternWind | 园豆:7 (初学一级) | 2011-12-29 18:24

 用这个  返回一个Article对象可以显示啦

if (!IsPostBack)
            {
                Article a = new Article();
                a = ArticleManager.GetArticlesByArticleId(200);

                Literal1.Text = a.ArticleId.ToString();

                Literal2.Text= a.ArticleDetails.ToString();
                

            }

WesternWind | 园豆:7 (初学一级) | 2011-12-29 18:25

@yanzhe: 如果返回一个IList<Article>,该如何呈现呢 !?

<div>

        <ul>

            <li> 编号:&nbsp;         </li>

            <li>标题: &nbsp;         </li>

            <li>内容: &nbsp;         </li>

………..

        </ul>

    </div>

WesternWind | 园豆:7 (初学一级) | 2011-12-29 18:27

@WesternWind: 那直接在前台有个Table,别用列表了,在后台填充Table就可以了。

        List<Artical> lists = ArticleManager.GetAtriclesByArticleId(200);
for (int i = 0; i < lists.Count; i++)
{
TableRow row = new TableRow();
Table1.Rows.Add(row);
TableCell cell0 = new TableCell();
cell0.Text = lists[i].ArticleId.ToString();
TableCell cell1 = new TableCell();
cell1.Text = lists[i].ChannelId.ToString();
TableCell cell2 = new TableCell();
cell2.Text = lists[i].Title.ToString();
TableCell cell3 = new TableCell();
cell3.Text = lists[i].ArticleDetails.ToString();
row.Cells.Add(cell0);
row.Cells.Add(cell1);
row.Cells.Add(cell2);
row.Cells.Add(cell3);
}

 

yanzhe | 园豆:213 (菜鸟二级) | 2011-12-30 09:48

@WesternWind: 

你写错了

if (Page.IsPostBack)  这是页面回传。

加个非 if (!Page.IsPostBack)

 

俺不是肥熊猫 | 园豆:18 (初学一级) | 2012-01-28 00:36

@WesternWind: 用了标签,还要在后台页面调用  Page.DataBind();绑定数据才行。。。

土豆屋 | 园豆:354 (菜鸟二级) | 2012-08-01 00:02
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册