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:
- < asp:LinkButton ID="linkUpdatePosts" runat="server" style="display:'none';" OnClick
- ="linkUpdatePosts_Clicked" />
- // Refresh the post list for the client js call)
- Protected VoidLinkUpdatePosts_Clicked (ObjectSender, 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 %>','');
- }
ASP. NET tips: how to get the values in cells in the editing state of the GridView?
Are you still using this code?
- Var txtName = grid1.Rows [e. RowIndex]. Cells [0]. FindControl ("TxtName")AsTextBox;
- If(TxtName! =Null)
- {
- // Read value
- //
- }
-
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:
- // Update
- Protected VoidGrid1_RowUpdating (ObjectSender, GridViewUpdateEventArgs e)
- {
- Var row = grid1.Rows [e. RowIndex];
- // Extract the value of the Id field
- Grid1.Columns [0]. ExtractValuesFromCell (
- E. Keys,
- Row. Cells [0]AsDataControlFieldCell,
- DataControlRowState. Edit,
- True /* Include readonly */);
-
- // Extract the value of the Name field
- Grid1.Columns [1]. ExtractValuesFromCell (
- E. NewValues,
- Row. Cells [1]AsDataControlFieldCell,
- 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. The above two common ASP. NET skills are introduced.
- How to deploy the asp.net mvc program in IIS6.0
- Use Winform to build the asp.net mvc Framework
- Programming idea of ASP. NET Session failure
- ASP. NET Session state storage
- Understand ASP. NET Web Application Models