Asp.net learning journey 5 (gridview, part 1)

Source: Internet
Author: User
1. Data Binding Query
This code is more common. It is mainly used to clarify the ideas. Different implementations can be implemented. Sqlconnection sqlcon;
Sqlcommand sqlcom;
String strconn = "Data Source = (local); database = testdb; uid = sa; Pwd = 123456"; // connection string

String strsql = "select * From tablename where ";
Sqlcon = new sqlconnection (strconn );

// Open the data connection
Sqldataadapter myda = new sqldataadapter (strsql, sqlcon );
Dataset myds = new dataset ();
Sqlcon. open ();

// Bind the result to the specified Dataset
Myda. Fill (myds, "cuabase ");
Gridview1.performanceid = NULL;
Gridview1.datasource = myds;
Gridview1.datakeynames = new string [] {"field name "};

// Display and close the connection
Gridview1.databind ();
Sqlcon. Close ();

2. Custom super connection
The editing function provided by the gridview is very weak. It usually jumps to the corresponding page after clicking edit.
First edit the field to add a column...

Then add the field flag of the data to be transmitted through get in the property> datanavigateurlfeild.
Add the format of the page to be dropped in datanavgateurlfromat
For example, page. aspx? Cusid = {0} 0 is a placeholder

Add the page to jump to in behavior-> navigateurl

To receive data on the corresponding interface, the Code is as follows.

String strname = httpcontext. Current. Request. querystring ["cusid"];
// Long id = long. parse (this. Request. querystring ["cusid"]. tostring ());
String strres = "this is the response from the server: \ r \ n" + "hello," + strname + "! ";

Httpcontext. Current. response. Clear (); // clear all content output in the buffer stream.

Httpcontext. Current. response. Write (strres); // write the information to the HTTP Response output stream.

Httpcontext. Current. response. Flush (); // send the Current Buffer output to the client.

Httpcontext. Current. response. End (); // send all the current buffered output to the client, stop the execution of the page, and raise an endrequest event (stop the request ).

3. process the checkbox in the gridview
(1) Check selection and determination of the record to obtain the index of the selected record Protected void button2_click (Object sender, eventargs E)
{
Textbox2.text = "";

For (INT I = 0; I <gridview1.rows. Count; I ++)
{
// Line I of gridview1 and column J of gridview1
// Checkbox chk = (checkbox) gridview1.rows [I]. cells [J]. findcontrol ("checkbox2 ");

Checkbox chk = (checkbox) gridview1.rows [I]. cells [0]. findcontrol ("checkbox2 ");
If (chk. Checked = true)
{
Textbox2.text = textbox2.text + (I + 1). tostring ();
}
}

}

(2) Check all options
Add a check change event. If it is selected, all the checkboxes of all records are selected, which works best with Ajax.
Protected void checkbox#checkedchanged (Object sender, eventargs E)
{
Int I;
If (checkbox) sender). Checked)
{
For (I = 0; I <gridview1.rows. Count; I ++)
{
(Checkbox) gridview1.rows [I]. findcontrol ("checkbox2"). Checked = true; // locate the template id value of the column
}
}
Else
{
For (I = 0; I <gridview1.rows. Count; I ++)
{
(Checkbox) gridview1.rows [I]. findcontrol ("checkbox2"). Checked = false;
}
}
}

Today is here... this is just the first article... but as you get to know it slowly, there may be another article... next article 1, next article 2, next article n ......

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.