How to validate input in gridview edit mode

Discussion in 'ASP .Net' started by ibiza, Jan 19, 2006.

  1. ibiza

    ibiza Guest

    Hi all,

    I have a gridview that can edit data, which is bound to an
    sqldatasource. Everything works fine, I can update the data via an Edit
    Command field.

    However, there are some textboxes in edit mode which I would like to
    validate. For example, one of the is required and cannot be left empty.
    How can I make such validation in edit mode in a gridview?

    Thanks in advance,

    Bruno
     
    ibiza, Jan 19, 2006
    #1
    1. Advertisements

  2. ibiza

    Guest Guest

    Guest, Jan 20, 2006
    #2
    1. Advertisements

  3. ibiza

    ibiza Guest

    thank you very much Phillip,

    I guess there are no way to do it with a boundfield.

    you are so helpful to me! :)

    Bruno
     
    ibiza, Jan 20, 2006
    #3
  4. ibiza

    Guest Guest

    No, you cannot do it with a boundfield. You have to replace the boundfield
    with a TemplateField if you wish to add validation controls.
     
    Guest, Jan 20, 2006
    #4
  5. ibiza

    ibiza Guest

    ibiza, Jan 20, 2006
    #5
  6. ibiza

    Guest Guest

  7. ibiza

    ibiza Guest

    wow thank you!

    I wonder why I did'nt think of this simple solution by myself :|
     
    ibiza, Jan 20, 2006
    #7
  8. ibiza

    jasonHall

    Joined:
    Jan 11, 2011
    Messages:
    1
    Likes Received:
    0
    A technique that worked well for me for validating BoundFields is to perform the validation in the gridView_RowUpdating event handler. "e" is the GridViewUpdateEventArgs. I just show the error message in red and display it to the user right in the textbox, and keep the Row in edit mode.

    Code (Text):

    GridViewRow aRow = gvBatchDetails.Rows[e.RowIndex];

    if (((TextBox)(aRow.Cells[1].Controls[0])).Text == "hello")
    {
        ((TextBox)(aRow.Cells[1].Controls[0])).Text = "Error";
        ((TextBox)(aRow.Cells[1].Controls[0])).ForeColor = System.Drawing.Color.Red;
        e.Cancel = true;
    }
     
     
    jasonHall, Jan 11, 2011
    #8
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.