Link to home
Create AccountLog in
Avatar of -Dman100-
-Dman100-Flag for United States of America

asked on

validating a gridview

I've read several articles on how to validate gridview input, but I'm still having problems figuring out how to validate my gridview.

Here is what I'm trying to do.

I want to add a required field validator and a custom field validator for the description field that checks if the field is empty and does not exceed 100 characters.

I want to add a required field validator and custom field validator for the metrics field that checks if the field is empty and is numeric only...5 or 10.5, etc.  (just no special characters)

Once the user clicks "Update" I want the validation to trigger.  I want to display any validation errors in a popup alert box with the field in question having focus.

If anyone can help with a code example, it would be greatly appreciated.

Here is my code:


<asp:GridView 
            ID="GridView1"
            AutoGenerateColumns="False" 
            runat="server" 
            OnRowEditing="GridView1_RowEditing" 
            OnRowCancelingEdit="GridView1_RowCancelingEdit" 
            Width="750px" 
            OnRowUpdating="GridView1_RowUpdating">
            <Columns>
                <asp:TemplateField HeaderText="Description">
                    <ItemTemplate>
                        <asp:Label ID="lblDescription" Text='<%# Eval("description") %>' runat="server"></asp:Label>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:TextBox ID="txtDescription" Text='<%# Eval("description") %>' runat="server"></asp:TextBox>
                    </EditItemTemplate>
                    <ControlStyle Width="300px" />
                    <HeaderStyle HorizontalAlign="Left" />
                    <ItemStyle Width="300px" />
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Metric">
                    <ItemTemplate>
                        <asp:Label ID="lblMetric" Text='<%# Eval("metric") %>' runat="server"></asp:Label>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:TextBox ID="txtMetric" Text='<%# Eval("metric") %>' runat="server"></asp:TextBox>
                    </EditItemTemplate>
                    <ControlStyle Width="300px" />
                    <HeaderStyle HorizontalAlign="Left" />
                    <ItemStyle Width="300px" />
                </asp:TemplateField>
                <asp:CommandField 
                    ButtonType="Button" 
                    ShowEditButton="True" >
                </asp:CommandField>
            </Columns>
        </asp:GridView>
 
 
protected void Page_Load(object sender, EventArgs e)
    {
        GridView1.DataSource = CustomerServiceMetricsDAO.GetCustomerServiceMetrics();
        GridView1.DataBind();
    }
 
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        DataBind();
    }
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        DataBind();
    }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        
    }

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of naspinski
naspinski
Flag of United States of America image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of -Dman100-

ASKER

Hi  naspinski,
Thanks for replying to my post.  How does the validation get triggered?  I've added the validator to the <EditItemTemplate>, but I get the following error:

Invalid postback or callback argument.  Event validation is enabled using <pages enableEventValidation="true"/> in configuration or <%@ Page EnableEventValidation="true" %> in a page.  For security purposes, this feature verifies that arguments to postback or callback events originate from the server control that originally rendered them.  If the data is valid and expected, use the ClientScriptManager.RegisterForEventValidation method in order to register the postback or callback data for validation.
I found that adding the validators at runtime using rowdatabound fixed the problem:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if (GridView1.EditIndex == e.Row.RowIndex)
            {
                RequiredFieldValidator rfv1 = new RequiredFieldValidator();
                rfv1.ID = "valrDescription";
                rfv1.ControlToValidate = "txtDescription";
                rfv1.ErrorMessage = "Field cannot be empty.";
                rfv1.SetFocusOnError = true;
                rfv1.Display = ValidatorDisplay.Dynamic;
                e.Row.Cells[0].Controls.Add(rfv1);

                RequiredFieldValidator rfv2 = new RequiredFieldValidator();
                rfv2.ID = "valrMetric";
                rfv2.ControlToValidate = "txtMetric";
                rfv2.ErrorMessage = "Field cannot be empty.";
                rfv2.SetFocusOnError = true;
                rfv2.Display = ValidatorDisplay.Dynamic;
                e.Row.Cells[1].Controls.Add(rfv2);

                CustomValidator cv1 = new CustomValidator();
                cv1.ID = "valxDescription";
                cv1.ServerValidate = ValidateInputLength();
                cv1.ErrorMessage = "Field length cannot exceed 255 characters.";
                cv1.SetFocusOnError = true;
                cvl.Display = ValidatorDisplay.Dynamic;
                e.Row.Cells[0].Controls.Add(cv1);

            }
        }
    }