Asp.net skills (1) (2)

Source: Internet
Author: User

Server Control uses client scripts

Server controls are easy to use. We all know that what we need to do is to implement client scripts on server controls. In general, client scripts can be directly integrated with server controls on the server. You can write in the label.
For example, <asp: button id = "button1" text = "test"
Onmouseover = "This. style. backgroundcolor = 'green '"
Runat = "server"/> but the problem arises, and The onclick event is blocked, which is not achieved. The following method can be used to solve the problem:
... 1
<Asp: button id = "button1" text = "test"
Onclick = "mytest" runat = "server"/>
... 2
Private void page_load (Object sender, system, eventargs E)
{
Button1.attributes ["onclick"] = "javascript: Return confirm ('Are you sure? ')"
}
... 3
Private void button#click (Object sender, system, eventargs E)
{
Response. Write ("your click button ");
}
In this way, The onclick event and client script are added to the button during webpage loading. If there are many client script codes, you can use the function call method.
... 1
<SCRIPT merge Ge = JavaScript>
<! --
Function Test ()
{
Alter ("test ");
Return false
}
-->
</SCRIPT>
... 2
Private void page_load (Object sender, system, eventargs E)
{
Button1.attributes. Add ("onclick"), "javascript: return test ();");
}
... 3
Private void button#click (Object sender, system, eventargs E)
{
Response. Write ("your click button ");
}
Another method is better: you do not need to write the server code:
<Script for = "button1" event = "onclick ()" Language = "JavaScript">
{
<! --
Return confirm ("test ");
-->
}

DataGrid profiling
DataGrid
Advantages:
Strong flexibility and wide variety of internal direct events
Provides paging, editing, sorting, and other features
Powerful DataGrid editor and template Columns
Fast development and easy deployment
Table operations are very convenient.
Various layout styles
Disadvantages:
Low performance/PostBack is required for each operation to return to the server. The viewstate is as big as the data volume.
Personalized output data is available (only HTML tables can be output)
Datalist
Advantages:
Powerful template features and high flexibility
Data editing status is supported.
Disadvantages of better performance than DataGrid:
The development cycle is slightly higher than that of the DataGrid.
No editor like DataGrid
It is not easy to implement paging and sorting functions
Repeater is completely HTML-based and more personalized
Disadvantages: editing, paging, and sorting are not supported.
Longest development cycle
The default style is not provided and must be edited manually.
Best performance, but few features
Scope of use:
DataGrid: it is mainly used for tables containing data. Generally, the region of the data is dataset and multiple columns are displayed.
Datalist; mainly used for the data list of A Single Column
Repeater: used for displaying flexible and high-performance data
In general, the DataGrid includes
Each row represents a datagriditem: listitemtype:
Header, footer, item, alternatingitem, selecteditem, edititem, separastor, pager
Each column represents a datagridcolumn: boundcolumn, bttoncolumn, hyperlinkcolumn, templatecolumn, editcommandcolumn
Notes for using DataGrid:
We recommend that you do not use auto-generated columns.
Do not reference the control in the DataGrid only by using the control ID. Use findcontrol to specify the Control ID.
The sequence of events that the DataGrid needs to complete from loading data to rendering data:
Execute databinding when using the databind method (objective: to load columns in dataset and create itemcreated events)-> itemcreated event-> itemdatabind event-> prerender event
Itemcreated and itemdatabind can all be converted into a datarowview, so that we can easily obtain the field of the row in the DataGrid, but the conversion in itemcreated does not make much sense, at this time, the row has not actually obtained a value of null. Generally, it is converted under the itemdatabind event.
FAQs about DataGrid binding events:
1. Error reported When referencing E. Item. dataitem
Cause: the type is not determined in itemcreated or itemdatabind:
(Datarowview) (E. Item, dataitem) A value is available only when items, alternatingitem, selecteditem, and edititem are run.
2. After data is bound, how does one access the data of a row?
You cannot use datagriditem for direct access. It only has a value during the binding process. After binding, the value is blank and needs to be accessed through loops:
For each (datagriditem DGI in datagrid1.items)
{.....}
3. Pay attention to the itemcreated event
The row type should be determined when row attributes or in-row control attributes are loaded or modified.
Private void maid (....)
{
If (E. Item. itemtype = listitemtype. item)
{
............
}
}
For example, add a confirmation basket:
Private void datagrid1_itemdatacreated (....)
{
If (E. Item. itemtype = listitemtype. Item | E. Item. itemtype = listitemtype. alternatingitem)
{
Linkbutton L = (linkbutton) E. Item. cells [11]. controls [0];
Datarowview DRV = (datarowview) E. Item. dataitem;
L. atrributes. Add ("onclick", "javascript: Return confirm (" are you sure you want to delete "+ DRV. Row. itemarray [0] + "? ");");
Label lB = (Label) E. Item. findcontrol ("LBID ");
LB. Text = convert. tostring (E. Item. itemindex + 1 );
}
}
Data Binding:
Simple attributes: <% # custid %>
Set: <asp: ListBox id = "list1" datasource = '<% # myarray %> 'runat = "server">
Expression: <% # (customer. First name + "" + customer. lastname) %>
Result: <% # getbalance (custid) %>
M template Column Binding:
Databidner, Eval

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.