Edit template field in gridview in asp.net
Top Rated Most Recent. Accept Solution Reject Solution. Posted Sep am NMehta Reason for my vote of 5 Automatic vote of 5 for accepting answer. You can add a suitable event handler that is fired when user updates the data in the edit mode Say, RowUpdating and find the control using the RowIndex of the corresponding row that is currently being updated.
That is, something llike the following I used C code to demonstrate the idea : C. Posted Sep pm Al-Farooque Shubho. Add your solution here. OK Paste as. Treat my content as plain text, not as HTML. Existing Members Sign in to your account. This email is in use. Do you need your password?
Submit your solution! When answering a question please: Read the question carefully. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome. Finally this article explains how to handle click events of dynamic Button, LinkButton or ImageButton that will be added to the dynamic TemplateField columns and also explains how to fetch values from the dynamic BoundField and TemplateField columns when thee Buttons are clicked.
You might also like to read:. Rotate ASP. HTML Markup. You will need to import the following namespaces. Imports System. For the BoundField column, DataField property has been defined, which is the name of the column that will be populated within it.
For the TemplateField, only HeaderText property has been set as we will have to first add controls to its ItemTemplate before we can bind data.
Add bfield ;. Add tfield ;. BindGrid ;. Add 1, "John Hammond" , "United States" ;. Add 2, "Mudassar Khan" , "India" ;. Add 3, "Suzanne Mathews" , "France" ;. Add 4, "Robert Schidner" , "Russia" ;. DataBind ;. If Not Me. IsPostBack Then. Dim bfield As New BoundField. Add bfield. Dim tfield As New TemplateField. Add tfield. End If. End Sub. Private Sub BindGrid. Dim dt As New DataTable.
Add 1, "John Hammond" , "United States". Add 2, "Mudassar Khan" , "India". Add 3, "Suzanne Mathews" , "France". Regardless, this tutorial shows that in some circumstances view state must be enabled in order to provide certain functionality. A third use of TemplateFields is in displaying metadata about the GridView's underlying data. In addition to showing the employees' hire dates, for example, we might also want to have a column that displays how many total days they've been on the job.
Yet another use of TemplateFields arises in scenarios when the underlying data needs to be displayed differently in the web page report than in the format it's stored in the database. Imagine that the Employees table had a Gender field that stored the character M or F to indicate the sex of the employee.
When displaying this information in a web page we might want to show the gender as either "Male" or "Female", as opposed to just "M" or "F". Both of these scenarios can be handled by creating a formatting method in the ASP. NET page's code-behind class or in a separate class library, implemented as a static method that is invoked from the template.
Such a formatting method is invoked from the template using the same databinding syntax seen earlier. The formatting method can take in any number of parameters, but must return a string. This returned string is the HTML that is injected into the template. To illustrate this concept, let's augment our tutorial to show a column that lists the total number of days an employee has been on the job. This formatting method will take in a Northwind. EmployeesRow object and return the number of days the employee has been employed as a string.
This method can be added to the ASP. NET page's code-behind class, but must be marked as protected or public in order to be accessible from the template. To utilize this method we need to invoke it from a TemplateField in the GridView using the databinding syntax. Its Row property returns the strongly-typed Northwind. This databinding syntax can appear directly in the ItemTemplate as shown in the declarative syntax below or can be assigned to the Text property of a Label Web control.
However, the Eval method returns an object , so we'd have to change our DisplayDaysOnJob method signature to accept an input parameter of type object , instead. Therefore, we'd need to accept an object as the input parameter for the DisplayDaysOnJob method, check to see if it had a database NULL value which can be accomplished using Convert. Due to these subtleties, I've opted to pass in the entire EmployeesRow instance.
In the next tutorial we'll see a more fitting example for using the Eval "columnName" syntax for passing an input parameter into a formatting method. The TemplateField in the GridView control allows for a higher degree of flexibility in displaying data than is available with the other field controls. TemplateFields are ideal for situations where:. In addition to customizing the display of data, TemplateFields are also used for customizing the user interfaces used for editing and inserting data, as we'll see in future tutorials.
The next two tutorials continue exploring templates, starting with a look at using TemplateFields in a DetailsView. Following that, we'll turn to the FormView, which uses templates in lieu of fields to provide greater flexibility in the layout and structure of the data. Scott works as an independent consultant, trainer, and writer. NET 2. He can be reached at mitchell 4GuysFromRolla.
This tutorial series was reviewed by many helpful reviewers. Lead reviewer for this tutorial was Dan Jagers. Interested in reviewing my upcoming MSDN articles? If so, drop me a line at mitchell 4GuysFromRolla. Previous Next. Skip to main content. This browser is no longer supported. Download Microsoft Edge More info.
0コメント