GridView TextField validation still allowing next action despite errors - asp.net

I have a GridView which has a TextField column in it .. I've setup validation for this TextField column so that it requires an input (i.e. its not optional), and that the input can be a positive integer only ..
The errors do show up when a text field is either empty or has doesn't have a positive integer value, but there's a server-side button which still executes even when there are errors in the GridView ..
I want the button to NOT do its processing if there are inputs errors .. Currently this doesn't happen, as the button's click event is still called even when there are errors ..
GridView Markup Code:
<asp:GridView ID="EPSAndTSRValuesInputGridView" runat="server" ShowFooter="true"
AutoGenerateColumns="false">
<Columns>
<asp:TemplateField HeaderText="EPS Value">
<ItemTemplate>
<asp:TextBox ID="EPSValue" Text='<%# Eval("EPSValue") %>' runat="server" CausesValidation="True" ValidationGroup="Display"></asp:TextBox>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ErrorMessage="Enter a valid value for EPS!"
ValidationExpression="^\d*$" ControlToValidate="EPSValue" ValidationGroup="Display"/>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="*"
ControlToValidate="EPSValue" ValidationGroup="Display"/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Button which performs the next action:
<asp:Button ID="btnDisplayReport2"
runat="server" CssClass="ButtonStyle"
Text="Display Report" ValidationGroup="Display" OnClick="btnDisplayReport2_Click" CausesValidation="true"/>

This is happening because your Button have validation group Display so on click of it will validate only control with same group i.e. Display.As I can see there no validation group of your Textbox so it will not validate it in button click..,to cause validation on Click of Button Add same validation group in your Textbox,RegularExpressionValidator, and RequiredFieldValidator too.

I think it's because they don't have the same ValidationGroup.
Try adding ValidationGroup="Display" to your validators.

You are saying to execute the validation group Display on your button click.It will validate only control have the validation group (dispaly) as you mentioned.You are not defined any validation group for Textbox validators Try by Add same validation group(displa) in your Textbox,RegularExpressionValidator, and RequiredFieldValidator.

Related

PNM Sequence & asp.net: GridTemplateColumn should be mandatory

I use PNM Sequence. And I need to make one grid column as the mandatory field.
I know how to make it with any separate control. E.g. I can type:
<sq8:GridBoundColumn DataField="txtField" HeaderText="txtField"
SortExpression="txtField" UniqueName="txtField" FilterControlAltText="">
<ColumnValidationSettings>
<RequiredFieldValidator ForeColor=""></RequiredFieldValidator>
</ColumnValidationSettings>
</sq8:GridBoundColumn>
And I can use this Validator for the TextBox:
<sq8:Label runat="server" Text="Field:" ID="Label1" Width="100%"></sq8:Label>
<nobr>
<sq8:TextBox runat="server" ID="txtField" Width="100%"></sq8:TextBox>
<sq8:RequiredFieldValidator runat="server"
ErrorMessage="RequiredFieldValidator"
ID="RequiredFieldValidator4"
ControlToValidate="txtField"
SetFocusOnError="True">*</sq8:RequiredFieldValidator>
</nobr>
<sq:BindableControl runat="server" TargetControlID="txtField"
DataField="txtField"></sq:BindableControl>
And it works. User can't send the form because he gets an error - the field is empty.
But I need to do the same with grid.
When I open "Edit columns" in Grid Wizard I can't see any property as "mandatory" or something like this.
And the code with RequiredFieldValidator doesn't work with a grid column. If I try to use it:
<Columns>
<sq8:GridBoundColumn DataField="txtFieldGrid" HeaderText="txtFieldGrid"
SortExpression="txtFieldGrid" UniqueName="txtFieldGrid"
FilterControlAltText="">
<sq8:RequiredFieldValidator runat="server"
ErrorMessage="RequiredFieldValidator"
ID="RequiredFieldValidator4"
ControlToValidate="txtFieldGrid"
SetFocusOnError="True">*</sq8:RequiredFieldValidator>
<sq:BindableControl runat="server" TargetControlID="txtFieldGrid"
DataField="txtFieldGrid"></sq:BindableControl>
</sq8:GridBoundColumn>
</Columns>
In this case, I have an error:
Is there some method for grid column validation? Or it's impossible with a grid?
Maybe I can use some javascript?

ASP.NET Validation Group Firing Without Being Called

This is the issue I have going right now with validation controls and I simply can't find answer anywhere for it.
I currently have a required field validator watching a listbox. The validator's initial value property is set to "None". The first item in the listbox's value is also set to "None", thus forcing the user to change the selection. However I don't want the validation to show on the selected index change, I want it to show after a button press. So I changed the validation group of the validator and the button to "final" however the validator still shows on selected index changed without the button being pressed.
My code on the validator:
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"
ErrorMessage="Please Select an Incident" ControlToValidate="lstIncidents"
ValidationGroup="final" CssClass="style3" InitialValue="None" Display="Dynamic">
</asp:RequiredFieldValidator>
My Code on the Submit Button:
<asp:Button ID="btnSubmit" runat="server" Text="Submit" ValidationGroup="final"
Enabled="False" />
Anyone see what might be wrong here?
Thanks in advance!
Set the validator's EnableClientScript to false (default is true):
<asp:RequiredFieldValidator EnableClientScript="False" ID="RequiredFieldValidator2" runat="server"
ErrorMessage="Please Select an Incident" ControlToValidate="lstIncidents"
ValidationGroup="final" CssClass="style3" InitialValue="None" Display="Dynamic">
</asp:RequiredFieldValidator>

