asp.net TemplateField template bind method and Eval method _ Practical Tips

Source: Internet
Author: User
Tags eval
For example, we want to take a date type of data, in the database column name is updated, the value is 2008/06/01. But like the June 01, 2008 show, we can write bind ("updated", "{0:yyyy mm month DD Day}"), and so does eval.
All 2 can read the values in the data and display them. When we use the edit update operation, bind can automatically update the modified values to the database and display the modified values. But the eval only gets the wrong picture, and the new data is not updated into the database.
From this point of view, the difference between the bind method and the Eval method is that the bind method is OK in the 2 aspects of reading and updating data, but the Eval method can only read the display data. So, when we choose the Bind method and the Eval method, there must be contention, and when the data definitely needs to be updated, we should use BIND, just display the data, and we can use the Eval method without any action.
In the update operation we can operate in the Gridview1_rowupdating event, as shown in the following example:
Copy Code code as follows:

protected void Gridview1_rowupdating (object sender, Gridviewupdateeventargs e)
{
Update Row GridViewRow
GridViewRow row = this. Gridview1.rows[e.rowindex];
Find an updated control
DropDownList present = (DropDownList) row. FindControl ("Ddlpresent");
TextBox price = (textbox) row. FindControl ("Txtprice");
TextBox updated = (textbox) row. FindControl ("txtupdated");
Update
E.newvalues["Present"] = present. SelectedValue;
e.newvalues[' price ' = Price. Text;
e.newvalues["updated"] = updated. Text;
}
protected void Gridview1_rowupdating (object sender, Gridviewupdateeventargs e)
{
Update Row GridViewRow
GridViewRow row = this. Gridview1.rows[e.rowindex];
Find an updated control
DropDownList present = (DropDownList) row. FindControl ("Ddlpresent");
TextBox price = (textbox) row. FindControl ("Txtprice");
TextBox updated = (textbox) row. FindControl ("txtupdated");
Update
E.newvalues["Present"] = present. SelectedValue;
e.newvalues[' price ' = Price. Text;
e.newvalues["updated"] = updated. Text;
}

If we can fully understand the bind method and the Eval method, there is no need to write to the above, it can be done automatically. The above method is used in addition to the more complex operations, which is also a skill to use.
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.