I'm trying to write a small program in C# to calculate a equation with a few known variables. A few textboxes (where each variable need to be typed) and a single "calculate" button.
What I'm trying to implement now is that my keyboard cursor is active in a selected textbox when the program starts.
But I can't figure it out.
Hope someone can help me.
For Winforms : View > Tab Order. Set tab order of start textbox to 0.
write this in your head tag
<script type='text/javascript'>
document.getElementById('txtName').focus();
</script>
I see, you forgot to write down your C# (when i post my first answer).
If you're using C# it would be lot easier thank HTML or PHP.
Simply type this in form_load() :
<your textboxname>.Focus()
Example :
TextBox1.Focus()
You can set up the order of focus with tabindex
e.g.
<input id="thing" tabindex="1" />
tabindex is ordinal. When set, the element with the lowest tab index value will be the first to gain focus.
You can also use tabindex to ensure the next element that the user will hit if he or she presses tab.
You can do that using JQuery.
Simple use that code:
$('#TextBox').focus();
where the TextBox is a ID from your TextBox Control.
I hope that helps.
So, what programming language that you are using?
In php you could use simple javascript :
<form>
<input type="text" id="mytext1" name="mytext1" value="1"><br>
<input type="text" id="mytext2" name="mytext2" value="2"><br>
<input type="text" id="mytext3" name="mytext3" value="3"><br>
</form>
<script language="javascript" type="text/javascript">
var mytext = document.getElementById("mytext1");
mytext.focus();
</script>
when you load the page it would be focused on "mytext1"
Hope this helpful
Related
I'm trying to automate the use of a website using Selenium, at one time I want to click on a hidden button defined this way :
<body>
<div class="smenu" id="smenu4">
<input tabIndex="-1" type="button" onclick="SearchEng" value="FindEng" />
<!--> Lots of inputs <!-->
</div>
</body>
I already tried to simply click the button, but it doesn't work. I can select it and retrieve information though. That's why I'm now trying to run a javascript to make the button visible before clicking it. Here is my code :
IWebElement element = driver.FindElement(By.XPath("//div[#id='smenu4']/input[#value=FindEng"));
String js = "arguments[0].style.height='auto'; arguments[0].style.visibility='visible';";
((IJavaScriptExecutor)driver).ExecuteScript(js, element);
The thing is nothing happens when I launch it. Is it possible that I can't run scripts ? Can you run them on any website ? - I use internet explorer 11, windows 7. Thanks !
Inspect the element in your Browser and check that what is the reason that the element is invisible. Or just simply compare the element when it is visible or not. Possible reasons:
The element is not in the DOM yet.
The element hasn't got width and/or height.
visibility parameter. (http://www.w3schools.com/cssref/pr_class_visibility.asp)
display parameter. (http://www.w3schools.com/cssref/pr_class_display.asp)
When you know the reason, you will know the solution :)
I am still new to the coding field and am having a bit of trouble with a part, hoping someone can help me. I am working on a MVC page where i am trying to move text, the user inputs, around the page to a few pre-set spots and without having to refresh the page. Do i need a type of script for this? And if i do what would be best? Thanks for the help.
You can do it this way.
Enter Text here... <input id="testText" type="text" onKeyUp="javascript:showText($(this), event);"/>
<br/>
<br/>
<br/>
See it here... <span id="testTextSpan"></span>
Then, add this script...
<script type="text/javascript">
var showText = function (el, e) {
$('#testTextSpan').html(el.val());
};
<script>
See it here in this JSFiddle...
http://jsfiddle.net/ZEcNp/
Be sure to include JQuery libraries in your project, or reference the CDN.
If you'd like me to include an example with raw javascript, with no JQuery, I can also provide that for you.
I would recommend jQuery to do all that client-side.
You would just need an ID for each element you would use as the predefined spots, using either .appendTo, .insertAfter, .html, or something else depending on your exact needs.
I am trying to get the value of a hidden input in code behind with the following code. I am trying to cast it but it cannot find it , any help ?
((HtmlControl)FindControl("contentId"))
I declare it in aspx with the following code:
<input id="contentId" type="hidden" />
I dont want to runat server because i have my own reasons
To access a HTML control at server side (in your C# code), you need to first add the runat="server" attribute. So, your markup should look like
<input type="hidden" id="contentId" runat="server"/>
Now, in the code behind you can use the control by its id contentId itself if the code behind got generated properly.
Please let us know why you are forced to use the FindControl in the first place as it can be accessed by using the id directly.
Update
As per the comment below, the user for some reason is not interested in making this input a server side control. Then the only possibility by which you can read the values at server side is as below. But this is not advised as any changes to the name goes unnoticed and breaks at runtime.
<input type="hidden" id="contentId" name="contentName" runat="server"/>
In Code
this.Request.Forms["contentName"] would return the hidden value.
Try to search it on the page this way
HiddenField hf = (HiddenField)page.FindControl("contentId");
To get the value:
HiddenField h = (HiddenField)Gridview.FindControl("HiddenFieldName");
Then with that you can put it into a string, if you wish to.
Use this code:
string s=((HiddenField)Panel1.FindControl("contentId")).Value;
Here panel is the container control. This may be a grid or anything else or even a master page. But if you are using FindControl, i think the control may be inside some container.
HtmlInputHidden hf = Page.FindControl("contentId") as HtmlInputHidden;
string hfValue = hf.Value;
I am working on an WebForms app, and we want to put a speed bump, for lack of a better term, in for the users at a particular point in the app. There is a particular action which is not supposed to be undone. So when they click the button to do that action we want to display a small confirmation window, have them enter a random string that we give them. Once they enter that string, and it matches the corresponding label, the submit button becomes enablesand they can perform the action. But for the life of me I can't figure out a good way to do this client side with WebForms. Is there a simple mechanism to use this type of workflow without a ton of post back events?
Note: This is an internal app where high security isn't truly a necessary requirement in this case. As I said, this is meant to be something to slow the user down slightly.
This is similar to the mint.com confimration style.
Add JavaScript to the textbox onChange or onKeyUp event, there do your check and enable the button.
For example:
<script type="text/javascript">
function checkConfirmationText(){
// Check if value of entered text = value of hidden text
var isOk = document.getElementById('confirmation-label').value == document.getElementById('confirmation-text').value);
// Show/Hide button depending on the text
document.getElementById('btn-submit').style.dispaly = isOk ? '' : 'none';
}
</script>
<!-- HTML -->
<input type="hidden" id="confirmation-label" value="DELETE" />
Enter "DELETE": <input type="text" id="confirmation-text" value="" onkeyup="checkConfirmationText()" />
<input type="submit" id="btn-submit" style="display:none" />
You could generate the javascript method to check the code (including the code in the method) server side and use registerclientscriptblock (or whatever the current method is), then use the onblur to call that method.
I want to display a background text in the textbox.
ex: The Title textbox in the stackoverflow Ask question page.
Needs: If i enter some text inside the textbox it should disappear.
Geetha.
A good link is http://attardi.org/labels as referenced here https://stackoverflow.com/questions/781473/how-to-create-a-label-inside-an-input-element
You need to use javascript for the same.
Use following two functions in javascript and it is done.
function clearDefault(obj,val)
{
objInput=document.getElementById(obj);
if(objInput.value==val)
{
objInput.value="";
}
}
function setDefault(obj,val)
{
objInput=document.getElementById(obj);
if(objInput.value=="")
{
objInput.value=val;
}
}
You need to apply it on your code by using two javascript events as follow.
<input name="email" type="text" class="textbox" id="email"
value="Email" size="12" onfocus="clearDefault('email','Email');"
onblur="setDefault('email','Email');"/>
So when you control get focus it the default text will be cleared.
Ant That's it.
Hope this will help!
Well, since you put jquery as a tag for the question, I assume you're already using jquery for your project. In that case, here's a neat plugin that does the trick: Labelify.
Good luck!