A summary of one-day learning experience proficient in Asp.net

Source: Internet
Author: User

1. validator
2. ispostback
3. autopostback. Automatically post when the control leaves focus.
4. Use the Repeater control. : The Repeater control is easier to use than the previous version of Asp.net. You only need eval and do not need databinder. eval (container. dataitem, "***"): Now, you only need eval ("name"). Note that you cannot lose the previous "#".
<Asp: repeater id = "repeater1" runat = "server">
<Headertemplate>
Ga ga
</Headertemplate>
<Itemtemplate>
<% # Eval ("name") %>
<% # Eval ("DESC") %>
</Itemtemplate>
</ASP: repeater>

Protected void button3_click (Object sender, eventargs E)
{
List <person> List = new list <person> ();
List. Add (new person () {name = "Barbara", DESC = "White Teeth "});
List. Add (new person () {name = "Obama", DESC = ""});
Repeater1.datasource = List;
Repeater1.databind ();
}
5. datalist control:
(1) Highlight rows
<Asp: datalist id = "datalist1" runat = "server">
<Selecteditemstyle backcolor = "# ff6666"/>
<Itemtemplate>
<% # Eval ("name") %>
<% # Eval ("DESC") %>
<Asp: linkbutton id = "linkbutton1" runat = "server" text = "select" commandname = "select"/>
</Itemtemplate>
</ASP: datalist>
The core is the commandname attribute. The optional values include edit and delete. When a button is clicked, events such as editcommand and deletecommand are executed.
(2) Row in-place editing:
<Asp: datalist id = "datalist1" runat = "server"
Oneditcommand = "datalist1_editcommand">
<Selecteditemstyle backcolor = "# ff6666"/>
<Edititemtemplate>
<Asp: textbox runat = "server" id = "T1" text = '<% # eval ("name") %>'/>
<Asp: textbox runat = "server" id = "T2" text = '<% # eval ("DESC") %>'/>
<Asp: button runat = "server" text = "Submit" commandname = "Update"/>
</Edititemtemplate>
<Itemtemplate>
<% # Eval ("name") %>
<% # Eval ("DESC") %>
<Asp: linkbutton id = "linkbutton1" runat = "server" text = "edit" commandname = "edit"/>
</Itemtemplate>
</ASP: datalist>

Protected void datalist1_editcommand (Object source, datalistcommandeventargs E)
{
Datalist1.edititemindex = E. Item. itemindex;
Rebind ();
}

