Saturday, March 24, 2012

Validation not working with gridview template field

I am using a Gridview to Edit, Delete, and Insert (thru the footer). Very similar to Enhancing the Gridview section of the 3-tier Architecture tutorial. I have included the code below. When you click Edit, validation is completely skipped. I catch the missing required fields in the database stored proc. When you click the Insert button, you quickly see the red * and then get an exceptoin in the ObjectDataSource.Updating code because data values are missing. Validation controls seem simple enough, so my error is probably simple. However, I am glazed over from looking at it. Any help is appreciated. Thanks

<asp:GridView

ID="GridView1"runat="server"DataSourceID="ObjectDataSource1"AutoGenerateColumns="False"DataKeyNames="PN"EmptyDataText="No Products Found in Database!"Style="z-index: 100; left: 24px; position: absolute;top: 112px"ShowFooter="True">

<Columns>

<asp:TemplateFieldShowHeader="False">

<EditItemTemplate>

<asp:LinkButtonID="btnUpdate"runat="server"CausesValidation="True"CommandName="Update"Text="Update"/>

<asp:LinkButtonID="btnCancel"runat="server"CausesValidation="False"CommandName="Cancel"Text="Cancel"/>

</EditItemTemplate>

<ItemTemplate>

<asp:LinkButtonID="btnEdit"runat="server"CausesValidation="False"CommandName="Edit"Text="Edit"/>

<asp:LinkButtonID="btnDelete"runat="server"CausesValidation="False"CommandName="Delete"Text="Delete"OnClientClick="return confirm('Delete Product?');"/>

</ItemTemplate>

<FooterTemplate>

<asp:LinkButtonID="btnInsert"runat="server"CausesValidation="true"CommandName="Insert"Text="Insert"/>

</FooterTemplate>

</asp:TemplateField>

<asp:TemplateField>

<HeaderTemplate>

<asp:LabelID="lblPNHeader"runat="server"Text="PN"></asp:Label>

</HeaderTemplate>

<ItemTemplate>

<asp:LabelID="lblPN"runat="server"Text='<%#Bind("PN")%>'SkinID="DataDisplay"/>

</ItemTemplate>

<EditItemTemplate>

<asp:LabelID="lblPNEdit"runat="server"Text='<%#Bind("PN")%>'SkinID="DataDisplay"/>

</EditItemTemplate>

<FooterTemplate>

<asp:TextBoxID="txtPNNew"runat="server"/>

</FooterTemplate>

</asp:TemplateField>

<asp:TemplateField>

<HeaderTemplate>

<asp:LabelID="lblDescHeader"runat="server"Text="Description"></asp:Label>

</HeaderTemplate>

<ItemTemplate>

<asp:LabelID="lblDesc"runat="server"Text='<%#Bind("PNDesc")%>'SkinID="DataDisplay"/>

</ItemTemplate>

<EditItemTemplate>

<asp:TextBoxID="txtDescEdit"runat="server"Text='<%#Bind("PNDesc")%>'/>

<asp:RequiredFieldValidator

ID="validDescEdit"

ControlToValidate="txtDescEdit"

ValidationGroup="EditValidation"

runat="server"

ErrorMessage="Description is Required"

Text="*"/>

</EditItemTemplate>

<FooterTemplate>

<asp:TextBoxID="txtDescNew"runat="server"/>

<asp:RequiredFieldValidator

ID="validDescNew"

ControlToValidate="txtDescNew"

ValidationGroup="NewValidation"

Display="Dynamic"

ForeColor=""

runat="server"

ErrorMessage="Does This Prduct have a Name?"

Text="*"/>

</FooterTemplate>

</asp:TemplateField>

<asp:TemplateField>

<HeaderTemplate>

<asp:LabelID="lblCatHeader"runat="server"Text="Category"></asp:Label>

</HeaderTemplate>

<ItemTemplate>

<asp:LabelID="lblCat"runat="server"Text='<%#Bind("CategoryName")%>'SkinID="DataDisplay"/>

</ItemTemplate>

<EditItemTemplate>

<asp:DropDownList

ID="ddlCatEdit"

runat="server"

DataSourceID="ObjectDataSource2"

DataTextField="CategoryName"

DataValueField="CategoryID"

SelectedValue='<%#Bind("CategoryID") %>'/>

</EditItemTemplate>

<FooterTemplate>

<asp:DropDownList

ID="ddlCatNew"

runat="server"

DataSourceID="ObjectDataSource2"

DataTextField="CategoryName"

DataValueField="CategoryID"/>

</FooterTemplate>

</asp:TemplateField>

<asp:TemplateField>

<HeaderTemplate>

<asp:LabelID="lblUnitHeader"runat="server"Text="Unit $"></asp:Label>

</HeaderTemplate>

<ItemTemplate>

<asp:LabelID="lblUnit"runat="server"Text='<%#Bind("UnitPrice")%>'SkinID="DataDisplay"/>

</ItemTemplate>

<EditItemTemplate>

<asp:TextBoxID="txtUnitEdit"runat="server"Text='<%#Bind("UnitPrice")%>'/>

<asp:RequiredFieldValidator

ID="validUnitEdit"

ControlToValidate="txtUnitEdit"

ValidationGroup="EditValidation"

runat="server"

ErrorMessage="I Doubt This Product is Free."

Text="*"/>

</EditItemTemplate>

