DataList控件使用!

 private void show()
    {
        SqlConnection con = new SqlConnection("server=ZMQHBD2007;uid=sa;pwd=123;database=data;");
        con.Open();
        SqlCommand cmd = new SqlCommand("select top 3 * from category",con);
        SqlDataReader sdr = cmd.ExecuteReader();
        DataList1.DataSource = sdr;
        DataList1.DataBind();
        sdr.Close();
        con.Close();     
     } //链接数据库
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            show();
        }
    }
    protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
    {
       
        if (e.CommandName.ToLower() == "show")
        {
            Label Label2 = (Label)e.Item.FindControl("Label2");
            Label Label3 = (Label)e.Item.FindControl("Label3");
            Label Label4 = (Label)e.Item.FindControl("Label4");
            Label Label5 = (Label)e.Item.FindControl("Label5");
            Label Label6 = (Label)e.Item.FindControl("Label6");
            if (Label2 != null && Label3 != null && Label4 != null && Label5 != null && Label6 != null)
            {
                Label1.Text = Label2.Text + " " + Label3.Text + " " + Label4.Text + " " + Label5.Text + " " + Label6.Text + " ";
            }
        }
    } //为dataList编写ItemCommand方法,它实现把控件的选中项(Item)的文本显示在Lable控件上。该事件使用CommandName属性标识触发事件的按钮,使用控件ID和方法FindControl()查找触发事件的按钮。

HTML代码:


       
          


       
       
          

              
              
              
              
              
          
       
       
           
categoryidParentIDcategorycategoryorderintro

       
  
     
        你选择的是: 

你可能感兴趣的:(.NET,server,null,button,asp,object,database)