1. In asp.net AJAX, how do I invoke a server-side method with JavaScript?
This does not mean calling simple Pagemethod, because the static method is not able to manipulate the current page of the control, so the static Pagemethod function is the same as ordinary WebService, more limited.
Then, invoking the generic server-side method is actually initiating an asynchronous callback. The simplest way to achieve this is through UpdatePanel.
First define a hidden LinkButton, in its handling of events, to invoke the action we want to perform:
Copy Code code as follows:
<asp:linkbutton id= "linkupdateposts" runat= "Server" style= "display: ' None ';" onclick= "linkupdateposts_clicked"/>
Refresh the list of posts (for client JS calls)
Protected
void Linkupdateposts_clicked (object sender, EventArgs e)
{
Loadposts ();
}
You can then define a function in JavaScript to trigger this LinkButton postback:
Refresh the list of posts
function refreshposts () {
__doPostBack (' <%= linkupdateposts.uniqueid%> ', ');
}
2. How do I get the values in the cells in the GridView edit state?
Are you still using this code?
Copy Code code as follows:
var txtname = grid1. Rows[e.rowindex]. Cells[0]. FindControl ("Txtname") as TextBox;
if (txtname!=
Null
{
Read value
//
}
Actually the work (finding the control in a cell and trying to get the value in it) has been encapsulated. Now, just call the ExtractValuesFromCell method.
This method is also supported by a variety of column types:
DataControlField, BoundField, Autogeneratedfield, CheckBoxField, ImageField, TemplateField, DynamicField you can be in the GridView RowUpdating, rowdeleting and other events to use it. Using this method, you can extract the value into the dictionary you want, and then read it from the dictionary. These dictionaries include: E. Keys, E.newvalues, e.oldvalues and so on.
A short sample code:
Copy Code code as follows:
Update
Protected
void Grid1_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 on most occasions, and we can also read the value of its edits correctly, and save a bunch of code for the custom TemplateField.