A few ASP.net skills _ practical skills

Source: Internet
Author: User
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:
<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%> ', ');
}
Refresh the list of posts
function refreshposts () {
__doPostBack (' <%= linkupdateposts.clientid%> '. Split ('_'). Join (' $ '), ');
The use of Split ('_'). Join (' $ ') is noted here because control markers that are __dopostback parameters are separated by $, and ClientID is separated by _ to convert between the two.

2. How do I get the values in the cells in the GridView edit state?

Are you still using this code?
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 use it in the RowUpdating, rowdeleting, and other events of the GridView. 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:
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.

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.