categorylist.aspx.cs

来自「microsoft公司在进行ASP.NET 2.0培训课时课堂操作演示代码。」· CS 代码 · 共 116 行

CS
116
字号
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class Categorylist : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!Page.IsPostBack)
        {
            this.GetCategoryDataBind();
            this.GetCountdata();
            Response.Write("你是第"+Application["usercount"].ToString()+"位访问本网站的客户!");
        }
    }


    private string  GetProfile()
    {
        string username = Request.QueryString["bname"].ToString();
        MembershipUser muser = Membership.GetUser(username);
        string uid = muser.ProviderUserKey.ToString();
        string constr = System.Configuration.ConfigurationManager.ConnectionStrings["ASPNETDBConnectionString"].ConnectionString;
        SqlConnection Con = new SqlConnection(constr);
        string sql = "select * from aspnet_Profile where userid='"+uid+"'";
        SqlDataAdapter Sda = new SqlDataAdapter(sql, Con);
        DataSet ds=new DataSet();
        Sda.Fill(ds,"aspnet_Profile");
        string pvalue = ds.Tables[0].Rows[0]["PropertyValuesString"].ToString();
        return pvalue;

    }
    private void Page_PreInit()
    {
        Theme = GetProfile();
    }
    private void GetCountdata()
    {
        if (Application["usercount"] != null)
        {
            Application["usercount"] = Int32.Parse(Application["usercount"].ToString()) + 1;
        }
        else
        {
            Application["usercount"] = "1";
        }
    }
    private void GetCategoryDataBind()
    {
        CategoryDB cdb = new CategoryDB();
        DataSet Ds = cdb.GetCategoryData();
        this.CategoryLBox.DataSource = Ds.Tables[0].DefaultView;
        this.CategoryLBox.DataTextField = "CategoryName";
        this.CategoryLBox.DataValueField = "CategoryId";
        this.CategoryLBox.DataBind();
    }
    protected void btnAddCategory_Click(object sender, EventArgs e)
    {
      if(this.txtAddCategoryName.Text.Trim()!="")
      {
          CategoryDB cdb = new CategoryDB();
          cdb.InsertCategoryData(this.txtAddCategoryName.Text.Trim());
          this.txtAddCategoryName.Text = "";
          this.GetCategoryDataBind();
      }
        else
        
        {
         Response.Write("<script>alert('值不能为空!')</script>");
        }
    }
    protected void btnEdit_Click(object sender, EventArgs e)
    {   if(this.txtEditCategoryName.Text!="")
    {
        CategoryDB cdb = new CategoryDB();
        cdb.UpdateCategoryData(this.txtEditCategoryName.Text,Int32.Parse(this.CategoryLBox.SelectedValue));
       this.txtEditCategoryName.Text="";
        this.GetCategoryDataBind();
    }
    else
    {
        Response.Write("<script>alert('请先选择值在更新!')</script>");
    }

    }
    protected void btnDelete_Click(object sender, EventArgs e)
    {
             if(this.CategoryLBox.SelectedIndex>-1)
             {
                 CategoryDB cdb = new CategoryDB();
                 cdb.DeleteCategoryData(Int32.Parse(this.CategoryLBox.SelectedValue));
                 this.GetCategoryDataBind();
             }
             else
             {
                 Response.Write("<script>alert('请先选择值在更新!')</script>");
             
             }
    }
    protected void CategoryLBox_SelectedIndexChanged(object sender, EventArgs e)
    {
    if(this.CategoryLBox.SelectedIndex>-1)
    {
        this.txtEditCategoryName.Text = this.CategoryLBox.SelectedItem.Text;
    }
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?