Field validation of a single button only - asp.net

I have the following validator on a textbox inside a modal dialog box.
<asp:RequiredFieldValidator runat = "server"
ErrorMessage = "Role name can not be empty."
ControlToValidate = "tbxRoleName" />
It works like it should, except that the validation triggers on every other buttons OnClick handler on the aspx-page too. Since the dialog is invisible it looks like buttons just dont work on the page. My workaround is to add CausesValidation = "false" on all buttons on the page. But it is a very bad solution and I think there should be a smarter way.

Assign ValidationGroup to each validator and also to the button that should trigger validation (but not the the other button). Something like:
<asp:RequiredFieldValidator ValidationGroup='valGroup1' ... />
<asp:Button ValidationGroup='valGroup1' Text='I trigger validation' ... />

How about setting a ValidationGroup?
http://msdn.microsoft.com/en-us/library/ms227424.aspx

Also you can use 'causesvalidation' to the button. If it is false Button will not response to Validation in aspx page.
Example:
<asp:Button runat="server" Text="Cancel" CausesValidation="false" />

The Button has a property CausesValidation that can disable the validation for that button.
More info here: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.button.causesvalidation.aspx

Related

Stop Postback in Image Button after validation failed

I have a aspx page containing a text box and an image button for search. I have used compare validator (to check for integer values) with the textbox. But the page reloads on the image button click even if I enter alphanumeric characters, along with showing the error message.
I tried using a regularexpressionvalidator instead but the problem persists.
But when i used a simple asp:button instead and binded it with textbox validation, its working fine (i.e. postback does not occur on incorrect value of textbox) and same is true with dropdownlist also (no postback occuring).
Please suggest.
Here's the code-
#peroija : Here's the code
<asp:ImageButton ID="btnSearch" runat="server" OnClick="btnSearch_Click"
ToolTip="Search" ValidationGroup="valControl" CausesValidation="true" />
<asp:TextBox ID="txtWidth" CssClass="TextFont" runat="server"
Width="233px" MaxLength="20"
ValidationGroup="valControl" CausesValidation="true"></asp:TextBox>
<asp:CompareValidator runat="server" ID="cmpValWidth"
ErrorMessage="Please enter integer values" ControlToValidate="txtWidth" Display="Dynamic"
Operator="DataTypeCheck" ValidationGroup="valControl"Type="Integer"/>
Sounds to me like you need to write
if(!isPostBack)
{
"your code"
}
in the code behind. To prevent the code from being run if the page is not viewed for the first time
Remove this from your textbox, you only need it on the validator and the button:
ValidationGroup="valControl" CausesValidation="true"
If javascript is disabled, then there will be no client side validation, so always check the validity on the server side also:
if(Page.IsValid)
{
"your btnSearch_Click code"
}

Server Control Auto Postback with form has _blank attribute

My case is I have an asp.net page has a form
<form id="form1" runat="server" target="_blank">
and a button redirect to another page and this page will open in a new window because of the target attribute of the form .
<asp:Button ID="button1" runat="server" PostBackUrl="~/kindofpage.aspx" Text="Generate" />
and I have a dropdownlist has auto postback = true to post the past to fill another dropdownlist by selected data .
<asp:dropdownliast id="Make" name="Make" runat="server" autopostback="true"></asp:dropdownlist>
the question is : why when I select item from the auto postbacked dropdown an blank page opened ?
I need a way to post the page by the dropdownlist without openning a blank page ..
Thank you,
For lack of a better idea, you could just remove the target="_blank" attribute from your markup, and when your button is clicked, modify the form tag with JavaScript and set the attribute.
You can set the OnClientClick property and run JavaScript when it's clicked. For example:
<asp:Button ID="button1" OnClientClick="document.getElementById('form1').setAttribute('target', '_blank')" runat="server" PostBackUrl="~/kindofpage.aspx" Text="Generate" />
You could always just adjust your buttonpress code to open a new window such as this:
<asp:Button ID="myBtn" runat="server" Text="Click me"
onclick="myBtn_Click" OnClientClick="window.open('kindofpage.aspx', 'kindofpage');" />
then remove the:
target="_blank"
From the form tag.
I struggled with a similar situation but solved it in the following way.
As mentioned in this answer, you can use the OnClientClick property to set the target to "_blank". E.g.
<asp:Button ID="button1" OnClick="codebehind_method" OnClientClick="document.forms[0].target = '_blank';" runat="server" Text="targets new window" />
Then, in the aspx page that my "codebehind_method" function redirects to, I reset the target of the opener form like so:
<script type="text/javascript">
function resetTarget() {
opener.document.forms[0].target = '';
}
</script>
<body onload="resetTarget()">
Now, if you go back to your opener form and use a control that does not have the "OnClientClick" property set, the AutoPostBack should occur in the same tab.
If you want to find your form by ID, replace "document.forms[0]" with:
document.getElementByID('yourFormName')
<form id="form1" runat="server">

