GridView RequiredFieldValidator Display

A

AG

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
 
W

Walter Wang [MSFT]

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 ([email protected], 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.
 
W

Walter Wang [MSFT]

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 ([email protected], 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.
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,708
Latest member
SherleneF1

Latest Threads

Top