validation group validations programatically in asp.net with vb.net as code behind

I have a panel, controls in the panel..these controls validated with a validation group.
This panel will show / hide when a dropdown box changes.
Question is...i want to validate all the controls with validation group in the panel
how to write the code
You define validationgroup="Test" on your validators , and you define also this validationgroup="Test" on your Button.
Sample
.....
<asp:requiredfieldvalidator id="NameTextBoxRequiredValidator"
controltovalidate="NameTextBox"
display="Dynamic"
text="Please enter your name."
validationgroup="ForPanel" <----------------
runat="server"/>
<asp:button id="SubmitButton"
text="Submit"
validationgroup="ForPanel" <------------------
runat="server"/>
Nota : Gets or sets the name of the validation group to which this validation control belongs.

ASP.NET Gridview: How do I stop a <span></span> within a Gridview from resetting when posting back?

I have a Gridview which contains some controls which are updated from the server when the user makes a selection from a gridview.
After making their selection from a drop down list the user can enter a percentage into a textbox on a row and I have some javascript which then carries out some calculations and outputs to a span within an item template in the Gridview.
This all works but if the user then makes another drop down list selection on any row all the spans in the gridview are reset to blank (I've tried with various controls, both asp.net and html).
This is all inside an Update Panel.
Code:
<asp:GridView runat="server" ID="gdvIngredients" AutoGenerateColumns="false" CssClass="table table-stripped"
GridLines="None" Visible="false" ShowFooter="true" EnableViewState="true">
<Columns>
<asp:TemplateField Visible="false">
<ItemTemplate>
<asp:Label runat="server" ID="lblId" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Ingredient" ItemStyle-Width="12%">
<ItemTemplate>
<asp:DropDownList runat="server" ID="ddlName" AutoPostBack="true" OnSelectedIndexChanged="ddlName_SelectedIndexChanged"
ForeColor="Black" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="%" ItemStyle-Width="6%">
<ItemTemplate>
<asp:TextBox runat="server" Text="0" Width="50px" ID="txtPercentage" onkeyup="calculate(this);"
ForeColor="Black" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="kj" ItemStyle-Width="6%">
<ItemTemplate>
<asp:Label runat="server" ID="lblKj" CssClass="Kj" Style="display: none;" />
<span runat="server" id="spnKj" class="NewKj">0</span>
</ItemTemplate>
From your question what i have understood is that you are doing calculation in javascript and assigning it to span,but when next postback happens the value is not retained...If i am right i would suggest you to try the following steps.Use HiddenFields also to assign Values as HiddenFields Retain values during PostBacks.
Add Hidden field in Item Template
Set "calculate" function to Textbox from Codebehind(from rowdatabound event) & also pass the currosponding HiddenField Value to it as calculate(this,'HiddenFieldId');
protected void grd_RowDataBound(object sender,GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
HiddenField hdn = ((HiddenField)e.Row.FindControl("HiddenFieldID"));
TextBox txtPercentage = (TextBox)e.Row.FindControl("txtPercentage");
HtmlGenericControl spnKj=(HtmlGenericControl )e.Row.FindControl("spnKj");
txtPercentage.Attributes["onClick"]="calculate(this,'"+hdn .ClientID+"');"
spnKj.innerHTML=hdn.Value;
}
}
Javascript:
Assign Calculated Value to Hidden Field...
function calculate(hdnID)
{
var CalcValue=//do your Calculation
document.getElementByID(hdnID).Value=CalcValue;
}
3.In the DropDown PostBack Event Bind the GridView Again.
The above code should take care of the Issue You are facing.
You can use the "AutoPostBack" property to control the Postback event of some controls.
Have a look if your dropdown list selection do any PostBack.
If it does, then span values disappear, becuase Span is not ASP.NET webcontrol. After pages is postback, asp.net webcontrol values are restored from ViewState. So, spans doesn't have ViewState and no values are restored.
You can try to change your tag to
<span runat="server" id="spanIDHere">SomeValue here</span>
runat="server" makes span to be HtmlGenericControl and maintains its ViewState.
Span values would not be saved after postback if you update them using Javascript. You should call server-side function to update span values or try to use Hidden fields to store your values.

FindControl("someTextBox") in GridView not sending the updated value

Im populating a GridView from List so am forced to use TemplateField controls to allow editing. This requires displaying a TextBox populated with the original value when in edit mode and using FindControl to get the new value out on update submit.
Problem is foundTextBox.Text == "OriginalTextBoxValue"
<asp:TemplateField HeaderText="A Field">
<ItemTemplate>
<asp:Label ID="_theLabel" runat="server" Text='<%# Eval("AField") %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="_theTextBox" runat="server" Text='<%# Eval("AField") %>' />
</EditItemTemplate>
</asp:TemplateField>
And the code in my update event handler
TextBox newText = (TextBox)_myGridView.Rows[e.RowIndex].FindControl("_thTextBox");
//newText.Text == the old value of the text box
Is your gridview binded at every postback? This could explain why you never get the updated value, because the gridview is rebinded before reading the textbox.
Could you paste your complete update method?
You've got the code behind in the wrong event handler. Move it to the Editing event handler, so it will populate the textbox whenever the user clicks on the Edit command for a row.

Resources