ASP.NET学习中常见错误总结归纳

2022-04-14 17:59:54
目录
前言下拉框绑值绑值GridView删除数据修改修改赋值到另外一个页面修改赋值到另外一个页面绑定值换页不报错

前言

自己在学习.NET中常犯的错误(持续更新)

下拉框绑值

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 :显示给用户看的数据库列标识列

修改赋值到另外一个页面

RZWbEAJbSkSession["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是什么控件就转换为什么类型
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 换页不报错