tech_question
asked on
custom validator in asp.net for gridview when item count is zero and total amount is negative (in footer) ?
I have a grid view, which has a footer . One of the property of this footer is total amount. I need to alert the user when the total amount in the footer is negative amount and there are no items in the grid. i.e. when the items.count == 0 and when the total amount is negative.
how can I right a custom validator to achieve this ? Could someone please provide an example of this ?
how can I right a custom validator to achieve this ? Could someone please provide an example of this ?
Do you want the validation to be done on the client-side(immediate) or on the server-side(after posting to server)?
ASKER
server side please ?
I would recommend using Client-Side validation because it will eliminate the need to make a round-trip to the server. Making round-trips reduces website streamlined performance. Client-Side is extremely fast!
<asp:CustomValidator ID="[Give your validator a name]" runat="server" ErrorMessage="[error message text]"
ControlToValidate="[ID of the control you need to validate]" Display="Dynamic" ClientValidationFunction=" [name of javascript function]"
Font-Bold="True" Font-Size="Large"></asp:Cu stomValida tor>
If you want to use ServerSide Validation you just remove the bold text from above and in the ServerValidate event you can put your code for validating the control. (onServerValidate)
<asp:CustomValidator ID="[Give your validator a name]" runat="server" ErrorMessage="[error message text]"
ControlToValidate="[ID of the control you need to validate]" Display="Dynamic" ClientValidationFunction="
Font-Bold="True" Font-Size="Large"></asp:Cu
If you want to use ServerSide Validation you just remove the bold text from above and in the ServerValidate event you can put your code for validating the control. (onServerValidate)
One side note:
When writing a javascript function for the CustomValidator, you need to have 2 parameters. One should be Source and the other should be Args. Call them what you want but it needs to be there.
EXAMPLE SYNTAX:
function validationFunction(Source, Args){
//LOGIC
}
EXAMPLE FUNCTION:
function validateSentToAdjuster_dt( sender, args) {
var checked = document.getElementById("[ ID of control validating against]").checked;
var argument = args.Value;
if (checked == true && argument == "") {
args.IsValid = false;
return;
}
else if (checked == false && argument != "") {
args.IsValid = false;
return;
}
args.IsValid = true;
}
Your Source is the control that you specified is being validated, while Args is used to tell the CustomValidator whether it should fire or not. (Args.IsValid = true -- Validator does not fire || Args.IsValid = false -- Validator does fire)
I hope that helps...If you need anything further just let me know.
When writing a javascript function for the CustomValidator, you need to have 2 parameters. One should be Source and the other should be Args. Call them what you want but it needs to be there.
EXAMPLE SYNTAX:
function validationFunction(Source,
//LOGIC
}
EXAMPLE FUNCTION:
function validateSentToAdjuster_dt(
var checked = document.getElementById("[
var argument = args.Value;
if (checked == true && argument == "") {
args.IsValid = false;
return;
}
else if (checked == false && argument != "") {
args.IsValid = false;
return;
}
args.IsValid = true;
}
Your Source is the control that you specified is being validated, while Args is used to tell the CustomValidator whether it should fire or not. (Args.IsValid = true -- Validator does not fire || Args.IsValid = false -- Validator does fire)
I hope that helps...If you need anything further just let me know.
ASKER
thank so much for that. I have a different problem now, I need to validate a total amount which is present in the footer of a gridview. I put a hidden field and stored this total amount in to this field and put the custom validator on this hidden field. how can I fire this custom validator on the submit of a button ?
Please send me the code for the gridview control. I don't want the code behind, I would like to see the .aspx. I can help you then...
ASKER
I actually use RadGrid but it is similar to GridView. I have posted the code below. Basically if the d_total_amount is zero or negative , I need to fire the required validations on submit of a button.
<telerik:RadGrid ID="gridInvLines" AllowPaging="False" Skin="Office2007"
runat="server" AutoGenerateColumns="False" DataSourceID="odsInvLines"
GridLines="None" OnItemDataBound="gridInvLines_ItemDataBound" ShowFooter="True">
<ClientSettings >
<ClientEvents OnPopUpShowing="PopUpShowing" />
</ClientSettings>
<MasterTableView DataKeyNames="LINE_NUMBER" DataSourceID="odsInvLines" EditMode="PopUp" InsertItemDisplay="Top" AllowAutomaticInserts="true" AllowAutomaticUpdates="true" CommandItemDisplay="Top" CommandItemSettings-AddNewRecordText="Add New Line">
<Columns>
<telerik:GridEditCommandColumn ButtonType="LinkButton" InsertText="Save" UniqueName="EditButton">
<ItemStyle HorizontalAlign="Center" Width="30px" />
</telerik:GridEditCommandColumn>
<telerik:GridBoundColumn DataField="LINE_NUMBER" HeaderText="Line Number"
UniqueName="LINE_NUMBER" ReadOnly="True">
<HeaderStyle Width="30px" />
<ItemStyle HorizontalAlign="Center" />
</telerik:GridBoundColumn>
<telerik:GridTemplateColumn HeaderText="Quantity" UniqueName="QUANTITY" EditFormColumnIndex="1">
<HeaderStyle Width="30px" />
<ItemTemplate>
<asp:Label runat="server" ID="lblQuantity" Text='<%# Eval("QUANTITY") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" ID="txtQuantity" Text='<%# Bind("QUANTITY") %>'></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvQuantity" ControlToValidate="txtQuantity" ErrorMessage="Please enter Quantity"
runat="server">*</asp:RequiredFieldValidator>
</EditItemTemplate>
</telerik:GridTemplateColumn>
<telerik:GridTemplateColumn HeaderText="Amount" UniqueName="UNIT_SELLING_PRICE" EditFormColumnIndex="1">
<HeaderStyle Width="50px" />
<ItemStyle HorizontalAlign="Right" Width="50px" />
<ItemTemplate>
<asp:Label runat="server" ID="lblUnitPrice" Text='<%# Eval("UNIT_SELLING_PRICE", "{0:C}") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" ID="txtUnitPrice" Text='<%# Bind("UNIT_SELLING_PRICE") %>'></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvUNIT_SELLING_PRICE" ControlToValidate="txtUnitPrice" ErrorMessage="Please enter Amount"
runat="server">*</asp:RequiredFieldValidator>
</EditItemTemplate>
</telerik:GridTemplateColumn>
<telerik:GridBoundColumn DataField="TOTAL_AMOUNT" HeaderText="Total Amount" DataFormatString="{0:c}" ReadOnly="true" ForceExtractValue="None">
<HeaderStyle Width="50px" />
<ItemStyle HorizontalAlign="Right" Width="50px" />
</telerik:GridBoundColumn>
<telerik:GridBoundColumn Visible="false" UniqueName="D_TOTAL_AMOUNT" DataField="TOTAL_AMOUNT" HeaderText="Total Amount" DataType="System.Double" ReadOnly="true" ForceExtractValue="None">
</telerik:GridBoundColumn>
</Columns>
<EditFormSettings EditColumn-UpdateText="Save" CaptionFormatString="Invoice Line Information" ColumnNumber="2">
<FormTableItemStyle Wrap="False" Width="100%"></FormTableItemStyle>
<FormCaptionStyle CssClass="EditFormHeader" Width="100%"></FormCaptionStyle>
<FormMainTableStyle GridLines="Horizontal" CellSpacing="0" CellPadding="3" BackColor="White"
Width="100%" />
<FormTableStyle CellSpacing="0" CellPadding="2" CssClass="module" Height="110px"
BackColor="White" />
<FormTableAlternatingItemStyle Wrap="False"></FormTableAlternatingItemStyle>
<PopUpSettings Modal="true" Width="600" />
</EditFormSettings>
</MasterTableView>
<ValidationSettings CommandsToValidate="PerformInsert,Update" />
</telerik:RadGrid>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
apparently I did that and the validation does not fire. The alert message in the validation also does not fire. I had to make the textbox visible='true' to fire off the alert message that I put to debug but even then the validation does not fire. What am I doing wrong ?
1) do I need to make the textbox control visible = 'true'
2) why isn't the validation firing ? I can see the first alert message that I put to debug but not the actuall error message (for this to happen I had to make the txtTotalLineAmount visible="true".
1) do I need to make the textbox control visible = 'true'
2) why isn't the validation firing ? I can see the first alert message that I put to debug but not the actuall error message (for this to happen I had to make the txtTotalLineAmount visible="true".
function validateTotalAmount(source, args)
{
alert(parseFloat(args.Value) + "I am in debug");
if(parseFloat(args.Value < 0))
{
args.IsValid = false;
}
//args.IsValid = true;
}
<tr>
<asp:TextBox ID="txtTotalLineAmount" runat="server" visiible="false" />
<asp:CustomValidator ID="CustomValidator1" runat="server" ErrorMessage="Invalid input"
ControlToValidate="txtTotalLineAmount" ClientValidationFunction="validateTotalAmount">*</asp:CustomValidator>
My mistake. I left out a very important part. After args.IsValid = False;
On the next line you need: Return;
On the next line you need: Return;
ASKER
It stil does not fire. what am I doing wrong ?
Give me all of your code for the aspx page.
ASKER
kyle thanks for your help, I am actually calling the javascript from a different function and now it works.
You are very welcome!