asp.net 数据绑定的实例代码

这篇文章介绍了asp.net 数据绑定的实例代码,有需要的朋友可以参考一下
 
复制代码 代码如下:

public partial class _Default : System.Web.UI.Page
{
    protected string title="大家好";            //前台代码<title><%#title %></title>
    protected void Page_Load(object sender, EventArgs e)
    {
        DataSet ds = new DataSet();
        string sql = ConfigurationManager.ConnectionStrings["strsql"].ConnectionString;
        using (SqlConnection sqlCnn=new SqlConnection(sql))
        {
            using (SqlCommand sqlCmm=sqlCnn.CreateCommand())
            {
                sqlCmm.CommandText = "select * from List";
                SqlDataAdapter adapter = new SqlDataAdapter(sqlCmm);
                adapter.Fill(ds);
            }
            this.RadioButtonList1.DataSource = ds.Tables[0];
            this.RadioButtonList1.DataTextField = "listname";
            this.RadioButtonList1.DataValueField = "id";
            //this.RadioButtonList1.DataBind();
            this.CheckBoxList1.DataSource = ds.Tables[0];
            this.CheckBoxList1.DataTextField = "listname";
            this.CheckBoxList1.DataValueField = "id";
            //this.RadioButtonList1.DataBind();
            this.DataBind();
        }            //数据绑定到RadioButtonList,CheckBoxList
        if (!IsPostBack)
        {
            DataSet ds1 = new DataSet();
            using (SqlConnection sqlCnn1 = new SqlConnection(sql))
            {
                using (SqlCommand sqlCmm1 = sqlCnn1.CreateCommand())
                {
                    sqlCmm1.CommandText = "select provinceid,provincename from Province";
                    SqlDataAdapter adapter = new SqlDataAdapter(sqlCmm1);
                    adapter.Fill(ds1);
                    this.DropDownList1.DataSource = ds1.Tables[0];
                    this.DropDownList1.DataTextField = "provincename";
                    this.DropDownList1.DataValueField = "provinceid";
                    this.DropDownList1.DataBind();
                }
            }
        }
    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        DataSet ds = new DataSet();
        string str = ConfigurationManager.ConnectionStrings["strsql"].ConnectionString;
        using (SqlConnection sqlCnn = new SqlConnection(str))
        {
            using (SqlCommand sqlCmm = sqlCnn.CreateCommand())
            {
                sqlCmm.CommandText = "select cityid,cityname from City where provinceid='" + this.DropDownList1.SelectedValue + "'";
                SqlDataAdapter adapter = new SqlDataAdapter(sqlCmm);
                adapter.Fill(ds);
                this.DropDownList2.DataSource = ds.Tables[0];
                this.DropDownList2.DataTextField = "cityname";
                this.DropDownList2.DataValueField = "cityid";
                this.DropDownList2.DataBind();
            }
        }
    }//实现省市二级联动
}

时间: 2024-08-03 17:54:34

asp.net 数据绑定的实例代码的相关文章

asp.net 数据绑定的实例代码_实用技巧

复制代码 代码如下: public partial class _Default : System.Web.UI.Page{    protected string title="大家好";            //前台代码<title><%#title %></title>    protected void Page_Load(object sender, EventArgs e)    {        DataSet ds = new Da

asp用户登录模块实例代码

 asp用户登录模块实例代码: 用户登录验证脚本,Chkpwd.asp 以下为引用的内容: <%  '=======用户登录验证脚本=======  '如果尚未定义Passed对象,则将其定义为false,表示没有通过验证  If IsEmpty(Session("Passed")) Then  Session("Passed")=false  End If  'Session("Passed")=False,表示尚未通过验证,则开始读取从

ASP.NET存取XML实例代码与注解

  ASP.NET存取XML实例代码: using System.Xml; 初始化一个xml实例 XmlDocument xml=new XmlDocument(); 导入指定xml文件 xml.Load(path); xml.Load(HttpContext.Current.Server.MapPath("../7in10.xml")); 指定一个节点 XmlNode root=xml.SelectSingleNode("/root"); 获取节点下所有直接子节点

一款asp.net GridView 分页实例代码

一款asp教程.net gridview 分页实例代码 本款gridview 分页实例,他从数据库教程到分页sql全部有,只要你按照本实例来做,完全可以实例asp.net教程 gridview 分页功能.*/ create procedure [dbo].[sp_systemquotationrevisionpaging] ( @clientid nvarchar(255), @clientmodel nvarchar(255),  @collection nvarchar(255), @sta

浅谈ASP.NET的Postback 实例代码第1/2页_实用技巧

我们知道,无论是ASP.NET1.x,2.0,甚至是以后的版本,ASP.NET最终Render到Client端通过浏览器浏览的都是一样:一个单纯的HTML.Client通过Submit Form的方式将填入Form的数据提交给Server进行处理.我们现在来看看ASP.NET整个Postback程序处理的过程. 首先我们通过一个Sample来看ASP.NET如何处理一个通过Click一个Button引起的Postback.下面是Web Page的HTML: 复制代码 代码如下: <%@ Page

ASP程序实现查看ASP文件源码实例代码

程序   <%   SUB PrintLine (ByVal strLine)    strLine=server.HTMLEncode(strLine)    strLine=replace(strLine,"<%","<FONT COLOR=#ff0000><%")    strLine=replace(strLine,"%>","%></FONT>")    strL

Asp.net 无限级分类实例代码_实用技巧

在网上找了很久也没有找到例子,没办法了,想偷懒也偷不成了,还是自己老老实实的写一个吧.不多废话,下面请看我做的详细方法. 1.无限级分类肯定要数据库支持啦,用户可以动态添加他们的分类嘛,下面是数据库的结构: 数据库主要有三个字段,这是最基本的了.第一个是主键,第二个是分类的名称了,第三个是父级目录id号. 2.成品的效果图:(大家最关心的是不是这个啊?看看是什么样子再看程序怎么吧) 看!!就长这个模样啦,这个层次关系就是上面那个数据所显示的结果.怎么样?还行吧... 3.最后就是提供程序咯,程序

ASP 多关键词查询实例代码_应用技巧

程序代码: 以下为引用的内容: 复制代码 代码如下: '关键字的搜索 str="select * from tableName where id>4" if srhKey<>"" then keyArr=split(srhKey," ") j=UBound(keyArr) t=0 dim keystrArr() for i=0 to j if keyArr(i)<>"" then redim pr

ASP 多关键词查询实例代码

程序代码: 以下为引用的内容: 复制代码 代码如下: '关键字的搜索 str="select * from tableName where id>4" if srhKey<>"" then keyArr=split(srhKey," ") j=UBound(keyArr) t=0 dim keystrArr() for i=0 to j if keyArr(i)<>"" then redim pr