ASP. Net learning

Time:2022-6-20

preface

I am learning Net (continuous update)

Drop down box binding value

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 ("all", "0");
    	}

this.DropDownList1.DataTextField = “DeviceName”;

Datatextfield: database columns displayed to users
Datavaluefield: binding data source is equal to binding unique identification column
Datasource: data source, bound to SQL language to display data
Databind: it can only be displayed after binding data. It is a function

Items. Insert (0, new listitem (“all”, “0”);

Ltems represents a collection
Insert() two parameters (int index, ltems item)

Final effect:

在这里插入图片描述

Bound value GridView


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

Datasource: data source, bound to SQL language to display data
Databind: it can only be displayed after binding data. It is a function

Final effect:

在这里插入图片描述

Delete data

  • Click Delete

Commanddocument and commandname are used together. They are usually used to delete
Will be executed in the rowcommand event

Bind ID first

在这里插入图片描述

Rebinding commandname

在这里插入图片描述

Enter the rowcommand event

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 ('delete succeeded ') </script>");
                jiaz();
            }
            else
            {
                Response. Write ("<script>alert ('delete failed ') </script>");
            }
        }
    }

e.CommandName==”del”

Rowcommand will come here first no matter what, so judge whether to delete or not according to e.commandname== “del”

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

Deleting the executed SQL statement is targeted according to the ID unique identification column

modify

  • Click Modify

Change the commandname to update to trigger the updating event

在这里插入图片描述

Binding ID, here is the key value pair

在这里插入图片描述

Why not bind the commanddocument? Because the commanddocument mentioned above is used in conjunction with the rowcommand event. We change the commandname to update to use the rowupdating event

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 ('modified successfully ') </script>");
            jiaz(); 
        }
        else
        {
            Response. Write ("<script>alert ('modify failed ') </script>");
        }
    }

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

Modifying the SQL statement is also the unique identification column to be modified
Datakeys of this gridview1
[e.rowindex] [deviceid]: database identification column of the current row

Modify assignment to another page


		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();

Assign the found ID to the session

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

Find the label2 control of the current row

Session[“BookName”] = Booksname.Text;

Pass the value text of the found control to the session

Label is a type. You can convert the type according to the GridView control
Findcontrol (find control)

Modify the binding value assigned to another page

Text box binding value


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

Drop down box binding value

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

Determine whether the session contains this value

this.DropDownList1.SelectedIndex = 0;

Selectedindex = 0 means the first one is displayed

Page change without error

protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        this. GridView1.PageIndex = e.NewPageIndex; // Page change without error
        jiazGridview();
    }

GridView page change without error

This is about asp Net learning common errors summary article introduced here, more related asp Net common error content, please search the previous articles of developeppaer or continue to browse the relevant articles below. I hope you can support developeppaer in the future!