Cannot get Javascript function to fire from a textbox in a gridview - javascript

I have a textbox in the ItemTemplate of a Gridview. On the _RowDataBound event I add an attribute to the textbox like so:
TextBox txtQuantity = (TextBox)e.Row.FindControl("txtQuantity");
txtQuantity.Attributes.Add("onkeypress", "CheckInputForNumeric(event)");
And it simply will not fire a JS function.
I've tried doing onClick, onBlur, onKeyPress... even tried changing the case to: onclick, onblur, onkeypress... nothing seems to be able to fire my JS function.
elsewhere on that same page I have:
txtAddMarkup.Attributes.Add("onkeypress", "CheckInputForNumeric(event)");
(that textbox is not in a gridview)
and this works just fine.
I'm totally stuck and frustrated at this point because it seems no matter what I do, I cannot get this textbox to fire a JavaScript function

Please run your project and look at the name of the textbox generated by viewing the source in the broswer (IE, Firefox, Safari, whatever). You'll likely see that the name of the textbox has changed. Thanks, ASP.
You can't use the DOM to access the elements by name because they're renamed for you.

For some reason, deleting temporary internet files and reloading the page wasn't getting the newest .js. I had to unload the project and re-build it. Which is weird because I've never had to do that before for other controls
thanks for your inputs!

try this one:
TextBox txtQuantity = (TextBox)e.Row.FindControl("txtQuantity");
txtQuantity.Attributes.Add("onkeypress", "CheckInputForNumeric(event)");
txtQuantity.Attributes["onkeypress"] =
string.Format("javascript:CheckInputForNumeric(this,'{0}','{1}','{2}');", argument1, argument2, argument3);
Or Simply
txtQuantity.Attributes["onkeypress"] =
string.Format("javascript:CheckInputForNumeric (this);");

Related

Simulate sending data to the text input field with JS

I have a task where I need to automate Sign in form authentication. For this example, I'll show you Tiktok authentication form (Mobile interface, not desktop. E-mail and password option)
If I enter text values into the fields programmatically, the Login button won't become active, and if I manually focus on the fields with a mouse click, the value disappears. These are two lines of code I run to put the value in:
let email_input = document.getElementsByName("email")[0];
email_input.value = 'sample#email.com';
I understand it needs to trigger a certain event to assign a value into it's JS model, but I can't figure out how to do it. I have tried sending change or input events onto this text field with no luck using this code:
let email_input = document.getElementsByName("email");
email_input[0].value = 'sample#email.com';
custom_event = new Event('input');
email_input[0].dispatchEvent(custom_event);
// tried also change, textInput like so:
custom_event = new Event('change');
email_input[0].dispatchEvent(custom_event);
But this does not seem to help.
So my goal is to put values into both fields Email and Password in the way it will be detected and Log in button would become active.
Any suggestion would be much appreciated
You should first focus needed input element and then execute document.execCommand with insertText command:
let email_input = document.getElementsByName("email");
email_input[0].focus();
document.execCommand('insertText', false, 'sample#email.com');
With this method input\textarea value modification should be captured by all major frameworks including Angular and Vuejs. This modification will be processed by frameworks the same way as if user pressed "Paste" option in browser main menu.
It all depends...
Who/what are you? A normal browser user? A bot? The browser author?
Because code like this is useless...
let email_input = document.getElementsByName("email")[0];
What document are you referring to? Who's document? Did you inject this instruction into the page and executed it?
You're not telling us where you're coming from, but anyway...
If you are the browser author, or you can run JavaScript macros from your browser (ie: the Classic browser) then you can do something like this...
var Z=W.contentWindow.document.querySelectorAll('input[type="password"]');
if(Z.length>0){
Z[0].value='password123';
Z=W.contentWindow.document.querySelectorAll('input[type="email"]');
if(Z.length>0){Z[0].value='email#abc.com';}
}
To automatically populate such fields, and if you also want you can SubmitButtonID.click() the submit button for as long as the isTrusted property is not tested by the website.
Continued...
Test if normal (non-custom) submit button exists and click...
Z=W.contentWindow.document.querySelectorAll('input[type="submit"]');
if(Z.length>0){
if(Z[0].hasAttribute('disabled')){Z[0].removeAttribute('disabled');} <--- Enable it if disabled
Z[0].click(); <--- automate click
}

Why cant I disable ajaxToolkit:AsyncFileUpload

