views:

460

answers:

2

Hi,take a look at this sample code: (question bellow)

<asp:GridView ID="GridView1" runat="server" AllowPaging="True" 
    AllowSorting="True" DataSourceID="SqlDataSource2" 
    AutoGenerateColumns="False" onrowupdated="GridView1_RowUpdated" 
         DataKeyNames="Product_Id">
    <Columns>
    <asp:ImageField DataImageUrlField="Image_Name" HeaderText="Image_Name" 
           ReadOnly="True" >
    <ItemStyle Width="50px" Height="50px"  Wrap="true"/>
    </asp:ImageField>       
    <asp:BoundField DataField="Product_Id" HeaderText="Product_Id" 
           InsertVisible="False" ReadOnly="True" SortExpression="Product_Id">
    </asp:BoundField>
        <asp:BoundField DataField="Product_Name" HeaderText="Product_Name" 
            SortExpression="Product_Name" />
        <asp:BoundField DataField="Category_Name" HeaderText="Category_Name" 
            SortExpression="Category_Name" ReadOnly="true" />
        <asp:BoundField DataField="Description" HeaderText="Description" 
            SortExpression="Description" />
        <asp:BoundField DataField="Size" HeaderText="Size" 
                 SortExpression="Size" />
        <asp:BoundField DataField="Price" HeaderText="Price" 
                 SortExpression="Price" />
        <asp:CommandField ShowEditButton="True" />
        <asp:CommandField ShowDeleteButton="True" />
    </Columns>
</asp:GridView>

Assume I initialize an SqlDataSource, add a parameter and so on.

The thing is, that when a user clicks edit we get a textbox to edit the colnumn value. I want to validate the data enter by the user before the update is performed and the new data is propagated back to the server.How?

10x a lot!

+1  A: 

You need to convert the BoundField into a TemplateField. Then you can add a validator to the actual TextBox control.

Dan Diplo
Been there.Working with templatefields opens a whole new world of trouble for me. There must be a way...Anyone?
Template fields is the way you are supposed to do this. I don't really see what is difficult or troublesome in using them? I do all the time without issue. What problems do you have?
Dan Diplo
+1  A: 

Option 1:

But from the UNKNOWN answer, the Microsoft recommends the same.. as he told.

ref: http://msdn.microsoft.com/en-us/library/bb426882.aspx#aspnett19_vldcntredtuics_topic2

Option 2:

But, we can do.

You need to add the validation either the javascript validation or server side validation

control, when the GridView's DataBound event is happening at runtime on the particular

TableCell of the Gridview rows.

Hence, when you click the update button that custom generated javascript or the validation

control will check for the validation on editing the values.

This process is more harder than the converting boundfield to templatefield

refer: http://www.aspdotnetcodes.com/GridView_Dynamic_Validation.aspx

Option 3:

And you can go for server side validation on the values instead of client side validation:

refer: http://msdn.microsoft.com/en-us/library/bb332383.aspx

solairaja