码迷,mamicode.com
首页 > Web开发 > 详细

WebForm数据绑定

时间:2017-08-06 22:58:33      阅读:249      评论:0      收藏:0      [点我收藏+]

标签:connect   ring   using   char   run   exec   oev   nic   --   

Users表

using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Web;

/// <summary>
/// Users 的摘要说明
/// </summary>
public class Users
{
    public Users()
    {

    }
    public int Ids { get; set; }
    public string Ucode { get; set; }
    public string UserName { get; set; }
    public string password { get; set; }
    public string NickName { get; set; }
    public bool Sex { get; set; }
    public string Sexstr
    {
        get
        {
            return Sex ? "" : "";
        }
    }
    public DateTime Birthday { get; set; }
    public string Nation { get; set; }
    public string NationName
    {
        get
        {
            string s = "";
            SqlConnection conn = new SqlConnection("server=.;database=Date0504;user=sa;pwd=y920106k");
            SqlCommand cmd = conn.CreateCommand();
            cmd.CommandText = "select * from UserNation where NationCode=@a";
            cmd.Parameters.Add("@a",Nation);
            conn.Open();
            SqlDataReader dr = cmd.ExecuteReader();
            if (dr.Read())
            {
                s = dr["NationName"].ToString();
            }
            conn.Close();
            return s;
        }

    }

 

UsersData

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;

/// <summary>
/// UsersData 的摘要说明
/// </summary>
public class UsersData
{
    SqlConnection conn;
    SqlCommand cmd;
    public UsersData()
    {
        conn = new SqlConnection("server=.;database=Date0504;user=sa;pwd=y920106k");
        cmd = conn.CreateCommand();
    }
    public List<Users >SelectAll()
    {
        List<Users> ulist = new List<Users>();
        cmd.CommandText = "select * from Users";
        conn.Open();
        SqlDataReader dr = cmd.ExecuteReader();
        while (dr.Read())
        {
            Users u = new Users();
            u.Ids = Convert.ToInt32(dr["Ids"]);
            u.Ucode = dr["Ucode"].ToString();
            u.UserName = dr["UserName"].ToString();
            u.password = dr["PassWord"].ToString();
            u.NickName = dr["NickName"].ToString();
            u.Sex = Convert.ToBoolean(dr["Sex"]);
            u.Birthday = Convert.ToDateTime(dr["Birthday"]);
            u.Nation = dr["Nation"].ToString();
            ulist.Add(u);
        }
        conn.Close();
        return ulist;
    }
}

 

Default

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>

            <table>
                <%--创建一个表格--%>
                <thead>
                    <%--表格的表头--%>
                    <tr>
                        <%--每一列的列名--%>
                        <td>Ids</td>
                        <td>编号</td>
                        <td>用户民</td>
                        <td>密码</td>
                        <td>昵称</td>
                        <td>性别</td>
                        <td>生日</td>
                        <td>民族</td>
                    </tr>
                </thead>
                <tbody> <%--表格的主体--%>
                   
                    <asp:Repeater ID="Repeater1" runat="server"><%--循环数据--%>
                        <ItemTemplate>
                            <tr>
                                <%--括号内为属性名--%>
                                <td><%#Eval("Ids") %></td>
                                <td><%#Eval("Ucode") %></td>
                                <td><%#Eval("UserName") %></td>
                                <td><%#Eval("Password") %></td>
                                <td><%#Eval("NickName") %></td>
                                <td><%#Eval("Sexstr") %></td><%--属性扩展的性别--%>
                                <td><%#Eval("Birthday","{0:yyyy年MM月dd日}") %></td><%--中括号内的0代表前面的数据看找后面的展示--%>
                                <td><%#Eval("NationName") %></td>
                            </tr>
                        </ItemTemplate>
                    </asp:Repeater>


                </tbody>


            </table>
        </div>
    </form>
</body>
</html>

 

Default右键查看代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Repeater1.DataSource = new UsersData().SelectAll();//指定数据源
        Repeater1.DataBind();//绑定数据源
    }
}

 

WebForm数据绑定

标签:connect   ring   using   char   run   exec   oev   nic   --   

原文地址:http://www.cnblogs.com/skyhorseyk/p/7296013.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!