CustomValidation not firing on second click - asp.net

I have text box and a button in a gridview. The textbox has validation when the button is clicked. However the validation event never gets fired when the button is clicked a second time.
<asp:GridView ID="ChapterGridView"
EnableSortingAndPagingCallbacks="false"
AllowSorting="false"
AllowPaging="false"
runat="server"
AutoGenerateColumns="False"
CellPadding="2"
ForeColor="#333333"
GridLines="None"
Width="780px"
OnRowCommand="ChapterGridView_OnRowCommand"
ShowFooter="False"
AutoGenerateDeleteButton="true"
AutoGenerateEditButton="true"
onrowediting="ChapterGridView_RowEditing"
onrowdeleting="ChapterGridView_RowDeleting"
onrowcancelingedit="ChapterGridView_RowCancelingEdit"
onrowupdating="ChapterGridView_RowUpdating"
onrowupdated="ChapterGridView_RowUpdated"
DataKeyNames="ChapterId"
ValidationGroup="ChapterValidation"
Visible="true">
<FooterStyle BackColor="#eeeeee" Font-Bold="True" ForeColor="White" />
<Columns>
<asp:TemplateField HeaderText="*End Page" HeaderStyle-HorizontalAlign="Left">
<ItemTemplate><%# Eval("EndPage")%></ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="EndPage" runat="Server" Text='<%# Eval("EndPage") %>'></asp:TextBox>
<asp:CustomValidator ID="TotalPagesValidator" ValidationGroup="ChapterValidation" OnServerValidate="TotalPages_ServerValidate" EnableClientScript="false" ErrorMessage="Number of pages in chapter are greater than number of pages in entire publication." Display="None" ControlToValidate="EndPage" runat="server" />
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="EndPage" runat="Server"></asp:TextBox>
<asp:CustomValidator ID="TotalPagesValidator2" ValidationGroup="ChapterValidation" OnServerValidate="TotalPages_ServerValidate" EnableClientScript="false" ErrorMessage="Number of pages in chapter are greater than number of pages in entire publication." Display="None" ControlToValidate="EndPage" runat="server" />
<asp:Button ID="btnInsert" runat="Server" Text="Insert" CommandName="Insert" UseSubmitBehavior="false" ValidationGroup="ChapterValidation" /></span>
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
The same validation for edit works as expected. But not for the Insert button. Any suggestions?

1: Did you set the breakpoint to make sure there is a postback? Is the serverside validation called at all or not?
2: Can you try changing the ValidationGroup for Insert to something else than the Edit.
Edit There are issues using ValidatorCallOut with CustomValidator with ServerSide Validation.
http://forums.asp.net/t/1054676.aspx
http://programminginhell.wordpress.com/2008/08/03/hello-world/
Here's another way showing error in a jquery dialog: http://weblogs.asp.net/gurusarkar/archive/2011/03/28/part-3-showing-asp-net-server-side-messages-in-a-custom-dialog-server-side-with-asp-net-ajax.aspx

#TrekStir - thanks for pointing me in the right direction.
The problem was that there was code to make the footer invisible after a row was added. For some reason that was causing the validation code not to fire. I figured out a way to implement the functionality without setting the footer to invisible and this solved the problem.

Related

return confirm inside gridview does not fire

