I have a page with a RadioButtonList which should post back when the selection is changed. But because of RequiredFieldValidator on the page, the postback is not happening.
It is working fine on //localhost/ but not after deployment to production server.
The error on the page is:
'event' is null or not an object
Line: 126
Char: 5
Code: 0
URI: http://web-dev:90/aspnet_client/system_web/2_0_50727/WebUIValidation.js
A few things which I have already tried:
1) aspnet_client folder is in its proper place.
2) I have added Page.Validate() and if(Page.IsValid){} statements with the button on whose click the validation should happen.
3) The ValidatorCommonOnSubmit function in WebUIValidation.js file looks like this:
function ValidatorCommonOnSubmit() {
event.returnValue = !Page_BlockSubmit;
Page_BlockSubmit = false;
return event.returnValue;}
Can somebody help me with this? Any help would be appreciated. Thanks!!
you can group the required field validator related controls so that it wont effect the radio button list.
<asp:RequiredFieldValidator ID="rfv" runat="server" ValidationGroup="button1"
ErrorMessage="*"></asp:RequiredFieldValidator>
<asp:Button ID="Button1" runat="server" Text="Button1" ValidationGroup="button1" />
As far as your radio button list is not grouped under "button1" group, it will not have any problem.
Details here and here
Related
Googled variations of the title and everything was related to a null value.
I have an issue where the return value from Page.Request.Params["__EVENTTARGET"] duplicates the unique id of the control.
ctl00$MainContent$ActivityTabset$TabNewActivity$cbxActivityCode$ctl00$MainContent$ActivityTabset$TabNewActivity$cbxActivityCode
cbxActivityCode.UniqueID returns
ctl00$MainContent$ActivityTabset$TabNewActivity$cbxActivityCode
The following is the code that fails in the comparison. It is in the Page_Load event, and is the only code to execute right now if it is a postback.
string controlName = Page.Request.Params["__EVENTTARGET"];
if (cbxActivityCode.UniqueID == controlName)
{
ConfigureActivityUnits();
}
Here is the definition of the control
<obout:ComboBox ID="cbxActivityCode" runat="server"
DataSourceID="ObjectDataSourceDAOActivity"
FilterType="StartsWith" EmptyText="Select..."
AutoPostBack="true"
OnSelectedIndexChanged="cbxActivityCode_SelectedIndexChanged"
AllowCustomText="false" AutoValidate="true" DataValueField="Id"
DataTextField="Description" EnableViewState="true"
OpenOnFocus="true" MenuWidth="425" AllowEdit="False"
Width="300px">
</obout:ComboBox>
I am new to ASP.net and am wondering if it is possible one of the control attributes is causing this behavior?
Is it possibly a bug with the control?
Are there hooks in ASP.net where someone can manipulate a value which will effect Page.Request.Params["__EVENTTARGET"]? (This is a big messy legacy system, and I have no prior developers as a resource.)
If not any of the above, anyone have any ideas as to what could be causing this?
I have a role called 'member' and another 'admin' in Asp.Net website.
I did before, that button should be visible or not and i am successful in that,but,i am not able to get the proper code(aspx.cs) to disable the button so that it may be in view but not at all accessible.
<asp:Button ID="Button4" runat="server" PostBackUrl="~/report.aspx"
Text="print in report format" Width="173px"
Enabled='<%# HttpContext.Current.User.IsInRole("Admin") %>' />
i want that whenever a member login then button "report" should be disabled for him.
You have to set the Button.Enabled property value to according to the HttpContext.Current.User.IsInRole("admin") function returned value.
Either in html:
<Button ... Enabled='<%# HttpContext.Current.User.IsInRole("Admin") %>' ... >
Or in code behind:
Button.Enabled = HttpContext.Current.User.IsInRole("Admin");
if (HttpContext.Current.User.IsInRole("member"))
{
//enable/disable here
}
In the Page_Load after checking for the role you may be able to set the IsEnabled for the Button to be False.
e.g. buttonLogin.Enabled = (IsUserInRole(Admin));
Either I'm missing something or the solution is simply:
button.Enabled = false;
I'm assuming you are using an ASP.NET button control - if you are then you need to set the Visible and Enabled button properties to false
The primary problem you have here is the hash mark: <%# is used to identify a binding. Unless you're calling this in a gridview or a formview or something, this will not work. I would recommend setting it in the code behind as suggested by #Muhammad Akhtar, but if you're hell bent for leather on using the html side it should probably be:
Enabled='<%= HttpContext.Current.User.IsInRole("Admin").ToString() %>'
i m working on simple asp.net and in that i am using validators.
my situation is like that i have used reaquired field validator its working fine.
and after that if i ented data and fired insert query then data is inserted and sucessful message is displyed on the lable. but agin if i clik on submit button with empty fields then validator works but the lable of successful message does not disapper. how to hide that lable.
You need to use javascript to hide the success message, here is a sample
<script type="text/javascript">
function hide() {
document.getElementById('<%=lblSuccess.ClientID %>').style.display = 'none';
return false;
}
</script>
<asp:Label ID="lblSuccess" runat="server" Text="Success"></asp:Label>
..your form code
<asp:Button ID="btnOk" runat="server" Text="OK" OnClientClick="hide()" ValidationGroup="ValidateForm" />
Why javascript, the form doesn't get posted because validators don't let the form to be posted if the conditions aren't met, so you are left to hide the message dynamically with javascript
<script type="text/javascript">
function Hide() {
document.getElementById("Lable1").style.display = 'none';
return false;
}
</script>
<asp:Button ID="Button1" OnClientClick="Hide()" runat="server" onclick="Button1_Click" Text="Button"/>
and use
if (Page.IsValid){}
on clik event.
Show us some code of what you're up to and we can tell you more precisely where you are going wrong. In a nutshell though the visibility of that message is going to be persisted through a postback so you have to explicitly tell it to not be visible if validation has failed.
Set the label to visable=false and on save set the text value if required and change visible =true ?
On form load, do something like this:
TheValidMessageLabel.Visible = Page.IsValid;
You are probably just setting the visible state to true when it's valid and never setting it to false again.
Set your success label visibility in page load to false.
And only if operation is successfully set that label visibility to true.
cheers
I have some code where I need two separate required field validators for one control, both in separate validation groups which are then validated by two separate buttons.
This approach works well when the buttons are clicked but both validators show if I enter a value in the textbox and then remove it.
Is there a way to turn this"lost focus" validation off? I only need it to validate when the buttons are clicked.
EDIT
Unfortunately, if I set EnableClientScript=false then I dont have any client notifications. What I want is for the dynamic error message to show (effectivly in the OnClientClick event of the button) but not the "lost focus" of the textbox.
Is there some way I can disable or "unhook" the lostfocus client event?
EDIT
A combination dDejan's answer and womp's answeer here sorted the problem perfectly.
My final code looks like this (for anyone else with a similar situation)...
Javascript...
<script type="text/javascript">
$(document).ready(function() {
$('body').fadeIn(500);
//Turn off all validation = its switched on dynamically
$.each(Page_Validators, function(index, validator) {
ValidatorEnable(validator, false);
});
});
function ToggleValidators(GroupName) {
$.each(Page_Validators, function(index, validator) {
if (validator.validationGroup == GroupName) {
ValidatorEnable(validator, true);
} else {
ValidatorEnable(validator, false);
}
});
}
</script>
ASPX Control Example...
<telerik:RadTextBox Width="196px" ID="txtFirstName" runat="server" MaxLength="50" Skin="Black"></telerik:RadTextBox>
<asp:RequiredFieldValidator ID="valFirstName" CssClass="Validator" runat="server" EnableClientScript="true" Display="Dynamic" ErrorMessage="You must enter your first name." ControlToValidate="txtFirstName" ValidationGroup="NeededForEmail"></asp:RequiredFieldValidator>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" CssClass="Validator" runat="server" EnableClientScript="true" Display="Dynamic" ErrorMessage="You must enter your first name." ControlToValidate="txtFirstName" ValidationGroup="NeededForSubmit"></asp:RequiredFieldValidator>
ASPX Button Code...
<asp:Button ID="btnGetConfCode" runat="server" Text="Get Confirmation Code" OnClientClick="ToggleValidators('NeededForEmail')" OnClick="btnGetConfCode_Click" Width="100%" ValidationGroup="NeededForEmail"/>
<asp:Button ID="btnRegisterUser" runat="server" Text="Register" OnClientClick="ToggleValidators('NeededForSubmit')" OnClick="btnRegisterUser_Click" Width="100px" ValidationGroup="NeededForSubmit" />
So, now there is no validation until a user clicks either the "Get Email Confirmation Code" button or the "Register" button.
If they click the "Get Email Confirmation Code" button all of the controls validate apart from the textbox where the user is to input the email validation code and we only see one validator message.
If they click the "Register" Button then all of the controls validate and we only see one validation message.
If either button is pressed, the user goes back, adds and then removes some text then we only see one validator. Before this change you used to see both messages saying the same thing.
Thank you for help guys
You can set if the validators are "active" or not with client side code using the ValidatorEnable function. Basically it goes like this
var validator = document.getElementById('<%=Validator1.ClientID%>');
ValidatorEnable(validator , state); //where state is boolean
You can also trigger the validator to validate on some event (like for example the click of the buttons) using the ValidatorValidate(validator) function.
I am not sure which would work better for you (enabling/disabling the validators or custom triggering of the validation) but I suggest this article that will guide you in the right direction
ASP.NET Validation in Depth
There's no way to unhook them if EnableClientScript=true.
What you could do is set it to false. Then create a javascript validation method that is called on your submit-button onClientClick event.
In your method, you would have to call ValidatorValidate(control) for each control you want to validate client side
There's an example here:
http://msdn.microsoft.com/en-us/library/Aa479045#aspplusvalid_clientside
You could turn off the javascript validation by setting EnableClientScript="false" that would get rid of the lost focus validation.
You can use Custom Validator controls instead and either validate the input using Javascript on the client or within the event handler on the server. Ensure you set ValidateEmptyText="true" on the validation controls otherwise the events will not fire on an empty field.
Try to Enable on Both button click using javascript and disable it on textbox blur event.
Try resetting the onchange event for the input-control.
$(document).ready(function () {
$("#controlid").each(function () { this.onchange = null; })
});
var validator = document.getElementById('<%=Validator1.ClientID%>');
ValidatorEnable(validator , state);
It is working in javascript but when we use the page.Isvalid function on Server side it creates the problem to check page is valid or not.
simply type this code in page_load event
textboxname.Attributes.Add("onblur","ValidatorOnChange(event);");
I have an ASP.NET application where there's a few ASP.NET buttons and several plain HTML buttons. Anytime there's a textbox where a user hits enter, the ASP.NET button tries to submit the form.
I know I can change the defaultButton, but I don't want there to be any default button. I just want it so when the user presses enter it doesn't do anything.
I've tried setting defaultButton to blank, but that doesn't seem to work. How do I prevent the form from being submitted by the ASP.NET button when enter is pressed?
You can set the button's UseSubmitBehavior = false
btnCategory.UseSubmitBehavior = false;
Here is what I used to fix this problem.
<form runat="server" defaultbutton="DoNothing">
<asp:Button ID="DoNothing" runat="server" Enabled="false" style="display: none;" />
I also had this problem with an ImageButton on my MasterPage (the Logout button) being set as the default button (so whenever someone pressed Enter, it would log them out). I solved it by using the following line in the Page_Load event on every child page, which is a bit of a work-around, but it works:
Form.DefaultButton = cmdSubmit.UniqueID;
Hope this helps someone else.
<asp:TextBox ID="TXT_Quality" runat="server" Width="257px"
onkeypress="return key_Pressed(event, this);">
</asp:TextBox>
function key_Pressed(e, textarea)
{
var code = (e.keyCode ? e.keyCode : e.which);
if(code == 13)
{
return false;
}
return true;
}