A day proficient ASP.net learning experience Summary _ practical skills

Source: Internet
Author: User
Tags connectionstrings
1, Validator
2, IsPostBack
3, AutoPostBack. Automatic post when the control leaves the focus.
4, the use of repeater control. : The Repeater control works better than the previous version of ASP.net, as long as the Eval is OK, without databinder.eval (container. DataItem, "* * *"): Yes, as long as eval ("Name") can be, note must not lose the front "#".
<asp:repeater id= "Repeater1" runat= "Server" >
<HeaderTemplate>
Quack Gaga
</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 Tooth Chant"});
List. ADD (new person () {name= "Obama", desc= "Black"});
Repeater1.datasource = list;
Repeater1.databind ();
}
5, DataList control:
(1) Highlight of line
<asp:datalist id= "DataList1" runat= "Server" >
<selecteditemstyle backcolor= "#FF6666"/>
<ItemTemplate>
<%# Eval ("Name")%>
<%# Eval ("Desc")%>
<asp:linkbutton id= "LinkButton1" runat= "Server" text= "choose" commandname= "select"/>
</ItemTemplate>
</asp:DataList>
The core is commandname this attribute, optional value and edit, delete and other optional values, when the button is clicked will perform EditCommand, DeleteCommand and other events.
(2) in-situ editing of the line:
<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= "editing"/>
</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 Tooth Chant"});
List. ADD (new person () {Name = "Obama", Desc = "Black Dark"});
Repeater1.datasource = list;
Repeater1.databind ();

Datalist1.datasource = list;
Datalist1.databind ();
}
(3) In-situ editing of rows and submission of changes
<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= "editing"/>
</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 Tooth Chant"});
List. ADD (new person () {Name = "Obama", Desc = "Black Dark"});
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 take it directly from the Datalist1.datasource because the null is taken
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= "names" sortexpression= "named"/>
<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 ("First" +e.commandargument+ "line is ordered");
}
}

