disable aspxdateedit control in javascript - devexpress

iam using aspx date edit control in my asp.net and c#.net application.
is it possible to disable aspxdateedit control in javascript?if so how acan that be done.becoz ,i tried
using
"document.get elementbyid("datedit").disabled=true;"
but it didn't worked out !!
any suggestion on this?

This can be done using the editor's client side SetEnabled method. I.e.
// JS
dateEdit.SetEnabled(false);
Note, here the dateEdit is the editor's ClientInstanceName property value. It allows you to define the name of the java script client side object. Using it, you will be able to access its client side properties and methods.

First of all set the control's ClientInstanceName property to access the control at client side.
<dx:ASPxButton ID="btnSubmit" runat="server" Text="Save" AutoPostBack="false"
Width="120px" **ClientInstanceName="btnSave"**><ClientSideEvents Click="OnButtonClientClick" />
</dx:ASPxButton>
Now you can do this in two ways as:
ClientSideEvents Click="function(s, e) {document.getElementById('btnSave').enabled = false;}"
Have can try this also:
ClientSideEvents Click="function(s, e) {btnSave.SetEnabled(false);}"
you can also access these controls in javascript code at aspx page either in a callback event or some other client side method.
just access the control with their associated client instance name as i have btnSave for my ASPxButton
btnSave.SetEnabled(false);
get more information about these control's methods and client events etc. see the clientscript namespace of these aspxEditors DevExpress.Web.ASPxEditors.Scripts Namespace
hope you will get your solution or help to solve your problem here..
Happy Coding..

Related

how to check a particular asp.net validation control is valid?