In my code pasted below the grid has an option to delete the row on click of remove button. Before hitting server side code i want to confirmation from the user to delete the record.
But always server side code is hit rather than the confirm popup showing first.
<asp:GridView ID="grdDelegateList" runat="server" CssClass="gridviewBorder" AutoGenerateColumns="False"
CellPadding="1" Style="margin-left: 0px" BackColor="White" Font-Names="Calibri"
BorderWidth="1px" Width="100%" AllowPaging="True" GridLines="Horizontal" RowHoverBackColor="#666666"
RowHoverForeColor="White" SelectedRowStyle-BackColor="#333333" SelectedRowStyle-ForeColor="White"
PageSize="10" OnPageIndexChanging="grdDelegateList_PageIndexChanging" OnRowCommand="grdDelegateList_RowCommand"
OnRowDataBound="grdDelegateList_RowDataBound" OnRowDeleting="grdDelegateList_RowDeleting">
<Columns>
<asp:BoundField HeaderText="Employee ID" DataField="DelegateID" ItemStyle-HorizontalAlign="Center" HeaderStyle-HorizontalAlign="Center" />
<asp:TemplateField HeaderText="Full Name" ItemStyle-HorizontalAlign="Center" HeaderStyle-HorizontalAlign="Center">
<ItemTemplate>
<p>
<%#DataBinder.Eval(Container.DataItem, "Owner.FirstName")%>
<%#DataBinder.Eval(Container.DataItem, "Owner.LastName")%>
</p>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Remove" ItemStyle-HorizontalAlign="Center" HeaderStyle-HorizontalAlign="Center">
<ItemTemplate>
<span style="cursor: pointer">
<asp:LinkButton ID="ImgRemove" runat="server" CommandName="Delete" CommandArgument='<%# Eval("ID") %>'
Text="Remove" OnClientClick="return confirm(Are you sure you want to remove this Delegate);">
</asp:LinkButton></span>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
What i want to do is show a confirm box if user presses the on button server side code (ie: row command ) event should be hit, other wise on cancel it should do nothing. but its not working.
Your code for the confirm() function is not valid.
confirm() expects a string variable (see window.confirm) yet in your case you are passing in an invalid object and would receive the error
Uncaught SyntaxError: Unexpected identifier
Update your code to be;
<asp:LinkButton
ID="ImgRemove"
runat="server"
CommandName="Delete"
CommandArgument='<%# Eval("ID") %>'
Text="Remove" OnClientClick="return confirm('Are you sure you want to remove this Delegate');">
My suggestion would be to add single quotes to the OnClientClick
OnClientClick="return confirm('Are you sure you want to remove this Delegate');
you can also try the following code:
OnClientClick="if (!confirm('Are you sure you want to remove this Delegate')) return false;"
Try below snippet.
<asp:LinkButton ID="ImgRemove" runat="server" CommandName="Delete" CommandArgument='<%# Eval("ID") %>' Text="Remove" OnClientClick="if (!confirm('Are you sure you want to remove this Delegate?'));">
OnClientClick="if (!confirm('Are you sure you want to remove this Delegate?'));
will show the alert with Yes/No options. If you click on No then nothing will happen. If you click on Yes then corresponding code will execute

CheckBoxList OnSelectedChange event not fired

I Have this type of code in Gridview in UpdatePanel..
Now when I Checked CheckBox then it fire its OnSelectedIndexChanged event But When I Select two or three CheckBox and then uncheck any checkbox among selected then it's
OnSelectedIndexChanged event is not fired..why this happen
<asp:Label ID="lbltempCity" runat="server" Text="City"></asp:Label>
<asp:CheckBoxList ID="lst" AutoPostBack="true" runat="server" Width="100px" Height="100px" BorderWidth="1px" BackColor="White" OnSelectedIndexChanged="hello">
<asp:ListItem>Test1</asp:ListItem>
<asp:ListItem>Test2</asp:ListItem>
<asp:ListItem>Test1</asp:ListItem>
<asp:ListItem>Test2</asp:ListItem>
</asp:CheckBoxList>
<asp:DropDownExtender ID="DropDownExtender1" runat="server" DropArrowWidth="300px" TargetControlID="lbltempCity" DropDownControlID="lst">
</asp:DropDownExtender>
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lblCity" runat="server" Text='<%# Eval("tempCity") %>'></asp:Label>
</ItemTemplate>
<ItemStyle Width="15%" />
</asp:TemplateField>
I tested the code it is working fine. May be cause of problem is in grid or any other control in your page. post the full code so that we can help.

Validating controls in ListView insert/edit templates