Private void rebind ()
{
List <person> List = new list <person> ();
List. Add (new person () {name = "Barbara", DESC = "White Teeth "});
List. Add (new person () {name = "Obama", DESC = ""});
Repeater1.datasource = List;
Repeater1.databind ();

Datalist1.datasource = List;
Datalist1.databind ();
}
(3) EDIT rows in place and submit modifications
<Asp: datalist id = "datalist1" runat = "server"
Oneditcommand = "datalist1_editcommand"
Onupdatecommand = "datalist1_updatecommand">
<Selecteditemstyle backcolor = "# ff6666"/>
<Edititemtemplate>
<Asp: textbox runat = "server" id = "T1" text = '<% # eval ("name") %>'/>
<Asp: textbox runat = "server" id = "T2" text = '<% # eval ("DESC") %>'/>
<Asp: button runat = "server" text = "Submit" commandname = "Update"/>
</Edititemtemplate>
<Itemtemplate>
<% # Eval ("name") %>
<% # Eval ("DESC") %>
<Asp: linkbutton id = "linkbutton1" runat = "server" text = "edit" commandname = "edit"/>
</Itemtemplate>
</ASP: datalist>
Public partial class _ default: system. Web. UI. Page
{
Protected void page_load (Object sender, eventargs E)
{
If (session ["personlist"] = NULL)
{
List <person> List = new list <person> ();
List. Add (new person () {name = "Barbara", DESC = "White Teeth "});
List. Add (new person () {name = "Obama", DESC = ""});
Repeater1.datasource = List;
Repeater1.databind ();
Session ["personlist"] = List;
}
}

Protected void datalist1_editcommand (Object source, datalistcommandeventargs E)
{
Datalist1.edititemindex = E. Item. itemindex;
Rebind ();
}

Private void rebind ()
{
Datalist1.datasource = session ["personlist"];
Datalist1.databind ();
}

Protected void datalist1_updatecommand (Object source, datalistcommandeventargs E)
{
Textbox nt1 = E. Item. findcontrol ("T1") as textbox;
Textbox nt2 = E. Item. findcontrol ("T2") as textbox;
// Do not retrieve data directly from datalist1.datasource, because null is obtained.
List <person> List = session ["personlist"] As list <person>;
Person curperson = list [datalist1.edititemindex];
Curperson. Name = nt1.text;
Curperson. DESC = nt2.text;
Datalist1.edititemindex =-1;
Rebind ();
}
}
6. gridview Control
<Asp: gridview id = "gridview1" runat = "server" allowsorting = "true"
Autogeneratecolumns = "false" onrowcommand = "gridview1_rowcommand"
Onsorting = "gridview1_sorting">
<Columns>
<Asp: buttonfield buttontype = "button" commandname = "dinggou" headertext = "order"
Showheader = "true" text = "order"/>
<Asp: buttonfield buttontype = "button" commandname = "tuiding" headertext = "Unsubscribe"
Showheader = "true" text = "Unsubscribe"/>
<Asp: boundfield datafield = "name" headertext = "name" sortexpression = "name"/>
<Asp: boundfield datafield = "DESC" headertext = "Description" sortexpression = "DESC"/>
</Columns>
</ASP: gridview>

Protected void gridview1_rowcommand (Object sender, gridviewcommandeventargs E)
{
If (E. commandname = "dinggou ")
{
Debug. writeline ("no." + E. commandargument + "Row ordered ");
}
}

