ASP. NET tips (2)

Source: Internet
Author: User

1. in ASP. NET Ajax, how does one use JavaScript to call server methods?
This does not mean calling a simple pagemethod. Because the static method cannot operate the control of the current page, the static pagemethod function is the same as that of a common WebService and is quite limited.
Therefore, calling a common server method is actually initiating an asynchronous callback. The simplest implementation method is through updatepanel.
First, define a hidden linkbutton to call the operation we want to perform in its event processing: CopyCode The Code is as follows: <asp: linkbutton id = "linkupdateposts" runat = "server" style = "display: 'none';" onclick = "linkupdateposts_clicked"/>
// Refresh the post list (called by the client JS)
Protected
Void linkupdateposts_clicked (Object sender, eventargs E)
{
Loadposts ();
}
Then, you can define a function in JavaScript to trigger the callback of this linkbutton:
// Refresh the post list
Function refreshposts (){
_ Dopostback ('<% = linkupdateposts. uniqueid %> ','');
}

2. How to get the value in the cell in the editing state of the gridview?
Are you still using this code?Copy codeThe Code is as follows: var txtname = grid1.rows [E. rowindex]. cells [0]. findcontrol ("txtname") as textbox;
If (txtname! =
Null)
{
// Read value
//
}

In fact, these operations (search for controls in cells and try to get the values) have been encapsulated. Now, you only need to call the extractvaluesfromcell method.
This method is also supported by many column types:
Datacontrolfield, boundfield, autogeneratedfield, checkboxfield, imagefield, templatefield, and dynamicfield can be used in rowupdating, rowdeleting, and other events of the gridview. This method can be used to extract values to the required dictionary and then read from the dictionary. These dictionaries include E. Keys, E. newvalues, and E. oldvalues.
Example code: Copy code The Code is as follows: // update
Protected
Void grid#rowupdating (Object sender, gridviewupdateeventargs E)
{
VaR ROW = grid1.rows [E. rowindex];
// Extract the value of the ID field
Grid1.columns [0]. extractvaluesfromcell (
E. Keys,
Row. cells [0] As datacontrolfieldcell,
Datacontrolrowstate. Edit,
True
/* Include readonly */);
// Extract the value of the Name field
Grid1.columns [1]. extractvaluesfromcell (
E. newvalues,
Row. cells [1] As datacontrolfieldcell,
Datacontrolrowstate. Edit,
True
/* Include readonly */);
VaR id =
Int. parse (E. Keys ["ID"]. tostring ());
VaR name = (string) E. newvalues ["name"];
// Perform related database update operations
//
}

In this way, we can use boundfield as much as possible in most cases, and can correctly read the value during editing, saving a bunch of custom templatefield code.

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.