您的位置:首页 > 编程语言 > ASP

ASP.NET 控件详解

2013-06-26 19:16 78 查看

1,验证控件

RequiredFieldValidator(必须字段验证)用于检查是否有输入值,限制空字段
CompareValidator(比较验证)按设定比较两个输入
RangeValidator(范围验证)输入是否在指定范围
RegularExpressionValidator(正则表达式验证)正则表达式验证控件
CustomValidator(自定义验证)自定义验证控件,通过客户端或服务器函数检查值
ValidationSummary(验证总结)总结验证结果,在页面中列出所有控件的验证错误

验证控件详解:http://www.51cto.com/specbook/14/3305.htm

2,数据绑定控件(Repeater,DataList,GridView)

repeater控件

当我们应用repeater控件绑定数据时,即使设置了DataSource,因为Repeater控件不具备内置的呈现功能,所以我们得用一些模板来实现他的数据呈现,它支持如下模板
1,ItemTemplate 要为数据源中每个数据项都要呈现一次的
HTML 元素和控件。
2,AlternatingItemTemplate
通常,可以使用此模板为交替项创建不同的外观,例如指定一种与在 ItemTemplate中指定的颜色不同的背景色。
3,HeaderTemplate 页眉设置
4,FooterTemplate 页脚设置
5,SeparatorTemplate 包含在每项之间呈现的元素。典型的示例可能是一条直线(使用
hr 元素)。

使用repeater分页功能:
举例:

protected void Page_Load(object sender,EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                this.lbNow.Text ="1";
                RepeterBind();
            }
        }
 
        private void RepeterBind()
        {
            int curPage = Convert.ToInt32(this.lbNow.Text);
            SqlConnection con = DB.GetCon();
            SqlDataAdapter sd = newSqlDataAdapter();
            sd.SelectCommand = newSqlCommand("select * from emp", con);
            DataSet ds = new DataSet();
            sd.Fill(ds, "emp");
            PagedDataSource ps = newPagedDataSource();
            ps.DataSource =ds.Tables["emp"].DefaultView;
            ps.AllowPaging = true;
            ps.PageSize = 2;
            //当前页数的索引
            ps.CurrentPageIndex = curPage-1;
            btnNext.Enabled = true;
            btnUp.Enabled = true;
            if (curPage == 1)
            {
                btnUp.Enabled = false;
            }
            if (curPage == ps.PageCount )
            {
                btnNext.Enabled = false;
            }
            Repeater1.DataSource = ps;
            Repeater1.DataBind();
        }
 
        protected void btnUp_Click(objectsender, EventArgs e)
        {
            lbNow.Text =Convert.ToString (((Convert.ToInt32(this.lbNow.Text)) - 1));
            RepeterBind();
        }
 
        protected void btnNext_Click(objectsender, EventArgs e)
        {
            lbNow.Text =Convert.ToString (((Convert.ToInt32(this.lbNow.Text)) + 1));
            RepeterBind();
        }


DataList控件

DataList控件,类似于 Repeater 控件,用于显示限制于该控件的项目的重复列表。相比Repeater多了两个模板分别为SelectedItemTemplate和EditltemTemplate
repeater控件在使用时不能右键编辑模板的,为datalist控件可以
SelectedItemTemplate 获取或设置控件中选中项的模板
EditltemTemplate 获取或设置
DataList控件中为进行编辑而选定的项的模板
DataList实现分页与Repeater控件一样(代码同上)
实现DataList或Repeater控件的分页显示有几种方法:
1、写一个方法或存储过程,根据传入的页数返回需要显示的数据表(DataTable)
2、使用PagedDataSource类(位于System.Web.UI.WebControls命名空间里)

GridView控件

GridView控件是Asp.Net2003中DataGrid控件的升级版本,它提供了比DataGrid更强大的功能,同时比DataGrid更加易用。
GridView控件是一个功能强大的控件。它可以使用数据绑定技术,在数据初始化的时候绑定一个数据源,从而显示数据。除了能够显示数据外,还可以实现编辑、排序和分页等功能,而这些功能的实现有时可以不写代码或写很少的代码。
实现分页:
html代码:

<asp:GridViewID="GridView1" runat="server"AllowPaging="True"  
       onpageindexchanging="GridView1_PageIndexChanging" PageSize="3"> 
    </asp:GridView>


public class DB
    {
        public static SqlConnection createCon()
        {
            return newSqlConnection("server=.;database=Department;uid=sa;pwd=123456");
        }
    }
 
 protected void Page_Load(object sender,EventArgs e)
        {
            SqlDataAdapter sda = newSqlDataAdapter();
            SqlConnection con = DB.createCon();
            SqlCommand cmd = new SqlCommand();
            string sqlStr = "select * fromemp";
            sda.SelectCommand = newSqlCommand(sqlStr, con);
            DataSet ds = new DataSet();
            sda.Fill(ds, "employee");
            GridView1.DataSource =ds.Tables["employee"].DefaultView;
            GridView1.DataBind();
        }
      
        protected voidGridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView1.PageIndex =e.NewPageIndex;         
                          //绑定
            SqlDataAdapter sda = newSqlDataAdapter();
            SqlConnection con = DB.createCon();
            sda.SelectCommand = newSqlCommand("select * from emp", con);        
            DataSet ds = new DataSet();
            sda.Fill(ds, "employee");
            GridView1.DataSource = ds;
            GridView1.DataBind();
        }


当然如果您正使用SqlDataSource 控件,并将其 DataSourceMode 属性设置为DataReader,则 GridView 控件无法实现分页。

数据行的选定:
//鼠标移动到哪行,哪行的背景色改变。
   protected voidGridView1_RowDataBound1(object sender, GridViewRowEventArgs e)
       {
           if (e.Row.RowType ==DataControlRowType.DataRow)
           {
              e.Row.Attributes.Add("onmouseover","c=this.style.backgroundColor;this.style.backgroundColor='#ffcc33'");
              e.Row.Attributes.Add("onmouseout","this.style.backgroundColor=c;");
           }
       }


3,用户控件

用户控件:它是一小段页面,可以包括静态HTML 代码和 Web 服务器控件。用户控件的好处是一旦创建了它,就可以在同一个 Web 应用程序的多个页面重用它。用户控件可以加入自己的属性,事件和方法。
一个web用户控件与一个完整的web窗体页相似,只是用户控件的扩展名为.ascx,且用户控件中不包含<html>,<body>和<form>元素
创建步骤:
-创建一个web项目
-在“项目”菜单中单击“添加web用户控件”
-将文本和控件添加到设计图面
-希望能够以编程方式访问的所有控件都必须是web窗体服务器控件或html服务器控件
-使用web窗体设计器设置属性并创建控件所需的任何代码

举例:用户控件
private string newTypeID;
        public string NewTypeID
        {
            set { this.newTypeID = value; }
        }
        protected void Page_Load(object sender,EventArgs e)
        {
            if (!this.IsPostBack )
            {
                SqlConnection con =DB.createCon();
              
                SqlCommand cmd = newSqlCommand("select newsTypeName from newsType where newsTypeID='" +this.newTypeID  + "'", con);
                con.Open();
                string newTypeName =Convert.ToString(cmd.ExecuteScalar());
                this.Label1.Text = newTypeName;
                cmd.CommandText = "select* from newsMaster where newsTypeID='" + this.newTypeID + "'";
                this.GridView1.DataSource =cmd.ExecuteReader();
                this.GridView1.DataBind();
            }
        }


html代码中给出标题NewTypeID

<tdalign="center" class="style4">
             <uc2:NewsID="News1" runat="server"NewTypeID="NT10001"/>
         </td>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: