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