how to set a default 'enter' on a certain button

There is a textbox on a ContentPage. When the user presses Enter in that textbox I am trying to fire a 'Submit' button on this ContentPage. I'd like to fire off that particular button's event.
Instead, there is a search textbox & button on the top of the page from a MasterPage, and this search button's event fires off.
How do I control to fire off this ContentPage's submit button, instead of the MasterPage's search button?
I am using Ektron CMS for my content management.
The easiest way is to put the fields and button inside of a Panel and set the default button to the button you want to be activated on enter.
<asp:Panel ID="p" runat="server" DefaultButton="myButton">
<%-- Text boxes here --%>
<asp:Button ID="myButton" runat="server" />
</asp:Panel>
if you need to do it from code, use
Me.Form.DefaultButton = Me.btn.UniqueID
Where btn is your button control.
You can use the DefaultButton property on either a server-side form control or Panel control. In your case, group the controls together in a Panel that should fire off the same button:
<asp:Panel ID="SearchBox" runat="server" DefaultButton="BtnSearch">
...
<asp:Button ID="BtnSearch" runat="server" Text="Search!" />
</asp:Panel>
....
<asp:Panel ID="UserPanel" runat="server" DefaultButton="BtnUserSubmit">
...
<asp:Button ID="BtnUserSubmit" runat="server" Text="Submit" />
</asp:Panel>
You can now use UseSubmitBehavior property to disable all the buttons you don't want to fire when hitting submit (check out the documentation for more info)
<asp:Button ID="BtnNotToFIre" runat="server" Text="Search" UseSubmitBehavior="false" />
Microsoft say:
<form id="Form1"
defaultbutton="SubmitButton"
defaultfocus="TextBox1"
runat="server">
enter link description here
$(document).ready(function(){
document.getElementById("text_box_id")
.addEventListener("keyup", function(event) {
event.preventDefault();
if (event.keyCode === 13) {
document.getElementById("button_id").click();
}
});
});

Problems with confirm on ASP.NET

This is very weird, i have an asp:ImageButton inside a list view and i want to confirm and action before i execute the method but i can't
then i created another imagebutton outside the list view and the confirm does work can u guys tell me why?
this doesn't work
<asp:ImageButton ID="btnEnabled" OnCommand="CommandExecution" OnClientClick="return confirm('Desea eliminar el cliente ?');" ImageUrl="~/App_Themes/Default/images/Habilitar.png"
Visible='<%# Eval("IsEnabled") %>' CommandName="ChangeStatus" ToolTip="Deshabilitar distribuidor"
runat="server" CommandArgument='<%# Eval("IdClient") %>' />
this one does
<asp:ImageButton ID="btnConfirmacion3" ImageUrl="~/App_Themes/Default/images/Habilitar.png" runat="server" Text="Confirmacion 3" OnClientClick="return confirm('Desea eliminar el cliente ?');" />
Set AutoPostback to false. then create two hidden textboxes for command name and params. then place a hidden linkbutton in page. in onclientclick event of your image buttons after confirm place command name and params in hidden boxes and then call postback method of link button. then handle command in link button event handler.

Setting DefaultButton to button.UniqueID throws exception

The problem I'm trying to solve:
I have several text boxes in an asp:Panel. When the user hits Enter from any of those boxes, I want the form to submit as if they've clicked btnAddTag. (When the cursor is not in those boxes, I have a different default submit button.)
The aspx:
<asp:Panel id="thePanel" runat="server">
<asp:Button ID="btnAddTag" Text="Add Tag" runat="server" />
</asp:Panel>
The vb:
tagPanel.DefaultButton = btnAddTag.UniqueID
The exception:
The DefaultButton of 'tagPanel' must
be the ID of a control of type
IButtonControl.
The value of btnAddTag.UniqueID is ctl00$phMain$btnAddTag (there's a master page, this section is called phMain).
I've also tried CType(tagPanel.FindControl("btnAddTag"), Button).UniqueID.
do:
tagPanel.DefaultButton = btnAddTag.ID
more info here:
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.panel.defaultbutton.aspx
You should set the control's ID not UniqueID:
tagPanel.DefaultButton = btnAddTag.ID

Resources