In a web form there are different asp.net validation controls. Is it possible to check a particular validation control is valid ? For example on leaving focus of textbox, first I will check requiredFieldValidatorUserName is valid ? If it is valid then I will check on server using ajax that this user name is not booked already.
Edit:
Explaination: I want to check validity (that input was valid) of a validation control on client side.
Please guide.
All validator controls implement IValidator which contains the IsValid property.
myValidatorControl.IsValid
The best way would be to use a CustomValidator with client side code, as this will display all the error messages, block form submission and also ensure that the validation is repeated at the server side - remember, just because you have client-side validation available, doesn't mean the user's seen it: Always validate your input at the server as well.
Your CustomValidator would then be coded to call the Ajax methods, and would show the error messages correctly to the client:
<asp:Label ID="UserNameLabel" AssociatedControlID="UserName" runat="server">
UserName *:</asp:Label>
<asp:TextBox ID="UserName" runat="server" />
<asp:RequiredFieldValidator ID="UserNameRequired" runat="server"
ControlToValidate="UserName" EnableClientScript="true"
ErrorMessage="You must supply a username!" />
<asp:CustomValidator ID="UserNameCustom" runat="server"
ControlToValidate="UserName"
ClientValidationFunction="CheckExisting"
OnServerValidate="UserNameCustomValidate"
ErrorMessage="Username already taken" />
And your ClientValidationFunction should look something like:
<script type="text/javascript">
function CheckExisting(source, arguments) {
// Pass the arguments.Value to your AJAX call:
if (ajaxCallUserNameTaken(arguments.Value)) {
arguments.IsValid = false;
}
}
</script>
(Obviously, you'll need to write the ajaxCallUserNameTaken method to call your page method/web service/etc.)
Doing it this way will ensure that the validation methods happen as expected; this will get called whenever the user tabs out of the textbox leaving a value (it won't get called if the textbox is empty), and will ensure that the user can't submit the page until they supply a unique value. You'll also want to create the method referenced in OnServerValidate to ensure that the value's good once it hits the server too - this should call the same code that the AJAX endpoint uses to reduce duplication of code, etc.
I was originally going to suggest that you could use the Page_Validators object on the client-side to do some checking in the onBlur event, but I don't really think this is suitable here as it results in more pain:
It assumes that although there might be more than one validator on the page, there's only the RequiredFieldValidator on the control we're checking
The RequiredFieldValidator isn't fired during OnBlur if a user moves out of a control without setting a value - only if they set and clear the value, so even if isvalid is true, you need to check for an empty string!
You could do this by setting the ValidationGroup for the Validator control that you want to treat as separate from the others. Make sure it matches the ValidationGroup of the control it's validating (your username field).
I have just faced the same issue and I Set CausesValidation="true" to the textbox control and it worked. Just give it a try :)
I have been messing around with this around for a bit and found a rather easy (not so efficient) solution to handle this using jQuery.
Use this function to check the validity of your control:
function validateControl() {
return $('#YOUR_VALIDATOR_ID').css("visibility") == "visible"
if you're using Display="Dynamic" on your validator then the function is like so:
function validateControl() {
return return $('#YOUR_VALIDATOR_ID').css("display") == "inline"
Be sure to check the true ID of your validator if you're using a Masterpage, as it will be different than the one in your IDE. Do so by viewing the page source in your browser.
The best solution will be of course to validate your form in some other way, using JavaScript or a CustomValidator that lets you write your own code.

When isn't runat="server" used in asp.net?

When don't you need to use runat="server" in ASP.NET?
EDIT: Thanks for all the answers, but I was really thinking of runat="server" inside an <asp: tag.
Use the runat=server attribute when you're using ASP.NET controls, and/or you require programmatic access to those controls in your code-behind.
HTML controls don't require this attribute. It's useful if you have any HTML element like <span> <div>, or <table> when you want the ability to access them in code-behind.
<asp:Label runat="server" id="foo" />
<div runat="server" id="bar />
...
foo.Text = "Hello Label";
foo.Attributes["class"] = "baz";
You need to use runat="server" on any control that you want to be parsed as a server control.
Any element with runat="server" will be parsed into a server control in the Page herarchy. Anything else will be handled as plain text, and put in LiteralControl controls in the Page hierarchy.
The exception is elements that aren't real elements, but special tags within another server tag, for example ContentTemplate tags. They don't need a runat="server" because the containing control will parse them.
When you don't want the server side ASP.NET to render a server side variable against us.
Generally speaking you don't use it when you don't need to manipulate the DOM element at the server side e.g. which are only used for layout purposes.
Without runat="server" there would also be no other way to make html controls server side controls. It does look like an odd thing, because you can't do runat="client".
So in summation you can't leave it out on any ASP .Net controls ever and it was probably the easiets and cleanest way to find all server side controls for the developers who created ASP .Net Web forms.
source: http://mikeschinkel.com/blog/whyrunatserverforaspnetpart2/
Tag runat="server" indicates that the code contained within the script block will run on the server (and not on the client). On execution, ASP.NET will create server-side objects that contain this code as well as an instance of the Page class to contain the controls defined inside the page as instances of their given type (System.Web.UI.WebControls.Textbox, for example). This server-side object will be invoked on user request and will execute code in response to events.
Create Control in Runtime
I need one label in runtime that time don't need runat="Server" is not required
Example
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Label lblNew = new Label();
lblNew.ID ="lblnew";
lblNew.Text ="Test";
}
}
this code create label in runtime at page load event

Question about the "runat" attribute in ASP.NET?

I went through this thread but couldn't understand much. I am very new to ASP/HTML/Server-side programming.
I tried running this code on a .aspx file:
<form id="form1" action="Default.aspx">
<div>
<asp:Label ID="lblName"></asp:Label>
</div>
</form>
And I got an error when I tried to use this in the CodeFile:
protected void Page_Load(object sender, EventArgs e)
{
lblName.Text = "123";
}
"lblName does not exist".
But if I use runat="server" attribute with the label, then this code works.
Also, is there any concept of nesting the runat attribute. e.g, If I specify runat=server for the form above, will all my controls inside the form automatically be configured to run at server? How does this attribute work?
In which case would I be required to specify runat=server for the and for the tag? How does the server-side know that the label is inside the form if I don't have a form object at server side? Or am I missing something?
Any element marked with runat="server" lets the framework know that this will be a control on the server side. This article has more details:
Exploring the Runat Attribute
Nope, there is no such nesting available in ASP.NET, you have to specify "runat" to every control that you want to use in code behind and that is part of ASP.NET web library.
Because ASP.NET can only recognize difference between client side tag (the html that runs on browser) and server side tag with help of "runat"

How to fill a Label.Text - Property via jQuery

I use ASP.NET and have a label control on my page, which I fill with
the jQuery-Command
$('#<%= myLabel.ClientID %>').html(content);
.val() does not seem to work with this.
Somehow, I have Problems getting the content in code-behind. In the code, the myLabel.Text-Property is still empty.
If you want to display the value on the client and have it available on the page, you need an input that'll get sent to the code-behind when you POST like this:
$('#<%= myLabel.ClientID %>').html(content);
$('#<%= myInput.ClientID %>').val(content);
<asp:Label Id="myLabel" runat="server" />
<asp:HiddenField ID="myInput" runat="server" />
In the code-behind:
myInput.Value
I think your problem is that labels (rendered as span tags) are inherently read-only in the asp.net world. They're not meant to be used as 'input' controls, and as such changes to their HTML on the client-side are ignored on the server-side, where values are set based on ViewState.
To do what you are asking, you'd have to notify the server of the change as well, such as by using AJAX. The only issue here is ajax webmethods in your code behind are static, and because of this can't access the page's control set to change the .Text value.
In the end the easiest option is to make use of hidden fields as Nick said. These are technically 'input' controls and their values changed on the client-side are sent to the server as you desire. You'd just have to keep the label/span and hidden field/input synchronized on the client.
Hope this helps.

ASP.NET invoke ASP.NET buttons server event in javascript

I am having an ASP.NET page with one Asp.net button control and a normal html link (anchor tage) I want to invoke the postbackl event of asp.net button control when someone clicks on the link.
I used the below code
<a href="javascript:myFunction();" class="checkout" ></a>
<asp:Button ID="btnCheckout" runat="server" Visible="false"
onclick="btnCheckout_Click" />
and in my javascript i have
function myFunction()
{
var strname;
strname = "Test";
__doPostBack('btnCheckout','OnClick');
}
But when runnin gthis , i am getting an error like __doPostBack is undefined
Can any one tell me why it is ?
Thanks in advance
This anyway wouldn't have worked. When you make your .NET control invisible by using 'Visible="false"' it isn't rendered, that means not available at the client.
Back to your question.
1- Where is myFunction defined? Between the tag?
2- Are there more .NET controls on the page? If there aren't any other .NET controls, .NET doesn't add all the scripts that are required for postbacks and stuff.
Why not do the following (based on TheVillageIdiot answer):
<asp:LinkButton ID="lbtnCheckout" runat="server" CausesValidation="false" OnClick="lbtnCheckout_Click" CssClass="checkout" />
With the above example you don't need the fake button and make it invisble. You still can do your postback. Way more cleaner approach I would say.
First of all I tried your code and also not get anything like __doPostBack, then I added another button on the page which was visible but it was all the same. Then I added a LinkButton and got __doPostBack method. You can do post back from javascript but then EventValidation is problem, as it does not allow this kind of thing. I had to use the following to overcome it and it worked:
protected override void Render(HtmlTextWriter writer)
{
ClientScript.RegisterForEventValidation(
new PostBackOptions(btnCheckout, "OnClick"));
base.Render(writer);
}
I think I'm bit incoherent in answering so I'll mark it as wiki :)

Resources