473,605 Members | 2,387 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 11709
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
5097
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 enabled in Netscape, Opera, Mozilla, Firefox,
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="<%$ ConnectionStrings: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 the record never gets loaded into the DetailsView. Instead, the DetailsView always and only...
0
2384
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" ConnectionString="<%$ ConnectionStrings:XConnectionString %>" SelectCommand="ViewForecast"...
0
2471
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" SkinID="Grey3" EditRowStyle-CssClass="dgedit"
0
1545
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 old information. When I click 'delete' the page completes the postback properly, but once again, the...
4
12504
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 and Oldvalues, NewValues are all empty. Then what suppose to be used in such case? Thanks for any...
11
6056
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 grid goes into Edit mode and the Cancel button takes the grid out of Edit mode. So, I don't get what...
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 better solution. However, when I place the controls in my ItemTemplate my Validators are not being...
0
8423
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8411
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8067
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
8281
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
5885
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5444
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3956
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2437
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1270
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.