Protected void gridviewinclusorting (Object sender, gridviewsorteventargs E)
{

}
7. User Control)
Create a usercontrol In the wizard, and edit the usercontrol as needed. You can also add attributes and events to the usercontrol. You only need to drag the control directly from solutionexplorer to the page.
8. inherit controls
(1) create a webcustomcontrol in the Wizard.
(2) define your own application interface. You need to reload the createchildcontrols method inherited from the control class and generate an interface control. If the custom control is used repeatedly on a page, it is recommended that implements system. Web. UI. inamingcontainer create a unique namespace for the control.
(3) define the message processing function of the control. Custom Controls contain two types of messages: messages generated by child controls and custom control messages.
9. Add "global application class" to the project to add global. asax, where you can monitor the application and session lifecycle.
10. (1) response. Redirect ("Newpage. aspx"); client forwarding
(2) server. Transfer ("Newpage. aspx"); server-side forwarding
11. Web. config Configuration
(1) <deleetask>
<Add key = "ftp" value = "127.0.0.1"/>
</Appsettings>
This. Title = webconfigurationmanager. deleettings ["ftp"];
(2)
<Connectionstrings>
<Add name = "mydb" connectionstring = "JDBC: DDD"/>
</Connectionstrings>
This. Title = webconfigurationmanager. connectionstrings ["mydb"]. connectionstring;
12. bulletedlist is <ul> <ol>
13. PostBack Essence
ASP. NET also adds two additional hidden input fields that are used to pass information
Back to the server. This information consists of the ID of the control that raised the event and
Any additional information that might be relevant. These fields are initially empty, as shown
Here:
<Input type = "hidden" name = "_ eventtarget" id = "_ eventtarget" value = ""/>
<Input type = "hidden" name = "_ eventargument" id = "_ eventargument" value = ""/>
The _ dopostback () function has the responsibility for setting these values with
Appropriate information about the event and then submitting the form. A slightly simplified
Version of the _ dopostback () function is shown here:
<Script language = "text/JavaScript">
Function _ dopostback (eventtarget, eventargument ){
VaR theform = Document. form1;
Theform. _ eventtarget. value = eventtarget;
Theform. _ eventargument. value = eventargument;
Theform. Submit ();
}
</SCRIPT>
14. Cross-page form submission
In page 1, specify the postbackurl attribute of the button as webform1.aspx, so that the form will be submitted to webform1.aspx. Then, in webform1.aspx, you can get all the values in the previous page:
Textbox1.text = previouspage. title;
You can also convert previouspage cast into a more detailed page subclass.
15. Method for retrieving querystring:
Request. querystring ["recordid"]
16. server. urlencode (lstitems. selecteditem. Text)
17. The multiview control is used to implement a dynamic interface. Multiple View Controls are nested in the multiview, and other controls can be used in each view control. You can control the display of different views by controlling the activeviewindex attribute of the multiview control.
18. The Wizard control is more like a tabcontrol than the multiview control.
19. Dynamic images:
In the pageload event:
Bitmap image = new Bitmap (300, 50 );
Graphics G = graphics. fromimage (image );
Response. contenttype = "image/PNG ";
Image. Save (response. outputstream,
System. Drawing. imaging. imageformat. GIF );
20 page navigation
Create a sitemap file and modify the sitemap file to add nodes.
Add a sitemapdatasource on the page, and drag the Treeview, menu, sitemappath and other controls to specify the datasource attribute as sitemapdatasource.
21 single-value binding
Url = "images/picture.jpg ";
This. databind ();
<Asp: checkbox id = "chkdynamic" text = "<% # URL %>" runat = "server"/>
22 bind the drop-down list box
<Asp: dropdownlist id = "dropdownlist1" runat = "server" datatextfield = "value"
Datavaluefield = "key">
</ASP: dropdownlist>
Idictionary <string, string> dict = new dictionary <string, string> ();
Dict ["1"] = "AA ";
Dict ["2"] = "BB ";
Dropdownlist1.datasource = dict;
Dropdownlist1.databind ();
23. Set the start page: Right-click aspx and select "set as startpage"
24 Program Database connection string settings
(1) Add the following to Web. config:
<Connectionstrings>
<Add name = "dbconnectionstring" connectionstring = "Server = 192.168.88.128 \ sqlexpress1; uid = sa; Pwd = 123456; database = CRM" providername = "system. Data. sqlclient"/>
</Connectionstrings>
(2) drag and drop the datasource component in the IDE and select dbconnectionstring in the connectionstring attribute of the attribute view.
(3) method for reading the connection string in the program:
System. configuration. Configuration rootwebconfig =
System. Web. configuration. webconfigurationmanager. openwebconfiguration ("~ ");
String connstring =
Rootwebconfig. connectionstrings. connectionstrings ["dbconnectionstring"]. connectionstring;
24. Steps for creating a simple crud page:
(1) drag and drop a sqldatasource component, set connectionstring, and name the component dslist.
(2) modify the deletequery attribute of the sqldatasource component to: delete from t_psi_user where FID = @ FID
Insertquery attributes: insert into t_psi_user (FID, fusername, fpassword) values (newid (), @ fusername, @ fpassword)
Selectquery: Select * From t_psi_user
Updatequery: Update t_psi_user set fusername = @ fusername, fpassword = @ fpassword where FID = @ FID
(3) drag and drop a gridview component and set its performanceid attribute to dslist. Set allowpaging, allowsorting, autogeneratedeletebutton, and autogenerateeditbutton to true. Set the autogeneratedcolumns attribute to false. Set the datakeynames attribute to FID (so that the edit and delete functions can be normally executed even if the FID field is hidden)
(4) modify the columns attribute of the gridview and click the refreshschema link in the pop-up dialog box. The fields FID, fname, and fpassword are displayed in boundfield, add the fusername and fpassword fields.
This eliminates the need for a row. Code The page with the deletion and modification functions is ready. Next we will do the "add" function.
(5) Select the gridview component, select edittemplete in the smart prompt, select "emptytemplete", drag and drop a formview component to emptytemplete, and select the formview component, set datasource to dslist in the smart prompt.
(6) create a new button and edit its Click Event Code as follows:
Gridview1.performanceid = "";
Gridview1.databind ();
(7) set the iteminserted Event code of formview:
Refreshlist ();
The refreshlist () function is defined as follows:
Gridview1.performanceid = "dslist ";
Gridview1.databind ();
In this way, the "add" function is ready, but there are still some shortcomings, that is, the FID field is not managed by us, and the field name and button are all in English.
(8) Select the formview component, click edittemplete, and select inserttemplete. In this way, you can delete unnecessary FID fields and modify the layout of the control and the language and text of the interface.
(9) In this case, the "cancel" button in the insert interface still cannot be used. Edit the itemcommand event of formview1 and write the following code:
If (E. commandname = "cancel ")
{
Refreshlist ();
}
25 The preceding crud implementation method has two drawbacks:
(1) You need to write an emptytemplete
(2) It is difficult to customize the Edit Control.
Therefore, we still use the listui and editui splitting method to solve the problem. Steps:
Create listui:
(1) Use datasource and gridview. However, you only need to configure selectquery and deletequery for datasource.
(2) The edit button is not automatically generated by the gridview.
(3) The gridview generates a buttonfield with the title "edit" and commandname = "editinpage"
Protected void gridview1_rowcommand (Object sender, gridviewcommandeventargs E)
{
If (E. commandname = "editinpage ")
{
Int Index = convert. toint32 (E. commandargument );
Guid = (guid) gridview1.datakeys [Index]. value;
Server. Transfer ("/sys/sysuseredit. aspx? Action = edit & FID = "+ guid );
}
}
(4) onclick event of the Add button:
Server. Transfer ("/sys/sysuseredit. aspx? Action = Insert ");

