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. Advertising

  2. Hi Bruno,

    You do it by adding your validation controls within the template field like
    I did for checking the dates with comparevalidators in this demo:
    http://www.webswapp.com/CodeSamples/aspnet20/GridView_2c.aspx
    --
    HTH,
    Phillip Williams
    http://www.societopia.net
    http://www.webswapp.com


    "ibiza" wrote:

    > 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
    >
    >
     
    =?Utf-8?B?UGhpbGxpcCBXaWxsaWFtcw==?=, Jan 20, 2006
    #2
    1. Advertising

  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. No, you cannot do it with a boundfield. You have to replace the boundfield
    with a TemplateField if you wish to add validation controls.
    --
    HTH,
    Phillip Williams
    http://www.societopia.net
    http://www.webswapp.com


    "ibiza" wrote:

    > thank you very much Phillip,
    >
    > I guess there are no way to do it with a boundfield.
    >
    > you are so helpful to me! :)
    >
    > Bruno
    >
    >
     
    =?Utf-8?B?UGhpbGxpcCBXaWxsaWFtcw==?=, Jan 20, 2006
    #4
  5. ibiza

    ibiza Guest

    ibiza, Jan 20, 2006
    #5
  6. =?Utf-8?B?UGhpbGxpcCBXaWxsaWFtcw==?=, Jan 20, 2006
    #6
  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
    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:
    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. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ante Perkovic

    How to validate a datagrid (in edit mode)?

    Ante Perkovic, Jun 24, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    1,926
    Ante Perkovic
    Jun 24, 2003
  2. John J Lee
    Replies:
    3
    Views:
    550
    bruno at modulix
    Dec 1, 2005
  3. Frank Kurka
    Replies:
    8
    Views:
    343
  4. Jaime Stuardo

    GridView control enters edit mode when I click Edit link twice

    Jaime Stuardo, Apr 7, 2006, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    451
    Jaime Stuardo
    Apr 7, 2006
  5. jobs at webdos
    Replies:
    0
    Views:
    524
    jobs at webdos
    Oct 9, 2006
Loading...

Share This Page