473,581 Members | 2,789 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

GridView RequiredFieldVa lidator Display

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 requiredfieldva lidator.

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="Defau lt2.aspx.vb"
Inherits="Defau lt2" %>

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

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

<head runat="server">

<title>Untitl ed Page</title>

</head>

<body>

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

<div>

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

AutoGenerateCol umns="false" OnRowCommand="G ridView1_RowCom mand"

OnRowDeleting=" GridView1_RowDe leting" OnRowEditing="G ridView1_RowEdi ting"

ShowFooter="Tru e">

<Columns>

<asp:BoundFie ld DataField="Cate goryID"

HeaderText="Cat egoryID" />

<asp:BoundFie ld DataField="Cate goryName"

HeaderText="Cat egoryName" />

<asp:BoundFie ld DataField="Desc ription"

HeaderText="Des cription" />

<asp:TemplateFi eld HeaderText="Tem plateField">

<ItemTemplate >

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

CommandName="Ed it" EnableViewState ="False" />

<asp:ImageButto n ID="imgBtn" runat="server"

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

CommandName="Ed it" EnableViewState ="False" />

</ItemTemplate>

<FooterTemplate >

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

CommandName="In sert" ValidationGroup ="Group1" EnableViewState ="False" />

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

<asp:RequiredFi eldValidator ID="RequiredFie ldValidator2" runat="server"
ControlToValida te="TextBox1"

Display="Dynami c" EnableClientScr ipt="False" EnableViewState ="False"
ErrorMessage="R equired" ValidationGroup ="Group1"
SetFocusOnError ="True"></asp:RequiredFie ldValidator>

</FooterTemplate>

</asp:TemplateFie ld>

<asp:CommandFie ld ShowDeleteButto n="True" />

</Columns>

</asp:GridView>

&nbsp;<br />

</div>

</form>

</body>

</html>

Partial Class Default2

Inherits System.Web.UI.P age

Private Sub BindGrid()

Dim categories(10) As CategoryVO

Dim i As Integer

For i = 0 To categories.Leng th - 1

Dim vo As New CategoryVO()

vo.CategoryID = i + 1

vo.CategoryName = "Category_" + vo.CategoryID.T oString

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

categories(i) = vo

Next i

GridView1.DataS ource = categories

GridView1.DataB ind()

End Sub

Protected Sub GridView1_RowCo mmand(ByVal sender As Object, ByVal e As
System.Web.UI.W ebControls.Grid ViewCommandEven tArgs) Handles
GridView1.RowCo mmand

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

Me.Page.Validat e("Group1")

Response.Write( "<br/>GridView1_RowC ommand: IsValid = " +
Page.IsValid.To String)

End Sub

Protected Sub GridView1_RowDe leting(ByVal sender As Object, ByVal e As
GridViewDeleteE ventArgs)

Response.Write( ("<br/>GridView1_RowD eleting " + e.RowIndex.ToSt ring))

End Sub

Protected Sub GridView1_RowEd iting(ByVal sender As Object, ByVal e As
GridViewEditEve ntArgs)

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

End Sub

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

If Not IsPostBack Then

'BindGrid()

End If

End Sub

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

BindGrid()

End Sub

End Class


--

AG
Email: discuss at adhdata dot com


Oct 15 '06 #1
3 11708
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="G ridView1_RowCom mand"; another is defined by your
code-behind: Protected Sub GridView1_RowCo mmand(ByVal sender As Object,
ByVal e As
System.Web.UI.W ebControls.Grid ViewCommandEven tArgs) Handles
GridView1.RowCo mmand

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_RowCo mmand(ByVal sender As Object, ByVal e As
System.Web.UI.W ebControls.Grid ViewCommandEven tArgs)
Response.Write( "<br/>GridView1_RowC ommand: " + e.CommandName)
Me.Page.Validat e("Group1")
flag1 = Not Page.IsValid
Response.Write( "<br/>GridView1_RowC ommand: IsValid = " +
Page.IsValid.To String)
End Sub

