go visit:
http://www.dotnetjunkies.com/quickstart/aspplus/here is a simple example:<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %><html>
<script language="C#" runat="server">    protected void Page_Load(Object sender, EventArgs e) 
    {
        SqlConnection myConnection = new SqlConnection("server=localhost;database=pubs;uid=sa;pwd=;");
        SqlDataAdapter myCommand = new SqlDataAdapter("select * from Authors", myConnection);        DataSet ds = new DataSet();
        myCommand.Fill(ds, "Authors");        MyDataGrid.DataSource=ds.Tables["Authors"].DefaultView;
        MyDataGrid.DataBind();
    }</script><body>  <h3><font face="Verdana">Simple Select to a DataGrid Control</font></h3>  <ASP:DataGrid id="MyDataGrid" runat="server"
    Width="700"
    BackColor="#ccccff" 
    BorderColor="black"
    ShowFooter="false" 
    CellPadding=3 
    CellSpacing="0"
    Font-Name="Verdana"
    Font-Size="8pt"
    HeaderStyle-BackColor="#aaaadd"
    EnableViewState="false"
  /></body>
</html>

解决方案 »

  1.   

    我给一个datalist的例子,包括分页,查询
    <Script Language="c#" RunAt="Server">
    string pdstr="",stocknumstr="",authorstr="",dpdl1str="",selectcmdstr="",regname="";
    int PageSize;
    int CurrentPage,RecordCount,PageCount;
    SqlConnection myconn=new SqlConnection("server=localhost;uid=sa;pwd=;database=works");
    void Page_Load( Object Src, EventArgs e )
    {
    //处理传递过来的参数
         try
         {
            regname=Request.QueryString["id"].ToString();
         }
         catch(Exception f)
         {
            regname="everyone";
         }
      //初始化在线专家列表   
         SqlDataAdapter sqlstr=new SqlDataAdapter("select * from techstock_member where isexpert='1' ",myconn);
         DataSet ds=new DataSet();
         sqlstr.Fill(ds,"expert");
         datalist1.DataSource=ds.Tables["expert"].DefaultView;
         datalist1.DataBind();
      //dropdownlist专家捆绑
         SqlDataAdapter sqlstr1=new SqlDataAdapter("select count(*) as tt from techstock_member where isexpert='1'",myconn);
         DataSet ds1=new DataSet();
         sqlstr1.Fill(ds1,"expert1");
         DataTable dt1=new DataTable();
         dt1=ds1.Tables["expert1"];
         int total=Convert.ToInt32(dt1.Rows[0][0].ToString());
         
         DataTable dt=new DataTable();
         dt=ds.Tables["expert"];
         ArrayList list1=new ArrayList();
         int i;
         list1.Add("选择经纪人");
         for(i=0;i<total;i++)
         {
            list1.Add(dt.Rows[i][19].ToString());
         }
         
         dpdl1.DataSource=list1;
         dpdl1.DataBind();
     //每页显示数目 
         PageSize =20;
         if(!Page.IsPostBack)
            {
      //初始化
            pdstr="all";
            selectcmdstr=select(pdstr);
            ViewState["select"]=selectcmdstr;
            init();
            ListBind();
            }
    }//初始化函数
    public void init()
    {
            RecordCount = CalculateRecord(pdstr);
    ViewState["RecordCount"] =RecordCount;
    ViewState["PageIndex"]=0;
    //计算总共有多少页
    PageCount = pagenum(RecordCount,PageSize);
    ViewState["PageCount"] = PageCount;
    }
    public int pagenum(int a,int b)    
    {
        int total;
        int fre;
        fre=a%b;
        if(fre!=0)
          {
            total=(int)a/b+1;
          }
          else
          {
            total=(int)a/b;
          }
          return total;
    }
    //计算总共有多少条记录
    public int CalculateRecord(string poststr)
    {
        string cmdstr="";
        switch(poststr)
           {
            case "all":
                 cmdstr="select count(*) as co from online_question ";
                 break;
            case "stock":
                 cmdstr="select count(*) as co from online_question where stock_number like '"+stocknumstr+"'";
                 break;
            case "author":
                 cmdstr="select count(*) as co from online_question where user_name like'"+authorstr+"'";
                 break;
            case "expert":
                 cmdstr="select count(*) as co from online_question where expert_name like'"+dpdl1str+"'";
                 break;
            default:
            break;     
                           
           }
        
    int intCount;
    SqlDataAdapter sqlcmd1=new SqlDataAdapter(cmdstr,myconn);
    DataSet ds=new DataSet();
    sqlcmd1.Fill(ds,"count");
    DataTable dt=new DataTable();
    dt=ds.Tables["count"];
    string str1=dt.Rows[0][0].ToString();
    if(str1!="")
      {
        intCount=Convert.ToInt32(str1);
      }
      else
      {
        intCount=0;
      }
    return intCount;
    }  
    ICollection CreateSource()

    //设定选择语句
         String strcmd=ViewState["select"].ToString();      
         int StartIndex;
     CurrentPage=(int)ViewState["PageIndex"];
    //设定导入的起终地址
     StartIndex= CurrentPage*PageSize;
         SqlDataAdapter sqlstr2=new SqlDataAdapter(strcmd,myconn);
         DataSet ds2=new DataSet();
         sqlstr2.Fill(ds2,StartIndex,PageSize,"expert2");
         
         return ds2.Tables["expert2"].DefaultView;

    void ListBind()
    {
         
         mydatalist.DataSource=CreateSource();
         mydatalist.DataBind();
         //上下页的选择设置
         lbnNextPage.Enabled = true;
     lbnPrevPage.Enabled = true;
     if(CurrentPage==(PageCount-1)) lbnNextPage.Enabled = false;
     if(CurrentPage==0) lbnPrevPage.Enabled = false;
     lblcurrent.Text = (CurrentPage+1).ToString();
     lblRecordCount.Text = RecordCount.ToString();
     lblPageCount.Text = PageCount.ToString();
    }
    public void Page_OnClick(Object sender,CommandEventArgs e)
    {
    CurrentPage = (int)ViewState["PageIndex"];
    PageCount = (int)ViewState["PageCount"];
        RecordCount=(int)ViewState["RecordCount"];
    string cmd = e.CommandName;
    //判断cmd,以判定翻页方向
    switch(cmd)
    {
    case "next":
    if(CurrentPage<(PageCount-1)) CurrentPage++;
    break;
    case "prev":
    if(CurrentPage>0) CurrentPage--;
    break;

    } ViewState["PageIndex"] = CurrentPage;
        ListBind();

    }
    //按代码查询
    void go1(Object sender,ImageClickEventArgs e)
    {
    //通过pdstr的不同来定义sql语句选择参数
         
         stocknumstr=Request.Form["stocknum"] .ToString();
         pdstr="stock";
         select(pdstr);
         ViewState["select"]=selectcmdstr;
         init();
         ListBind();
         dpdl1.SelectedItem.Text="选择经纪人";
         author.Value="";
    }
    //按作者查询
    void go2(Object sender,ImageClickEventArgs e)
    {
    //通过pdstr的不同来定义sql语句选择参数
         
         authorstr=Request.Form["author"] .ToString();
         pdstr="author";
         select(pdstr);
         ViewState["select"]=selectcmdstr;
         init();
         ListBind();
         dpdl1.SelectedItem.Text="选择经纪人";
         stocknum.Value="";
    }
    //按专家查询
    void go3(Object sender,ImageClickEventArgs e)
    {
    //通过pdstr的不同来定义sql语句选择参数
         dpdl1str=Request.Form["dpdl1"] .ToString();
         pdstr="expert";
         select(pdstr);
         ViewState["select"]=selectcmdstr;
         init();
         ListBind();
         author.Value="";
         stocknum.Value="";
    }
    //选择合适的sql选择语句
    public string select(string pd)
    {
         switch(pd)
         {
            case "all":
                 selectcmdstr="select a.*,b.petname as petname from online_question as a inner join techstock_member as b on a.user_name=b.member_name order by a.id desc";
                 break;
            case "stock":
                 selectcmdstr="select a.*,b.petname as petname from online_question as a inner join techstock_member as b on a.user_name=b.member_name where a.stock_number like '"+stocknumstr+"'order by a.id desc";
                 break;
            case "author":
                 selectcmdstr="select a.*,b.petname as petname from online_question as a inner join techstock_member as b on a.user_name=b.member_name where a.user_name like'"+authorstr+"'order by a.id desc";
                 break;
            case "expert":
                 selectcmdstr="select a.*,b.petname as petname from online_question as a inner join techstock_member as b on a.user_name=b.member_name where a.expert_name like'"+dpdl1str+"'order by a.id desc";
                 break;
            default:
            break;    
         }
         return selectcmdstr;  
    }
    </script>