Adding a textarea to a gridview

Today I had the need add a multi-line textbox field inside of an ASP .NET Gridview, and also have the ability to edit this field. This is usually pretty simple with your standard asp:BoundField control, but making the field allow for multiple lines of input was not as easy.

I ended up using an asp:TemplateField to achieve the look I was going for. I added an ItemTemplate as well as an edit template, where I added a textbox with the textmode set to MultiLine.

        
            
                
                
                    
                        
                    
                    
                        <%# Eval("longDescriptionField")%>
                    
                
            
        
        
            
                
                
            
        

The itemTemplate just contains the string that I want to display, and the editItemTemplate contains the text box that has the textbox set to multiLine mode. This creates a textarea for the string that needs to be displayed, and I can modify the height and width of the control.

In order for me to update this field, I need to add a parameter in the code behind that represents this text area. I added the OnRowUpdating event handler so that I can modify the data source just before it performs the update. In the code behind, I have something like this:

    protected void SetUpdateParameters(object sender, GridViewUpdateEventArgs e)
    {
        myDataSource.UpdateParameters.Add("longDescription", ((TextBox)myGrid.Rows[e.RowIndex].FindControl("longDescriptionTextBox")).Text);
    }

Obviously the last step in the process is to write my update statement to update the information in the database, using my newly created parameter.

Leave a Comment

NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>