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
Related
I have the following code:
<form id="frmLogOn" runat="server" DefaultButton="testbutton">
<asp:Panel runat="server" ID="testpanel" DefaultButton="testbutton">
<asp:TextBox runat="server"></asp:TextBox>
<asp:Button runat="server" ID="testbutton" OnClick="dotest" UseSubmitBehavior="True"/>
</asp:Panel>
</form>
If I click the button, the page does a postback and calls dotest(), as expected.
If I press the enter key while focused on the textbox nothing happens.
I have investigated, and found that WebForm_FireDefaultButton(event, target) is being called when I type a different key (for example a letter or a number) but it is NOT being called when I press the enter key.
(Note I found it is called twice, and this is probably because I set the same DefaultButton for both the form and the panel).
Why is it not being called?
After some searching I determined this was caused by the following in my master page:
<body onkeydown = "return (event.keyCode!=13)" >
Works fine without it.
Consider the following ASPX:
<asp:ValidationSummary ID="vSummary" runat="server" CssClass="error-message" HeaderText="<p>Please correct the following errors with your profile.</p>" ShowMessageBox="false" DisplayMode="BulletList" ShowSummary="true" />
<asp:TextBox ID="txtTest" runat="server" ClientIDMode="Static"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfv" runat="server" Display="None" ErrorMessage="Enter a value in the textbox." ControlToValidate="txtTest"></asp:RequiredFieldValidator>
<asp:Button ID="btn1" runat="server" Text="Button 1" OnClick="btn1Click" ClientIDMode="Static" />
With text entered into the textbox, everything behaves as expected.
Clicking the button submits the form / pressing 'Enter' while inside the textbox submits the form.
However, when the textbox is empty, pressing 'enter' does nothing. Clicking the button will display the validation summary, as expected, but pressing 'enter' does not.
How can I make the validation summary display after the 'Enter' key is pressed inside an empty textbox?
For some reason when you hit enter error does not being displayed by ValidationSummary control. However, if you change in RequiredFieldValidator Display to "Dynamic" you will see error message.
Update: to overcome this problem try to follow instructions from this article.
For lazy ones:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
// Add new attribute to form1 event to fire when enter key submit button click event
form1.Attributes.Add("onkeydown", "javascript: return WebForm_FireDefaultButton (event, '" + ButtonName.ClientID + "')");
// Allow you to use enter key for sumbit data
ClientScript.RegisterHiddenField("__EVENTTARGET", "ButtonName");
}
}
And finally:
// Add this code to div section on the form1 ASPX page.
<div id="inputArea" onkeypress="javascript:return WebForm_FireDefaultButton(event,'ButtonName')">
That behaviour is because your textbox is being validated but the validation message is not showing because you turned it off with Display = "none". It's doing the validation on client side and therefore, postback did not occur because of empty text.
If you remove the Validation control (which is not what you want) from the page, your text box will start posting back. I guess you don't want the message to show for each control, just in the validation summary, otherwise, that's the easiest way
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"
}
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
I have usercontrol which has required field validation in it. I have 2 instances of this usercontrol in single web page . Now if I click button on first usercontrol the validation messages are fired for 2 user controls. But only the required fields of that user control has to be fired. Please could any one solve this problem.
Thanks
Madhavi
If you have not set the ValidationGroup property of the RequiredFieldValidator within your usercontrol then field validation will fire whenever the form is submitted regardless of which button caused the postback.
If you wish to associated specific validators with specific submit buttons then you will have to associate them to the same ValdiationGroup.
If you are setting the ValidatioGroup within your user control but finding that the validation is firing for all instances of the control then you will need to take some attribute of the user control instance and incorporate it into the ValidationGroup to ensure the user control is exclusively validated for any submit button on the control.
Here is an example:
<asp:TextBox ID="txtTest" runat="server"/>
<asp:RequiredFieldValidator ID="txtTestReqVal"
runat="server"
Display="Dynamic"
ControlToValidate="txtTest"
Text="* Field is required"
ValidationGroup="valGroup<%= ClientId %>"
CssClass="modelError"
/>
<asp:Button ID="btnSubmit" runat="server"
Text="Submit"
CausesValidation="true"
ValidationGroup="valGroup<%= ClientId %>"
/>
The trick here is the <%= ClientId %> part. This will inject the instances client side unique id into the validation group value. This means that all validation for these controls will be grouped together by the single unique instance of the user control. This way you can have multiple instances of the same user control present on the same page but all uniquely validated.
make your validation groups runat="server"
Then give each one a unique validation group like this:
string validationGroup = Guid.NewGuid().ToString();
txtContactNameValidator.ValidationGroup = validationGroup;
txtContactNumberValidator.ValidationGroup = validationGroup;
btnSave.ValidationGroup = validationGroup;
This will isolate the user controls from each other no matter how many are on the page.
I think you need to specify a validatongroup attribute on every field validation control.
Each user control would have its own validation group defined.
See here
One way to solve this is to expose a public property on your usercontrol which you can pass in the validation group name.
<uc:mycontrol id=u1 validationgroup="valA" .. />
<uc:mycontrol id=u2 validationgroup="valB" .. />
One problem with this approach is that you will need to add the validation group to every validation control within page load on the usercontrol.
I think the validation group will not work.. As it is the same user control but dropped 2 times in the page. Any of the button will fire validation on both user control wh
function ValidateRadio(button)
{
var radioOptions = document.getElementById(button.id.split("_")[0] +'_rblPollOptions');
var RVPollOptions = document.getElementById(button.id.split("_")[0] +'_RVPollOptions');
var options = radioOptions.getElementsByTagName("input");
var radioSelected=false;
for (j=0; j < options.length; j++)
{
if(options[j].checked)
{
radioSelected=true;
break; // Found it, proceed to next question
}
}
if (!radioSelected) // no option selected
{ // warn user, focus question //alert("You did not answer question");
RVPollOptions.style.visibility = "visible";
return false;
}
}
</script>
<asp:RequiredFieldValidator ID="RVPollOptions" runat="server" ControlToValidate="rblPollOptions"
ErrorMessage="Select option!"> </asp:RequiredFieldValidator>
<asp:Button ID="btnPoll" Text="Vote" OnClientClick="javascript:return ValidateRadio(this)" runat="server" OnClick="btnPoll_Click" />