ASP. NET tips

Source: Internet
Author: User

ASP. NET tips: In asp.net AJAX, how does one use JavaScript to call the server method?

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:

 
 
  1. < asp:LinkButton ID="linkUpdatePosts" runat="server" style="display:'none';" OnClick  
  2. ="linkUpdatePosts_Clicked" />  
 
 
  1. // Refresh the post list for the client js call) 
  2. Protected VoidLinkUpdatePosts_Clicked (ObjectSender, EventArgs e)
  3. {
  4. LoadPosts ();
  5. }

Then, you can define a function in JavaScript to trigger the callback of this LinkButton:

 
 
  1. // Refresh the post list 
  2. Function refreshPosts (){
  3. _ DoPostBack ('<% = LinkUpdatePosts. UniqueID %>','');
  4. }

ASP. NET tips: how to get the values in cells in the editing state of the GridView?

Are you still using this code?

 
 
  1. Var txtName = grid1.Rows [e. RowIndex]. Cells [0]. FindControl ("TxtName")AsTextBox;
  2. If(TxtName! =Null)
  3. {
  4. // Read value 
  5. // 
  6. }
  7.  

In fact, these jobs search for controls in cells and try to get their 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, DynamicField

You can use it in the 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:

 
 
  1. // Update 
  2. Protected VoidGrid1_RowUpdating (ObjectSender, GridViewUpdateEventArgs e)
  3. {
  4. Var row = grid1.Rows [e. RowIndex];
  5. // Extract the value of the Id field 
  6. Grid1.Columns [0]. ExtractValuesFromCell (
  7. E. Keys,
  8. Row. Cells [0]AsDataControlFieldCell,
  9. DataControlRowState. Edit,
  10. True /* Include readonly */);
  11.  
  12. // Extract the value of the Name field 
  13. Grid1.Columns [1]. ExtractValuesFromCell (
  14. E. NewValues,
  15. Row. Cells [1]AsDataControlFieldCell,
  16. DataControlRowState. Edit,
  17. True /* Include readonly */);
  18.  
  19. Var id =Int. Parse (e. Keys ["Id"]. ToString ());
  20. Var name = (String) E. NewValues ["Name"];
  21.  
  22. // Perform related database update operations 
  23. // 
  24. }

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. The above two common ASP. NET skills are introduced.

  1. How to deploy the asp.net mvc program in IIS6.0
  2. Use Winform to build the asp.net mvc Framework
  3. Programming idea of ASP. NET Session failure
  4. ASP. NET Session state storage
  5. Understand ASP. NET Web Application Models

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.