There is an apparent bug in asp.net that when you have a checkbox inside a panel that is acting as a modal popup with the modalpopupextender, that when you try to check or uncheck it, it doesn't work!
I found the following thread, and it seems that quite a few people have had this very same problem.
From reading many posts, it seems that this happens if you do not set the TargetControlID property of the extender correctly.
However, this becomes an issue if you have multiple links, all that show the same extender. To get round this, you can simply create a fake button:
Showing posts with label AJAX. Show all posts
Showing posts with label AJAX. Show all posts
Tuesday, 27 April 2010
asp.net button not posting back
I have a modalpopup extender attached to a panel, inside which resides two asp.net buttons. Each of these had the normal "Onclick" attribute assigned to them. However, when they were clicked, no postback was occuring.
From some reading around, it appears that this is by design, and that the buttons are there for this to be closed.
So how do you get round this?
You can attach a javscript client side event that mimmicks the behaviour of what happens with asp anyway, by forcing the postback to take place.
Firstly, in the head of your page, define a function that ensures that the page is valid first, and then causes the postback to take place (the validation part is crucial for this to work.
function doPostback(sender,e)
{
if(Page_IsValid)
{
__doPostback(sender,e);
}
else
{
return false;
}
}
Now in your code behind, tell your buttons to call this:
btnSaveUserDetails.OnClientClick = "javascript:return doPostback('" + btnSaveUserDetails.UniqueID + "');";
It is important here that the uniqueID is used, as this ensures that any controls that are nested pass the right details to the postback function.
And ta da, they now work!
From some reading around, it appears that this is by design, and that the buttons are there for this to be closed.
So how do you get round this?
You can attach a javscript client side event that mimmicks the behaviour of what happens with asp anyway, by forcing the postback to take place.
Firstly, in the head of your page, define a function that ensures that the page is valid first, and then causes the postback to take place (the validation part is crucial for this to work.
function doPostback(sender,e)
{
if(Page_IsValid)
{
__doPostback(sender,e);
}
else
{
return false;
}
}
Now in your code behind, tell your buttons to call this:
btnSaveUserDetails.OnClientClick = "javascript:return doPostback('" + btnSaveUserDetails.UniqueID + "');";
It is important here that the uniqueID is used, as this ensures that any controls that are nested pass the right details to the postback function.
And ta da, they now work!
Monday, 26 April 2010
modalpopupextender event fires but does not show
I lost half a day of my life to this silly mistake today.
I had on my page a panel, with a modal popupextender. When I clicked a link that ran a serverside mdlPopup.show() or a client side $('mdlPopup').show(), the events fired, but no modal popup showed.
The reason for this was because I had set the "visible" attribute of my panel to "false", rather than setting the style of it to "display:none"!
Silly....
I had on my page a panel, with a modal popupextender. When I clicked a link that ran a serverside mdlPopup.show() or a client side $('mdlPopup').show(), the events fired, but no modal popup showed.
The reason for this was because I had set the "visible" attribute of my panel to "false", rather than setting the style of it to "display:none"!
Silly....
Friday, 16 October 2009
System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.FormatException: Invalid character
I started receiving these errors a couple of weeks ago, very randomly, and always reporting a fault with ScriptResource.axd and a strange parameter being passed as "d" as a querystring to it.
After some reading around, I eventually came across the following thread that was dicussing the issue.
https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=434997
The end of the thread, dated 12/10/09 states that this is a bug in IE8 that is hopefully going to be fixed soon, and that it doesn't affect the end users view of the site.
After some reading around, I eventually came across the following thread that was dicussing the issue.
https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=434997
The end of the thread, dated 12/10/09 states that this is a bug in IE8 that is hopefully going to be fixed soon, and that it doesn't affect the end users view of the site.
Monday, 29 June 2009
ASP.NET Ajax document.getElementById is null or not an object
This has taken me three days to figure out. For what seemed like absolutely no reason, my ajax enabled application just stopped working. I went back through all my changes to see what it could have possibly have been to make the application start throwing the javascript document.getElementById('') is null or not an object error, and specifically, why it was doing this on document.getElementById('HEAD[0]').
I eventually found a secton of html that I had commented out with html comments, and I had included in this an asp.net ajax tag.
Removing these comments fixed the problem!
I eventually found a secton of html that I had commented out with html comments, and I had included in this an asp.net ajax
Removing these comments fixed the problem!
Thursday, 11 June 2009
CompareValidator doesn't fire
I have an application where I am using a CompareValidator on a field that is acting as a CAPTCHA, therefore I am comparing the value that is entered.
When testing the page, I was clicking the submit button on the form, before entering any text, but when I did this, it was submitting the form! Why, when the value couldn't possibly be the same?
The reason for it is the depths of the AJAX js files:
The following line is the culprit:
If the length of the information input into the box being validated is 0, i.e. nothing entered, then this validates as true. This means that if you compare something to nothing, that is valid.... stupid, but valid.
The solution to this is to add a RequiredFieldValidator as well, which then forces the length to be greater than 0, an thus makes it all work
When testing the page, I was clicking the submit button on the form, before entering any text, but when I did this, it was submitting the form! Why, when the value couldn't possibly be the same?
The reason for it is the depths of the AJAX js files:
function CompareValidatorEvaluateIsValid(val) {
var value = ValidatorGetValue(val.controltovalidate);
if (ValidatorTrim(value).length == 0)
return true;
var compareTo = "";
if ((typeof(val.controltocompare) != "string") ||
(typeof(document.getElementById(val.controltocompare)) == "undefined") ||
(null == document.getElementById(val.controltocompare))) {
if (typeof(val.valuetocompare) == "string") {
compareTo = val.valuetocompare;
}
}
else {
compareTo = ValidatorGetValue(val.controltocompare);
}
var operator = "Equal";
if (typeof(val.operator) == "string") {
operator = val.operator;
}
return ValidatorCompare(value, compareTo, operator, val);
}
The following line is the culprit:
if (ValidatorTrim(value).length == 0)
return true;
If the length of the information input into the box being validated is 0, i.e. nothing entered, then this validates as true. This means that if you compare something to nothing, that is valid.... stupid, but valid.
The solution to this is to add a RequiredFieldValidator as well, which then forces the length to be greater than 0, an thus makes it all work
Wednesday, 10 December 2008
ScriptResource.axd - if(!a._events) error
I was getting this error today, for some unknown reason, and obviously debugging it is of no use, as the brightspark that declared everything as "a", "b" or "c" obviously didn't want us to!
Anyway, after some searching around on my page, I found that a modalpopupextender that I have added to my page, had an invalidad cancelcontrolid, I had mis-spelled it, and therefore this error must be because "a" in this instance was the control that was to be used to close (cancel) the popup.
The joys of using other peoples code....
Anyway, after some searching around on my page, I found that a modalpopupextender that I have added to my page, had an invalidad cancelcontrolid, I had mis-spelled it, and therefore this error must be because "a" in this instance was the control that was to be used to close (cancel) the popup.
The joys of using other peoples code....
Monday, 20 October 2008
AJAX NumericUpDown extender hides textbox
I couldn't for the life of me figure out why my nicely styled textbox was being hidden when I added an numeric up down extender to it.
The reason, you have to explicitly add a width attribute to the extender so that it knows how big the parent box is!
The reason, you have to explicitly add a width attribute to the extender so that it knows how big the parent box is!
Friday, 19 September 2008
Detecting an enter key press on an asp.net form (but when using AJAX as well!)
I've been more than familiar with detecting key presses in javascript for quite some time, and used to do it all the time in classic asp, but this time when I came to do it in the .net world, in was a little more involved.
This setup was also the first time I had to cope with Masterpages using AJAX inside them.
So, I have a simple content placeholder, inside this a textbox and these themselves are inside an updatepanel. I also have a modalpopup extender that shows whenever the page causes an update (asyncpostback).
So, to get this to work, I had to tie up my javascript with the ScriptManager.RegisterClientScriptBlock() method and placing the script into a LiteralControl that I had to place on the form also:
if(!Page.IsPostBack)
{
this.textBoxQuery.Attributes.Add("onkeypress", "javascript:return checkEnter(event);");
Page.RegisterHiddenField("__EVENTTARGET", textBoxQuery.ClientID);
string strScript = "function checkEnter(e){ " +
"var characterCode; " +
"if(e && e.which){ " +
"e = e; " +
"characterCode = e.which; " +
"} " +
"else { " +
"e = event; " +
"characterCode = e.keyCode; " +
"} " +
"if (characterCode == 13) " +
"{ " +
" " +
" document.getElementById('" + ButtonSearch.UniqueID + "').click(); " +
" return false; " +
"} " +
"else " +
"{ " +
" return true; " +
"}" +
"}";
ScriptManager.RegisterClientScriptBlock(LitScript, this.GetType(), "regScripts", strScript, true);
}
This now captures the enter key, and handles it as a normal button click event.
This setup was also the first time I had to cope with Masterpages using AJAX inside them.
So, I have a simple content placeholder, inside this a textbox and these themselves are inside an updatepanel. I also have a modalpopup extender that shows whenever the page causes an update (asyncpostback).
So, to get this to work, I had to tie up my javascript with the ScriptManager.RegisterClientScriptBlock() method and placing the script into a LiteralControl that I had to place on the form also:
if(!Page.IsPostBack)
{
this.textBoxQuery.Attributes.Add("onkeypress", "javascript:return checkEnter(event);");
Page.RegisterHiddenField("__EVENTTARGET", textBoxQuery.ClientID);
string strScript = "function checkEnter(e){ " +
"var characterCode; " +
"if(e && e.which){ " +
"e = e; " +
"characterCode = e.which; " +
"} " +
"else { " +
"e = event; " +
"characterCode = e.keyCode; " +
"} " +
"if (characterCode == 13) " +
"{ " +
" " +
" document.getElementById('" + ButtonSearch.UniqueID + "').click(); " +
" return false; " +
"} " +
"else " +
"{ " +
" return true; " +
"}" +
"}";
ScriptManager.RegisterClientScriptBlock(LitScript, this.GetType(), "regScripts", strScript, true);
}
This now captures the enter key, and handles it as a normal button click event.
Friday, 11 July 2008
ModalPopupExtender for deleting DataGridView rows
In the words of Little Britain, "What a caffuffle"!
How difficult does it have to be to delete a row from a datagrid, and use an AJAX ModalPopupExtender to provide a nice UI for it? The answer, very!
For all those who are trying to do it, i'm not going to re-invent the wheel, simply take yourself over to Matt Berseths' blog, where he has managed to do it (and now have I!)
http://mattberseth.com/blog/2007/07/confirm_gridview_deletes_with.html
How difficult does it have to be to delete a row from a datagrid, and use an AJAX ModalPopupExtender to provide a nice UI for it? The answer, very!
For all those who are trying to do it, i'm not going to re-invent the wheel, simply take yourself over to Matt Berseths' blog, where he has managed to do it (and now have I!)
http://mattberseth.com/blog/2007/07/confirm_gridview_deletes_with.html
Subscribe to:
Posts (Atom)