Edit UI:
(1) drag a datasouce control, configure insertcommand and updatecommand as usual, set selectcommand to "select * from [t_psi_user] where 1 <> 1", and set updatecommand to ""
(2) drag a formview and modify edittemplete and inserttemplte (copy the modified edittemplete to inserttemplte directly. Do not forget to modify the commandname of the button)
(3) code;
Protected void page_load (Object sender, eventargs E)
{
Switch (request ["action"])
{
Case "edit ":
Dsedit. selectcommand = "select * From t_psi_user where FID = @ FID ";
Dsedit. selectparameters. Clear ();
Dsedit. selectparameters. Add ("FID", request ["FID"]);
Formview1.changemode (formviewmode. Edit );
Break;
Case "insert ":
Formview1.changemode (formviewmode. insert );
Break;
}
}

Protected void formviewincluiteminserted (Object sender, formviewinsertedeventargs E)
{
Gogolist ();
}

Protected void formviewincluitemupdated (Object sender, formviewupdatedeventargs E)
{
Gogolist ();
}

private void gogolist ()
{< br> server. transfer ("/sys/sysuserlist. aspx ");
}< BR >}< br> 26. dropdownlist implements the basic data selector. For example, in product editing, select a measurement unit:
(1) drag a datasource for the t_measureunit table, for example, dsmeasureunit.
(2) drag the datasource of a product, such as dsmerchan.
(3) drag a formview and set its datasource to dsmerchan
(4) Put A dropdownlist in formview, because only in this way can the binding of dropdownlist itself be set.
(5) Select dropdownlist and "configdatesource" in the smart prompt. dsmeasureunit is configured here.
(6) Select dropdownlist, select "editdatabindings" in the smart prompt, and set the fmeasureunitid field bound to dsmerchan.

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.