protected void Gridview1_sorting (object sender, GridViewSortEventArgs e)
{

}
7, User control (UserControl)
You create a UserControl from the wizard, and you can edit the UserControl as you like, and you can add properties and events for UserControl. You can simply drag the control directly from SolutionExplorer to the page when you use it.
8, inherit the control
(1) Create a Webcustomcontrol through the wizard.
(2) Define your own application interface. You need to overload the CreateChildControls method inherited from the control class and generate the interface controls therein. If a user-defined control is used repeatedly on a page, preferably implements System.Web.UI.INamingContainer, it creates a unique namespace for the control.
(3) A message handler function that defines its own control. The control you define contains two types of messages, one is the message generated by the contained child controls, and the other is the custom control message.
9, add "Global application Class" to the project can add Global.asax, where you can monitor the application, session life cycle.
10, (1) Response.Redirect ("newpage.aspx"), client forwarding
(2) Server.Transfer ("newpage.aspx"); server-side forwarding
11, web.config Configuration
(1) <appSettings>
<add key= "FTP" value= "127.0.0.1"/>
</appSettings>
This. Title = webconfigurationmanager.appsettings["FTP"];
(2)
<connectionStrings>
<add name= "MyDB" connectionstring= "jdbc:ddd"/>
</connectionStrings>
This. Title = webconfigurationmanager.connectionstrings["MyDB"]. ConnectionString;
12, BulletedList is <ul><ol>
13. On the essence of postback
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 to 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 the
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
The PostBackUrl property of the specified button in page 1 is WebForm1.aspx so that the form is submitted to the WebForm1.aspx, and then the values in the previous page can also be taken in WebForm1.aspx:
TextBox1.Text = Previouspage.title;
You can also cast previouspage as a more detailed page subclass.
15, take the QueryString method:
request.querystring["RecordID"]
16, Server.URLEncode (LstItems.SelectedItem.Text)
17, MultiView control is used to implement dynamic interface, MultiView nested view control, each view control can be other controls. Control the display of different view by controlling the ActiveViewIndex property of the MultiView control.
18, Wizard control more than MultiView control, more like a TabControl
19, dynamic Picture:
In the event of Pageload:
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 sitemap file, modify sitemap file to add nodes.
Add a SiteMapDataSource to the page, and then just drag the TreeView, Menu, SiteMapPath and other controls up, and specify the DataSource property as SiteMapDataSource.
21 Single Value Binding
URL = "Images/picture.jpg";
This. DataBind ();
<asp:checkbox id= "chkdynamic" text= "<%# URL%>" runat= "Server"/>
22 Drop-down list Box bindings
<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 Start Page: Right-click on ASPX, select Set as StartPage
Setting of database connection strings in 24 programs
(1), web.config, add:
<connectionStrings>
<add name= "dbconnectionstring" connectionstring= "server=192.168.88.128\sqlexpress1;uid=sa;pwd=123456;database =crm "providername=" System.Data.SqlClient "/>
</connectionStrings>
(2), after dragging and dropping the DataSource component in the IDE, select Dbconnectionstring in the ConnectionString property of the property view.
(3) The method of reading this connection string in the program:
System.Configuration.Configuration Rootwebconfig =
System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration ("~");
String connstring =
rootwebconfig.connectionstrings.connectionstrings["Dbconnectionstring"]. ConnectionString;
24 steps to make a simple crud page:
(1) Drag and drop a SqlDataSource component, set a good connectionstring, naming components for dslist.
(2) Modify the DeleteQuery property of the SqlDataSource component to: Delete from T_psi_user where fid= @FId
The InsertQuery property is: INSERT into T_psi_user (FId, Fusername, Fpassword) VALUES (NEWID (), @FUserName, @FPassword)
SelectQuery is: SELECT * from T_psi_user
UpdateQuery is: Update t_psi_user set fusername= @FUserName, fpassword= @FPassword where fid= @FId
(3) Drag and drop a GridView component to set its DataSourceID property to Dslist. Modify the AllowPaging, AllowSorting, AutoGenerateDeleteButton, AutoGenerateEditButton properties to True. Set the Autogeneratedcolumns property to False. Set the DataKeyNames property to FID (so that even if the FID field is hidden, the Edit and delete functions are performed correctly)
(4) Modify the Columns property of the GridView, click the "RefreshSchema" link in the pop-up dialog box, so that the FID, FName, Fpassword Three fields are displayed under BoundField. Add the Fusername and Fpassword fields in.
This does not use a line of code, there are deleted, modify the function of the page is done. Here's the "Add" feature.
(5) Select the GridView component, select Edittemplete in the smart prompt, then select "Emptytemplete", drag and drop a FormView component into Emptytemplete, select the FormView component, Set DataSource as Dslist in the smart prompt.
(6) Create a new "new" button and edit its Click event code as:
Gridview1.datasourceid = "";
Gridview1.databind ();
(7) Set the FormView iteminserted event code is:
Refreshlist ();
The Refreshlist () function is defined as follows:
Gridview1.datasourceid = "Dslist";
Gridview1.databind ();
This "increase" function is done, but there is still a flaw, that is to show the FID of our pipe field, and the field name, buttons are in English.
(8) Select, FormView components, then click Edittemplete, select Inserttemplete, so you can delete the FID field does not need, and you can modify the layout of the control and the interface of the language text.
(9) The "Cancel" button in the Insert interface is still unavailable, edit the FormView1 ItemCommand event, and write the following code:
if (E.commandname = = "Cancel")
{
Refreshlist ();
}
25 the method for implementing CRUD above has two drawbacks:
(1) need to write a emptytemplete
(2) It is difficult to customize the edit control
Therefore, we still use the Listui and Editui method to solve. Steps:
Make Listui:
(1) The use of DataSource, GridView, but DataSource as long as the configuration SelectQuery, DeleteQuery can.
(2) The GridView does not automatically generate an Edit button.
(3) The GridView generates a ButtonField, titled "Edit", commandname= "Editinpage"
protected void Gridview1_rowcommand (object sender, Gridviewcommandeventargs e)
{
if (E.commandname = = "Editinpage")
{
int index = Convert.ToInt32 (e.commandargument);
GUID guid = (GUID) Gridview1.datakeys[index]. Value;
Server.Transfer ("/sys/sysuseredit.aspx?") Action=edit&fid= "+guid);
}
}
(4) The OnClick event for the new button:
Server.Transfer ("/sys/sysuseredit.aspx?") Action=insert ");

Make Editui:
(1) Drag a Datasouce control, configure InsertCommand and UpdateCommand as usual, configuring SelectCommand as "select * from [T_psi_user] where 1<>1", Configure UpdateCommand as ""
(2) Drag a FormView up, and modify the Edittemplete and Inserttemplte (you can direct the Edittemplete modified copy to the Inserttemplte, Be careful not to forget to modify the button's CommandName)
(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 formview1_iteminserted (object sender, Formviewinsertedeventargs e)
{
Gogolist ();
}

protected void formview1_itemupdated (object sender, Formviewupdatedeventargs e)
{
Gogolist ();
}

private void Gogolist ()
{
Server.Transfer ("/sys/sysuserlist.aspx");
}
}
}
26, DropDownList implementation of the basic data selector, such as in the selection of commodity editing units:
(1) Drag a t_measureunit table for the DataSource, such as the name Dsmeasureunit.
(2) Drag the DataSource of a commodity, such as Dsmerchan.
(3) Drag a FormView up, and set its datasource as Dsmerchan
(4) Put a DropDownList into the FormView, because it is the only way to set the binding of the DropDownList itself.
(5) Select DropDownList, select "Configdatesource" in the smart prompt, configure dsmeasureunit here.
(6) Select DropDownList, select "Editdatabindings" in the smart prompt, and then 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.