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