Iv tried many things (JS/Jquery) to disable this AsyncFileUpload but none are working...please advise..
<ajaxToolkit:AsyncFileUpload OnClientUploadError="uploadError"
OnClientUploadComplete="ajaxUploadImage_ClientUploadComplete" runat="server"
ID="ajaxUploadImage" Width="400px" UploaderStyle="Modern"
CompleteBackColor = "White"
UploadingBackColor="#CCFFFF" ThrobberID="imgLoader"
OnUploadedComplete = "ajaxUploadImage_OnUploadComplete"
OnClientUploadStarted="AssemblyFileUpload_Started"
/>
var upload = $$('ajaxUploadImage');
upload.enableSelection('false');
var upload = $$('ajaxUploadImage');
upload.enableSelection('false');
upload.disableSelection();
document.getElementById("ctl00_MainContent_MapUserControl_ajaxUploadImage").disabled = true;
So...iv tried using the ID assigned by me..iv tried using the ID assigned in the browser....what am I doing wrong? user can still click of the text box or the select button and the pictures folder pops up, allowing the user to select an image for upload
should note that iv also tried disabling it from code behind
ajaxUploadImage.Enabled = false;
Have also tried setting disabled="true" inside the control, and checking the control in developer tools it is disabled, but I can still click on the text box or button and the pictures folder opens
The control emits its own markup and is hard to manage.
You can try set Visible=false, because its logic runs every time the page is loaded and the control is visible.
I have been putting my head into the disabling part and it seems we can surely disable the AsyncFileUpload control.
The trick is, ajax toolkit re-assigns the ids to the its controls, so whatever your id is, it will re-assign a new id to it even if you are using <%= control.ClientID %> this approach to get the names of the control.
So, I would suggest you inspect the element in the browser and copy the element ID from there.
My Scenario:
This was my control in the markup:
<asp:AsyncFileUpload CssClass="custom-file-input" ID="fileupload" runat="server" OnUploadedFileError="fileupload_UploadedFileError" OnUploadedComplete="fileupload_UploadedComplete" />
Notice, My control's ID is fileupload. And this is how I was disabling it before:
$('#<%= fileupload.ClientID %>').attr('disabled', true);
Which obviously never worked because $('#<%= fileupload.ClientID %>') this returned me the ID as #fileupload which is not correct because originally, ajaxtoolkit had modified it from #fileupload to #fileupload_ctl02 so, I had to hard code the ID in my javascript to get it working. e.g:
$('#fileupload_ctl02').attr('disabled', true); // I am using an older version of jQuery,
For newer versions of jQuery, you would disable it using the prop() method instead.
JS
document.getElementById('fileupload_ctl02').disabled = true;
Hope it helps someone.

onkeyup event seems to not fire or is ignored

I have this:
txtNew.Attributes.Add("onkeyup", "alert('hi');");
But when I type in the textbox - no alert comes up. I just type and type and no alert ever comes up. Why is this not working?
Tried in IE 11, Chrome 42.0, and FF 37.0. Using .NET 4.0.
Also tried the "onkeypress" event. Same results. I cannot get the alert to come up.
The textbox definition is:
asp:TextBox ID="txtNew" TextMode="Password" runat="server" MaxLength="256
Tried removing the TextMode attribute but the result is still the same.
Ultimately, what I want to do is this:
txtNew.Attributes.Add("onkeyup", "CheckChars()");
Where CheckChars() is my JS script. However when it didn't work, I just put an 'alert()' in there to see if even that would work.
You can't just reference the ID of a control to reference it in javascript. You need to actually get a reference to the control using
var txtNew = document.getElementById("txtNew");
txtNew.Attributes.Add("onkeyup", "alert('hi');");
Edit: Actually, is this code in your javascript or in your codebehind? The question is unclear.

Setting field focus after javascript alert not working in MS CRM

I have the following code in the OnChange() event for a field.
alert("alert text");
crmForm.all.fieldname.SetFocus();
The page acts like the SetFocus call isn't even there.
Anyone know why this is?
EDIT: I've also tried the following to no avail.
crmForm.all.fieldname.Focus();
crmForm.all.fieldname.focus();
alert("alert text", function() { crmForm.all.fieldname.SetFocus()});
In the DOM, the function to set focus on an element is called focus(), not SetFocus().
Turns out that retaining focus on the field from which the OnChange() method was called is broken in CRM 4 without the most recent rollup. This is a known issue with a Microsoft KB article.
To achieve the illusion of retaining focus on the field simply set the focus to a different field on the same tab first and then reassign the focus to the field from which the OnChange() event was called like so:
alert("alert text");
crmForm.all.some_other_field_on_the_same_tab.SetFocus();
crmForm.all.fieldname.SetFocus();
Seems like the same problem exists in CRM 2011 - event when working with Xrm.Page.
The workaround is still working:
Xrm.Page.getControl("name").setFocus(true);
Xrm.Page.getControl("TheFieldYouReallyWantToFocus").setFocus(true);

Get Element in div with JavaScript

I want to disable/enable a button with JavaScript. Because the JavaScript is called, after a Flash animation is rendered, the button exists at the time of the execution.
The button is in a hierarchy:
<html><body><form#form1><div#control><asp:Button#Export1>
I tried for hours to get a reference to that button, but nothing seems to work:
document.getElementById("Export1")
// and
document.getElementbyId("form1").getElementById("control").getElementById("Export1")
// and many more
How to get a reference to that button (in order to change btnref.disabled = true)?
Thanks a lot for your help!
Have you tried right-clicking in the document and selecting "view source" to see how that code actually renders? An asp:Button is a server control, that gets translated to an input field during render. During this, the ID of the field will not be exactly what you set it to in your aspx.
You can use Export1.ClientID at serverside to get the ID of the control.
If it's the only button in your div, this should work:
var btnref = document.getElementById("controls").getElementsByTagName("button")[0];
Usually the id of the button won't stay the same in the page source. Click on view source in the HTML and look for that tag to find the new id. You can then use that id in something like:
document.getElementbyId("Export1_some_unique_id")...

Categories