I added a RequiredFieldValidator to my InsertItemTemplate, and it seems to be working fine. The problem I am having, however, is that now I cannot do anything else in the ListView (like edit or delete items) UNLESS the required field has a value. Is there some way I can manually do the validation when the user clicks the 'Insert' button on the InsertItemTemplate, or some other little trick I can perform so the user doesn't have to first type in a value just to delete something else in the list?
Thanks
A_Nablsi,
Please provide the code for your solution to turn the Insert New validation controls off when in Edit/Update mode or turn Edit/Update validation controls off when both the Edit and Insert Rows are active at the same time. This code using your notional solution fails with a null reference to the updateButton.
LinkButton updateButton = LVTasks.EditItem.FindControl("UpdateButtonTask") as LinkButton;
updateButton.CausesValidation = false;
The solution that works is adding Validation Groups.
Include ValidationGroup="myVGEdit" with your Validator Control(s) in the EditItemTemplate and your Update button.
Include ValidationGroup="myVGInsert" with your Validator Control(s) in the InsertItemTemplate and your Insert button.
<asp:ListView ID="LVTasks" runat="server"
DataKeyNames="IDTask"
DataSourceID="LDS_LVTasks"
InsertItemPosition="FirstItem"
oniteminserting="LVTasks_ItemInserting"
onitemupdating="LVTasks_ItemUpdating"
onitemcommand="LVTasks_ItemCommand"
>
<EditItemTemplate>
<asp:TextBox ID="TaskUpdateTextBox" runat="server"
Text='<%# Bind("Task") %>'
TextMode="MultiLine" Rows="1" Font-Bold="true" Width="300px"
/>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ErrorMessage="Please Set Task title"
ControlToValidate="TaskUpdateTextBox"
ValidationGroup="myVGUpdate"
/>
<asp:LinkButton ID="UpdateCancelButton" runat="server"
CommandArgument='<%#Eval("IDTask") %>'
CommandName="Cancel"
CausesValidation="False"
ToolTip="Cancel - Abort - No Changes"><div class="Cancel"></div></asp:LinkButton>
<asp:LinkButton ID="UpdateButtonTask" runat="server"
CommandArgument='<%#Eval("IDTask") %>'
CommandName="Update"
CausesValidation="True"
ValidationGroup="myVGEdit"
ToolTip="Save Changes - Update"><div class="Update" ></div></asp:LinkButton>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TaskInsertTextBox" runat="server" Text='<%# Bind("Task") %>'
TextMode="MultiLine" Rows="1" Font-Bold="true" Width="300px"
/>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ErrorMessage="Please Set Task title"
ControlToValidate="TaskInsertTextBox"
ValidationGroup="myVGInsert"
/>
<asp:LinkButton ID="CancelButton" runat="server"
CommandArgument='<%#Eval("IDTask") %>'
CommandName="Cancel"
CausesValidation="False"><div class="Clear" ></div></asp:LinkButton>
<asp:LinkButton ID="InsertButtonTask" runat="server"
CommandArgument='<%#Eval("IDTask") %>'
CommandName="Insert"
CausesValidation="true"
ValidationGroup="myVGInsert"
><div class="Insert" ></div></asp:LinkButton>
</InsertItemTemplate>
Yes,
set the CausesValidation property to false on the controls you don't want them to trigger the validation.

ASP Updatepanel inside content disappears

I have an update panel with a gridview and some radios inside it. Senario is that when user select a radio, some bottoms get visible. But after radio eventhandler is trigered, updatepanel contents get dissapered. Any idea about this problem?
<asp:ScriptManager ID="scriptManager_main" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="updatePanel_main" runat="server">
<ContentTemplate>
<asp:GridView ID="gridView_stLists" runat="server" AutoGenerateColumns="False" CellPadding="3"
BorderStyle="NotSet" CssClass="table_layout" Width="500">
<RowStyle CssClass="table_body" />
<Columns>
<asp:TemplateField HeaderStyle-Width="20">
<ItemTemplate>
<asp:RadioButton ID="rdBtn_stdl" runat="server" OnCheckedChanged="rdBtn_stdl_CheckedChanged"
AutoPostBack="True" GroupName="stdl" value='<%# Eval("uri") %>' />
</ItemTemplate>
<HeaderStyle Width="20px" />
</asp:TemplateField>
...
The RadioButton is doing an AutoPostBack. Are you rebinding to the GridView after postback and thus overridding your changes/state? Only DataBind if !IsPostBack and this might address the issue.

ASP.Net AJAX TabControl

I have placed an AJAX Tabcontrol on my page.
Inside the TabControl, I also placed a gridview.
<cc1:TabContainer id="tabconLandTransPlan" runat="server" Height="300px" ActiveTabIndex="0" AutoPostBack="True">
<cc1:TabPanel runat="server" ID="tabMasterPlan" HeaderText="Master Plan" >
<HeaderTemplate>
<span style="font-size: 8pt; font-family: Arial">Master Plan</span>
</HeaderTemplate>
<ContentTemplate>
<asp:GridView id="gvBuffer" runat="server" Width="100%" AutoGenerateColumns="False">
<Columns>
<asp:TemplateField HeaderText="Type of Services">
<HeaderStyle Width="26%"></HeaderStyle>
<ItemTemplate>
<asp:Label id="Label1" runat="server" Text='<%# EVAL("code_desc") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Tariff Code">
<HeaderStyle Width="4%" HorizontalAlign="Center"></HeaderStyle>
<ItemStyle HorizontalAlign="Center"></ItemStyle>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# EVAL("res_code") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</ContentTemplate>
</cc1:TabPanel>
When I retrieve the gridview, the gridview boundaries extend beyond the Tab Control boundaries. How Can I ensure that the gridview will be just within the boundaries of the tab control? The height of the Gridview is extending beyond the tab control. The width is just fine.
Thanks.
I may have found the answer to this little problem.
Apparently, the ajax tab control follows the size (height) of the controls inside it.
So what I did was set the height of the tabcontrol to Nothing (VB.Net).
Setting it to zero or any other percentage (conversion) values would otherwise throw an error.
Thanks to those who viewed.
Increase the height of the tab control!

Resources