亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

ASP.NET學習中常見錯誤總結(jié)歸納

 更新時間:2021年09月26日 15:15:57   作者:意愿三七  
這篇文章主要介紹了asp.net學習過程中碰到的常見錯誤的解決方法,通讀本篇對大家的學習或工作具有一定的價值,需要的朋友可以參考下

前言

自己在學習.NET中常犯的錯誤(持續(xù)更新)

下拉框綁值

		public void ddlist()
    	{
        this.DropDownList1.DataTextField = "DeviceName";
        this.DropDownList1.DataValueField = "DeviceID";
        this.DropDownList1.DataSource = dbl.ddlist();      
        this.DropDownList1.DataBind();
        this.DropDownList1.Items.Insert(0, new ListItem("全部", "0"));
    	}

this.DropDownList1.DataTextField = “DeviceName”;

DataTextField :顯示給用戶看的數(shù)據(jù)庫列
DataValueField:綁定數(shù)據(jù)源等于綁定唯一標識列
DataSource:數(shù)據(jù)源,綁定sql語言可以顯示數(shù)據(jù)
DataBind:綁定數(shù)據(jù)才可以顯示出來,是一個函數(shù)

Items.Insert(0, new ListItem(“全部”, “0”));

ltems表示集合
insert()兩個參數(shù) (int index,Ltems item)

最后效果:

在這里插入圖片描述

綁值GridView

public void jiaz()
    {
        this.GridView1.DataSource = dbl.show();
        this.GridView1.DataBind();
    }

DataSource:數(shù)據(jù)源,綁定sql語言可以顯示數(shù)據(jù)
DataBind:綁定數(shù)據(jù)才可以顯示出來,是一個函數(shù)

最后效果:

在這里插入圖片描述

刪除數(shù)據(jù)

  • 點擊刪除

CommandAgument和CommandName 配合一起使用,一般習慣用于刪除
會在RowCommand事件執(zhí)行

先綁定ID

在這里插入圖片描述

再綁定CommandName

在這里插入圖片描述

進入Rowcommand事件里面

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName=="del")
        {
            int id = int.Parse(e.CommandArgument.ToString());
            bool b1 = dbl.del(id);
            if (b1)
            {
                Response.Write("<script>alert('刪除成功')</script>");
                jiaz();
            }
            else
            {
                Response.Write("<script>alert('刪除失敗')</script>");
            }
        }
    }

e.CommandName=="del"

RowCommand無論怎么樣都會先來這里,所以判斷一下是不是要執(zhí)行刪除操作,根據(jù)e.CommandName==“del”

int id = int.Parse(e.CommandArgument.ToString());

刪除執(zhí)行的SQL語句是要根據(jù)ID唯一標識列來進行有目標的

修改

  • 點擊修改

修改CommandName為update,為了激發(fā)updateing事件

在這里插入圖片描述

綁定ID,這里是鍵值對

在這里插入圖片描述

為什么不綁定commandAgument呢,因為上面說了commandAgument,是要去Rowcommand事件配合使用的,我們把CommandName修改成為update,是要去Rowupdating事件

 protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        int id = int.Parse(this.GridView1.DataKeys[e.RowIndex]["DeviceID"].ToString());
        
        bool b1 = dbl.up(id);
        if (b1)
        {
            Response.Write("<script>alert('修改成功')</script>");
            jiaz(); 
        }
        else
        {
            Response.Write("<script>alert('修改失敗')</script>");
        }
    }

int id = int.Parse(this.GridView1.DataKeys[e.RowIndex][“DeviceID”].ToString());

修改SQL語句也是要獲取修改的唯一標識列
這個GridView1的DataKeys
[e.RowIndex] [“DeviceID”]:當前行的數(shù)據(jù)庫標識列

修改賦值到另外一個頁面

		Session["ID"] = this.GridView1.DataKeys[e.RowIndex]["BookID"].ToString();
        Label Booksname = (Label)this.GridView1.Rows[e. RowIndex].FindControl("Label2");
        Session["BookName"] = Booksname.Text;
        Response.Redirect("add.aspx");

Session[“ID”] = this.GridView1.DataKeys[e.RowIndex][“BookID”].ToString();

找到的ID賦值給session

Label Booksname = (Label)this.GridView1.Rows[e. RowIndex].FindControl(“Label2”);

找當前行的Label2控件

Session[“BookName”] = Booksname.Text;

把找到控件的值文本傳給session

Lable是類型,看Gridview是什么控件就轉(zhuǎn)換為什么類型
FindControl(找控件)

修改賦值到另外一個頁面綁定值

文本框綁定值

this.TextBox2.Text = Session["BookName"].ToString();

下拉框綁定值

if (Session["BookiS"].ToString().Contains("是"))
        {
            this.DropDownList1.SelectedIndex = 0;
        }
        else
        {
            this.DropDownList1.SelectedIndex = 1;
        }

判斷session里面是否包含這個值

this.DropDownList1.SelectedIndex = 0;

SelectedIndex = 0 代表 展示的是第一個

換頁不報錯

 protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        this.GridView1.PageIndex = e.NewPageIndex; //換頁不報錯
        jiazGridview();
    }

Gridview 換頁不報錯

到此這篇關(guān)于ASP.NET學習中常見錯誤總結(jié)歸納的文章就介紹到這了,更多相關(guān)ASP.NET 常見錯誤內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論