Protected Sub Page_PreRender( ByVal sender As Object, ByVal e As
System.EventArg s) 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 (wa****@online. microsoft.com, remove 'online.')
Microsoft Online Community Support

=============== =============== =============== =====
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.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/subscripti...t/default.aspx.
=============== =============== =============== =====

This posting is provided "AS IS" with no warranties, and confers no rights.

Oct 16 '06 #2
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.EventArg s) 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 (wa****@online. microsoft.com, 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.

Oct 16 '06 #3
AG
Thanks Walter,

Both items resolved!

--

AG
Email: discuss at adhdata dot com

"Walter Wang [MSFT]" <wa****@online. microsoft.comwr ote in message
news:Vf******** ******@TK2MSFTN GXA01.phx.gbl.. .
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.EventArg s) 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 (wa****@online. microsoft.com, 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.

Oct 16 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
5095
by: Joe | last post by:
Hello, I have created a login page using dotnet. I am using requiredFieldValidator and noticed that the code works fine in IE but not in Netscape, Opera, Mozilla, Firefox, etc. For example if I do not enter anything in the form in IE, then form won't be submitted but in other browsers it is submitted. I checked that JavaScript is...
3
8748
by: Mike P | last post by:
I must be missing something regarding update parameters. I have tried using <asp:Parameter> to specify my update parameters, and <asp:ControlParameter> when <asp:Parameter> didn't work for a particular parameter. Here is my code : < asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$...
0
1867
by: jeffmagill | last post by:
Hi Everybody, I'm really hoping that someone can help me out because I have spent too much time on this project already! Basically, I have a DetailsView that handles all the Edits for a GridView - each row has an edit button and clicking on this should trigger that record to be loaded into the DetailsView for editing. The problem is that...
0
2381
by: Mike P | last post by:
Where exactly are the updateparameters of a gridview picked up from? I have created 2 very similar gridviews and given the updateparameters the same names as in my edititemtemplates. Yet this method has worked for 1 gridview and failed for the second gridview. Here is my gridview : <asp:SqlDataSource ID="SqlDataSource1" runat="server"...
0
2469
by: Mike P | last post by:
I am trying to edit a gridview while using paging, but whenever I try to edit a row on a page other than page 1, I get an error. Here is my gridview and my code : <asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1" DataKeyNames="UserKey" AllowSorting="True" HeaderStyle-Height="24px" AutoGenerateColumns="false"...
0
1544
by: TheDebbis | last post by:
I'm making a quick CD catalog to help me learn ASP.NET (using C# primarily.) I have a number of gridview's on my site that are working properly, but for some reason, one is not updating or deleting. I have the buttons for both visible, and when I click edit, the update fields show up properly, but once I click 'update' the table is back with it's...
4
12502
by: anniebai | last post by:
Please help me with writing a RowUpdating function in C#, I don't know how to grab the current field's value and also get the old value for one of keys (which is ProjectName for editing) of the selected row. I've tried: e.Keys.Count, e.OldValues.Count, e.NewValues.Count -----------> all give zero Some said if TemplateField is used, e.Keys...
11
6054
by: SAL | last post by:
Hello, I have a Gridview control (.net 2.0) that I'm having trouble getting the Update button to fire any kind of event or preforming the update. The datatable is based on a join so I don't know if that's what's causing the behavior or not. It seems like the Update button should at least do something. When the Edit button is clicked, the...
1
5170
by: Mel | last post by:
I have a GridView that is bound to a DataTable. I decided to allow the user to change the column data right in the ItemTemplate. Then user won't need to click Edit to change the values and then click Update to save them because for this application the user will need to edit the data in every row so I thought the ItemTemplate would be a...
0
7808
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8157
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8312
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7914
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8181
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6564
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3809
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3835
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1145
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.