<FooterTemplate>

<asp:TextBoxID="txtUnitNew"runat="server"/>

<asp:RequiredFieldValidator

ID="validUnitNew"

ControlToValidate="txtUnitNew"

ValidationGroup="NewValidation"

Display="Dynamic"

ForeColor=""

runat="server"

ErrorMessage="I Doubt This Product is Free."

Text="*"/>

</FooterTemplate>

</asp:TemplateField>

<asp:TemplateField>

<HeaderTemplate>

<asp:LabelID="lblUMHeader"runat="server"Text="UM"></asp:Label>

</HeaderTemplate>

<ItemTemplate>

<asp:LabelID="lblUM"runat="server"Text='<%#Bind("UM")%>'SkinID="DataDisplay"/>

</ItemTemplate>

<EditItemTemplate>

<asp:DropDownList

ID="ddlUMEdit"

runat="server"

DataSourceID="UMDataSource"

DataTextField="UM"

DataValueField="UM"

SelectedValue='<%#Bind("UM") %>'/>

</EditItemTemplate>

<FooterTemplate>

<asp:DropDownList

ID="ddlUMNew"

runat="server"

DataSourceID="UMDataSource"

DataTextField="UM"

DataValueField="UM"/>

</FooterTemplate>

</asp:TemplateField>

<asp:TemplateField>

<HeaderTemplate>

<asp:LabelID="lblSupplyHeader"runat="server"Text="Supply"></asp:Label>

</HeaderTemplate>

<ItemTemplate>

<asp:CheckBox

ID="chkSupply"

runat="server"

Enabled="false"

Checked='<%#Bind("Supply") %>'/>

</ItemTemplate>

<EditItemTemplate>

<asp:CheckBox

ID="chkSupplyEdit"

runat="server"

Checked='<%#Bind("Supply") %>'/>

</EditItemTemplate>

<FooterTemplate>

<asp:CheckBox

ID="chkSupplyNew"

runat="server"/>

</FooterTemplate>

</asp:TemplateField>

</Columns>

</asp:GridView>

<asp:ValidationSummary

ID="EditValidationSummary"

runat="server"

ShowMessageBox="True"

ShowSummary="False"

Style="z-index: 101; left: 24px; position: absolute; top: 280px"

ValidationGroup="EditValidation"/>

<asp:ValidationSummary

ID="NewValidationSummary"

runat="server"

ShowMessageBox="True"

ShowSummary="False"

Style="z-index: 103; left: 24px; position: absolute; top: 344px"

ValidationGroup="NewValidation"/>

There should e no validation happening when you click the edit button. The validation should happen on the clicking of the Update button. So, I guess, if you could be more specific on what field and/or what event you are hoping to catch the validation errors on would be helpful.


All the CommandButtons are in templates fields. CausesValidation is true for the Update and Insert Commands. CausesValidation is false for Edit and Delete. The code should show that. If we just stick with the Edit functionality, only two fields require validation: txtUnitEdit and txtDescEdit. The code for each EditTemplate and the ValidationSummary are as follows. ValidationSummary is outside the GridView. If you wipe out the contents of the txtDescEdit textbox and click the Update button, you can quickly see the RED asterisk display next to the txtDescEdit textbox, but the code continues execution. What am I missing? Since the ObjectDataSource takes care fo the Update event, I don't think I need a Page.IsValid anywhere in the code behind page. Correct?

<EditItemTemplate>

<asp:TextBoxID="txtDescEdit"runat="server"Text='<%#Bind("PNDesc")%>'/>

<asp:RequiredFieldValidatorID="validDescEdit"ControlToValidate="txtDescEdit"ValidationGroup="EditValidation"runat="server"ErrorMessage="Description is Required"Text="*"/>

</EditItemTemplate>

<EditItemTemplate>

<asp:TextBoxID="txtUnitEdit"runat="server"Text='<%#Bind("UnitPrice")%>'/>

<asp:RequiredFieldValidatorID="validUnitEdit"ControlToValidate="txtUnitEdit"ValidationGroup="EditValidation"runat="server"ErrorMessage="I Doubt This Product is Free."Text="*"/>

</EditItemTemplate>

<asp:ValidationSummaryID="EditValidationSummary"runat="server"ShowMessageBox="True"ShowSummary="False"Style="z-index: 101; left: 24px; position: absolute; top: 280px"ValidationGroup="EditValidation"/>


It is good practice to use the Page.IsValid check even when using client side validation. Are you sure Javascripting is not turned off in your browser? If a browser does not allow javascripts, then the client side scripting is bypassed and the validation controls will be handled on the server side, which means that you will have to check the Page.IsValid before continuing to process your event.

http://weblogs.asp.net/rajbk/archive/2007/03/15/page-isvalid-and-validate.aspx

The link was helpful. I added the Page.Validate and If Not Page.IsValid into the OnClick event for the Update and New buttons. I was under the assumption that ASP.NET handled all the events for command buttons in a GridView. Since I put the buttons in Template fields, I guess I need to handle the Click event with code. Here is the page-behind code for the Update button. Thanks for the help.

ProtectedSub btnUpdate_Click(ByVal senderAsObject,ByVal eAs System.EventArgs)

' Validate the controls in the EditValidation group

Page.Validate("EditValidation")

IfNot Page.IsValidThen

Return

EndIf

EndSub

No comments:

Post a Comment