I have an anchor link like:<a id="linkOwner" runat="server"></a>
In my codebehind I am disabling it based on some condittions like:linkOwner.Disabled = true; But still the link is click-able.How to fix it?
If you use an ASP LinkButton control I think you can just disable it on the server side and it'll properly disable it on the client. Not positive on that though. Another method is to use javascript. In the past I have used jQuery to add a click event to the disabled anchor with a empty event that returns false. Something like:
function disabler(){ return false; }
$('#linkOwner').click(disabler);
//to reactive the link
$('#linkOwner').unbind('click', disabler);
The return false lets jQuery know not to bubble up the event.
There are two solutions:
Change the anchor tag to an <asp:HyperLink> then you can set the Enabled property as you see fit.
You need to add an attribute to the control as in
linkOwner.Attributes["disabled"] = "disabled";
Disable anchor button by calling javascript void function and call another doAction function which will hanle your condition.
HTML implementation:
some text
Javascript implementation:
function doAction() {
if ( condition here ) {
// do X
} else {
// do Y action
}
}
Related
I have an asp.net webform with a few Panels that each have several textboxes inside of it. I am currently hiding or displaying the panel using jQuery based on which item in a DropDownList is selected.
I have run into an issue where the required field validator is still firing even when the elements it is attached to is not showing because it's parent panel has display: none.
Is there any way to disable the RequiredFieldValidator when the element it is attached to is not showing because of CSS?
I know that if set Visible=false on the server side the elements wouldn't render at all, but I would prefer to keep the show/hide logic on the client side for user experience reasons.
I agree that a custom validator would be best, but if you do need to do it on the client side, you can use the ValidatorEnable function.
ValidatorEnable(document.getElementById("<%= RequiredFieldValidator.ClientID %>"), false);
(Note that I have never actually tried this myself, but I have heard of it successfully being used to disable validators on the client side.)
As this thread says, you can use that function. So modify your method like below
function cbSearchOption_SelectedIndexChanged(sender, args) {
var x = document.getElementById('cbSearchOption').value
if (x == 'Date')
{
ValidatorEnable($get(‘<%=DateValidator1.ClientID %>’), true);
document.getElementById('test').style.visibility = 'visible';
}
else
{
ValidatorEnable($get(‘<%=DateValidator1.ClientID %>’), false);
document.getElementById('test').style.visibility = 'hidden';
}
}
I have a page where user controls are loaded dynamically into a div/panel. I don't know anything about the content of these user controls, besides they have a button.
When the user is inside this div / panel (using the user control), I want the button inside the div/panel, to be the default button.
Right now, my sidewide search button is the default button no matter what.
I can't add "defaultButton=mybutton" in the user controls, and I can't add a class to the buttons.
How do I solve the problem?
Thanks :)
I ended up doing the following:
$("#CalculatorDiv").keypress(function (e) {
if ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13)) {
var div = $("#CalculatorDiv");
var firstInput = div.find("input[type=submit]");
firstInput.click();
return false;
} else {
return true;
}
});
:-) So a combination of find and keypress event.
You have to be more specific. Better yet, post the code.
By default button, do you mean pressing "Return" will make the button submit? In that case, you can enclose it in a form.
To access the button, you can do that via javascript's document.getElementById
https://developer.mozilla.org/en-US/docs/DOM/document.getElementById
You can also use jQuery to access the element by class, type or nearest button element to the div tag. Maybe use the "find" method in jQuery:
http://api.jquery.com/find/
I'm not certain how you are accomplishing this based upon your description but what you could do is set the Control.ID and then you can use FindControl to locate the UserControl.
UserControl uc = (UserControl)PANELID.FindControl(Control.ID);
I'm not sure, but can't you search for <input type="button" .../> and than add the default code part to it?
EDIT: read your question wrong, asumed it was a webpage 'cuz of the 'div'.
Normally in C#, you can select the Controls inside a panel and then search on the type to find the button
$("your div").children().blur(function(){
$(#button).focus();
});
With ASP.NET, how do I prompt the user for a yes/no question and getting the result back to my .ascx?
So far I can open a confirmation dialog with use of Javascript, but I can't return the value. But I don't know if this is the right approach.
You can use standart JavaScript confirm() function to show popup and do Post Back in case of Yes or No. For example:
if (confirm('Question')) {
__doPostBack('', 'Yes_clicked');
} else {
__doPostBack('', 'No_clicked')
}
Then on server in Page_Load() method do:
if (IsPostBack)
{
var result = Request.Params["__EVENTARGUMENT"];
}
You can also do it async by specifying the first parameter of __doPostBack() function as ID of any update panel.
This is not a good practice to do this. you can get your confirm using javascript and postback or callback result to server.
but if you want to do this, this will help you :
A Simple ASP.NET Server Control: Message Box & Confirmation Box
add this in head of source
function confirm_Edit()
{
if (confirm("Are you sure want to Edit?")==true)
return true;
else
return false;
}
call it like this
If you insist on using webforms, another solution could be the AJAX Control kit. Simply create a ModalPopup and have you confirm buttons inside that.
Read more here:
http://www.asp.net/ajax/ajaxcontroltoolkit/Samples/ModalPopup/ModalPopup.aspx
You need to use ajax, or to make a postback to the server.
Your c# code is server side and the javascript is client side.
If you use the ajax extensions for asp .net you can use javascript page methods:
PageMethods.YourMethod(confirm('your text'), OnSuccess, OnFailure);
I use this. As far as I know it prevents the rest of the button event from executing.
btnMyButton.Attributes.Add("onClick", "return confirm('Are you really sure?')");
Another option is to show yes/no:
<script>
function AlertFunction() {
if (confirm('Are you sure you want to save this thing into the database?')) {
$('#ConfirmMessageResponse').val('Yes');
} else {
$('#ConfirmMessageResponse').val('No');
}
}
</script>
to handle it from .net side:
string confirmValue = ConfirmMessageResponse.Value;
if (confirmValue == "Yes")
{...}
string confirmValue = ConfirmMessageResponse.Value;
showing error in this line when using in .net side
When the Page_Load event gets called to load the page, how would I have the page load already scrolled to a particular id. I cannot use a named anchor # because it breaks my css.
I can use javascript's scrollto function but i don't know how to call javascript from the asp.net page_load event.
I have several headers, and a menu that sorts the contents within each header. So each header has a sort menu. When I sort say Header 2 by "Oldest" I reload the page and change the orderby stuff in my sql and repopulate the repeater under each header. On load I would like the page to load already scrolled to Header 2. I would normally just use #Header2 on the redirect, but I can't use the this anchor method because it breaks my code.
Is there any neat method to accomplish this?
The css issue has to do with full length columns and having a border in the gutter. I know I can use images, but the nature of my pages would require the css to be dynamic to account for the different widths and apply the appropriate background image.
I ended up setting the <body> tag to runat="server" and assigning an id="body". I then added this code in the Page_Load event:
if (Request.QueryString["view"] != null && Request.QueryString["view"] != "")
{
body.Attributes["onload"] = "scroll('" + Request.QueryString["view"] + "');";
}
else //Probably redundant, but I have partial postbacks
{
body.Attributes.Remove("onload");
}
It calls this JS function:
function scroll(id)
{
var a = document.getElementById(id);
if (a != null) {
window.scrollTo(0, a.offsetTop);
}
}
So instead of using #header2, I use a querystring of view=header2 because I have to do a postback anyway. If I just want to go to a named anchor I use onclick="scroll('header2')" in the link.
i have an AJAXIFIED button(btnsend) thas is disable by it's Property -> Enabled="False"
i have a TextBox Next To This Button And I Want Enable that Button When Users Type Something in That TextBox...
so i did this (JavaScript):
function onkeyupontextbox() {
var btnSend = document.getElementById("btnSend");
btnSend.disabled = false;
}
but that button does not work after becomes enable...
what can i do about that?
(i am using radajaxmanager for ajaxify that button)
(when i remove that button from RadAjaxmanager Or UpdatePanel So EveryThing Is Ok , But I Want That Button In Ajaxify Mode)
thanks for your attention...
Sounds like you're trying to mix Ajaxified properties and DOM element properties.
Leave the property Enabled = "True" when you ajaxify it, then use JS on page load to btnSend.disabled = true it. If you use pure js to disable it the function you have above should work fine to re-enable it. For example, if the ajaxify property 'Enabled' is set to true, then place the following javascript into your page:
window.onload = function(){
document.getElementById("btnSend").disabled = true;
};
Then use the function you wrote above to enable it onkeyupontextbox(). Because javascript is disabling the button, it should be able to re-enable it. Before, you were disabling with the Ajaxified property and trying to re-enable with js.
Could following be the answer? (Dont have experiences with RadAjaxmanager)
function EnableBtnSend()
{
$find("<%=btnSend.ClientID %>").ajaxRequest("");
}
Found here: http://www.telerik.com/help/aspnet-ajax/grdenabledconventions.html