GridView RequiredFieldValidator Display

Discussion in 'ASP .Net' started by AG, Oct 15, 2006.

  1. AG

    AG Guest

    Below is code (slightly modified and converted to VB) that was provided to
    me in response to another post. I am using it to demonstrate another
    problem.

    In order for paging and other features to work properly in a gridview,
    viewstate must be enabled. So, in order to minimize the size of viewstate
    for a page, I will sometimes turn off viewstate for each control in template
    columns.

    That means that the gridview must be databound on each postback, which is
    ok.

    I have two questions.

    1. Why does the RowCommand event fire twice?

    2. In the below sample, I have a button and textbox in the gridview's footer
    for adding a new record. Also a requiredfieldvalidator.

    Clientscript has been disabled for the validator to simulate a browser with
    javascript disabled.

    In the RowCommand event, I validate the page for the appropriate group.

    My problem is that the validator does not display it's error message if I
    re-bind the grid on postback.

    If I don't re-bind the grid, the error message will display, but as I
    mentioned, in some cases I do need to rebind on every postback.

    Is there a way to force the validator to display the error message?



    TIA

    <%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default2.aspx.vb"
    Inherits="Default2" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml" >

    <head runat="server">

    <title>Untitled Page</title>

    </head>

    <body>

    <form id="form1" runat="server">

    <div>

    <asp:GridView ID="GridView1" runat="server"

    AutoGenerateColumns="false" OnRowCommand="GridView1_RowCommand"

    OnRowDeleting="GridView1_RowDeleting" OnRowEditing="GridView1_RowEditing"

    ShowFooter="True">

    <Columns>

    <asp:BoundField DataField="CategoryID"

    HeaderText="CategoryID" />

    <asp:BoundField DataField="CategoryName"

    HeaderText="CategoryName" />

    <asp:BoundField DataField="Description"

    HeaderText="Description" />

    <asp:TemplateField HeaderText="TemplateField">

    <ItemTemplate>

    <asp:Button ID="btnEdit" runat="server" Text="Edit"

    CommandName="Edit" EnableViewState="False" />

    <asp:ImageButton ID="imgBtn" runat="server"

    ImageUrl="http://wcf.netfx3.com/Themes/default/images/logo.gif"

    CommandName="Edit" EnableViewState="False" />

    </ItemTemplate>

    <FooterTemplate>

    <asp:Button ID="btnInsert" runat="server" Text="Insert"

    CommandName="Insert" ValidationGroup="Group1" EnableViewState="False" />

    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>

    <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"
    ControlToValidate="TextBox1"

    Display="Dynamic" EnableClientScript="False" EnableViewState="False"
    ErrorMessage="Required" ValidationGroup="Group1"
    SetFocusOnError="True"></asp:RequiredFieldValidator>

    </FooterTemplate>

    </asp:TemplateField>

    <asp:CommandField ShowDeleteButton="True" />

    </Columns>

    </asp:GridView>

    &nbsp;<br />

    </div>

    </form>

    </body>

    </html>

    Partial Class Default2

    Inherits System.Web.UI.Page

    Private Sub BindGrid()

    Dim categories(10) As CategoryVO

    Dim i As Integer

    For i = 0 To categories.Length - 1

    Dim vo As New CategoryVO()

    vo.CategoryID = i + 1

    vo.CategoryName = "Category_" + vo.CategoryID.ToString

    vo.Description = "Description of " + vo.CategoryName

    categories(i) = vo

    Next i

    GridView1.DataSource = categories

    GridView1.DataBind()

    End Sub

    Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As
    System.Web.UI.WebControls.GridViewCommandEventArgs) Handles
    GridView1.RowCommand

    Response.Write("<br/>GridView1_RowCommand: " + e.CommandName)

    Me.Page.Validate("Group1")

    Response.Write("<br/>GridView1_RowCommand: IsValid = " +
    Page.IsValid.ToString)

    End Sub

    Protected Sub GridView1_RowDeleting(ByVal sender As Object, ByVal e As
    GridViewDeleteEventArgs)

    Response.Write(("<br/>GridView1_RowDeleting " + e.RowIndex.ToString))

    End Sub

    Protected Sub GridView1_RowEditing(ByVal sender As Object, ByVal e As
    GridViewEditEventArgs)

    Response.Write(("<br/>GridView1_RowEditing " + e.NewEditIndex.ToString))

    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
    Handles Me.Load

    If Not IsPostBack Then

    'BindGrid()

    End If

    End Sub

    Protected Sub Page_PreRender(ByVal sender As Object, ByVal e As
    System.EventArgs) Handles Me.PreRender

    BindGrid()

    End Sub

    End Class




    --

    AG
    Email: discuss at adhdata dot com
     
    AG, Oct 15, 2006
    #1
    1. Advertising

  2. Hi AG,

    > 1. Why does the RowCommand event fire twice?


    It's because you handled the RowCommand event twice: one is defined in your
    ASPX: OnRowCommand="GridView1_RowCommand"; another is defined by your
    code-behind: Protected Sub GridView1_RowCommand(ByVal sender As Object,
    ByVal e As
    System.Web.UI.WebControls.GridViewCommandEventArgs) Handles
    GridView1.RowCommand

    You need to remove one.

    > 2. ... Is there a way to force the validator to display the error message?


    In the RowCommand event, you can see the validator is actually working
    correctly (Page.IsValid will be false if the TextBox is not filled in);
    however, when you rebind the Grid in PreRender, the validator loses the
    information about the state that an error message should be displayed if
    the control failed to validate.

    I think when the page is invalid, the GridView doesn't need rebinding
    again.

    I recommend use following workaround:

    Dim flag1 As Boolean = False

    Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As
    System.Web.UI.WebControls.GridViewCommandEventArgs)
    Response.Write("<br/>GridView1_RowCommand: " + e.CommandName)
    Me.Page.Validate("Group1")
    flag1 = Not Page.IsValid
    Response.Write("<br/>GridView1_RowCommand: IsValid = " +
    Page.IsValid.ToString)
    End Sub

    Protected Sub Page_PreRender(ByVal sender As Object, ByVal e As
    System.EventArgs) Handles Me.PreRender
    If Not flag1 Then
    BindGrid()
    Response.Write("<br/>PreRender and rebind")
    End If
    End Sub


    Please test this and let me know whether or not this works for you. Thanks.

    Sincerely,
    Walter Wang (, remove 'online.')
    Microsoft Online Community Support

    ==================================================
    Get notification to my posts through email? Please refer to
    http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
    ications. If you are using Outlook Express, please make sure you clear the
    check box "Tools/Options/Read: Get 300 headers at a time" to see your reply
    promptly.

    Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
    where an initial response from the community or a Microsoft Support
    Engineer within 1 business day is acceptable. Please note that each follow
    up response may take approximately 2 business days as the support
    professional working with you may need further investigation to reach the
    most efficient resolution. The offering is not appropriate for situations
    that require urgent, real-time or phone-based interactions or complex
    project analysis and dump analysis issues. Issues of this nature are best
    handled working with a dedicated Microsoft Support Engineer by contacting
    Microsoft Customer Support Services (CSS) at
    http://msdn.microsoft.com/subscriptions/support/default.aspx.
    ==================================================

    This posting is provided "AS IS" with no warranties, and confers no rights.
     
    Walter Wang [MSFT], Oct 16, 2006
    #2
    1. Advertising

  3. Hi AG,

    After a second thought, I think the better workaround would be
    re-validating in PreRender when it's invalid:

    Protected Sub Page_PreRender(ByVal sender As Object, ByVal e As
    System.EventArgs) Handles Me.PreRender
    BindGrid()
    If flag1 Then Page.Validate("Group1")
    Response.Write("<br/>PreRender and rebind")
    End Sub

    This way, the controls that have ViewState disabled will not lose the data
    when the postback is caused by the Insert command.

    Regards,
    Walter Wang (, remove 'online.')
    Microsoft Online Community Support

    ==================================================
    When responding to posts, please "Reply to Group" via your newsreader so
    that others may learn and benefit from your issue.
    ==================================================

    This posting is provided "AS IS" with no warranties, and confers no rights.
     
    Walter Wang [MSFT], Oct 16, 2006
    #3
  4. AG

    AG Guest

    Thanks Walter,

    Both items resolved!

    --

    AG
    Email: discuss at adhdata dot com



    "Walter Wang [MSFT]" <> wrote in message
    news:...
    > Hi AG,
    >
    > After a second thought, I think the better workaround would be
    > re-validating in PreRender when it's invalid:
    >
    > Protected Sub Page_PreRender(ByVal sender As Object, ByVal e As
    > System.EventArgs) Handles Me.PreRender
    > BindGrid()
    > If flag1 Then Page.Validate("Group1")
    > Response.Write("<br/>PreRender and rebind")
    > End Sub
    >
    > This way, the controls that have ViewState disabled will not lose the data
    > when the postback is caused by the Insert command.
    >
    > Regards,
    > Walter Wang (, remove 'online.')
    > Microsoft Online Community Support
    >
    > ==================================================
    > When responding to posts, please "Reply to Group" via your newsreader so
    > that others may learn and benefit from your issue.
    > ==================================================
    >
    > This posting is provided "AS IS" with no warranties, and confers no
    > rights.
    >
     
    AG, Oct 16, 2006
    #4
  5. AG

    turkush

    Joined:
    Sep 1, 2007
    Messages:
    2
    Hi,

    I hav the same prob my onRowCommand event fires twice...wot to do...
     
    turkush, Sep 1, 2007
    #5
  6. AG

    turkush

    Joined:
    Sep 1, 2007
    Messages:
    2
    I hav the same prob that my onRowCommand fires twice...wot to do???
     
    turkush, Sep 1, 2007
    #6
    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. James
    Replies:
    4
    Views:
    7,112
    James
    Aug 17, 2004
  2. Christian Ista
    Replies:
    5
    Views:
    3,151
    Johann MacDonagh
    Dec 7, 2004
  3. Nathan Sokalski
    Replies:
    1
    Views:
    9,077
    Peter Blum
    Jun 29, 2005
  4. =?iso-8859-1?Q?Andr=E9_Nobre?=

    Custom Control and RequiredFieldValidator.Display

    =?iso-8859-1?Q?Andr=E9_Nobre?=, Jul 27, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    2,027
    =?iso-8859-1?Q?Andr=E9_Nobre?=
    Jul 27, 2006
  5. Norm

    RequiredFieldValidator: No message display.

    Norm, May 21, 2004, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    174
Loading...

Share This Page