How find in which text box is focused in Asp.Net
Hi
I have four text box. In my application the 2nd text box is focused. while i click
the button the 2nd text box is focused. But i dont know while i click the button
In which text box is focused. How i find that while i click the button in which
text box is focussed. I hope for ur's reply friends.
Thanks
Re: How find in which text box is focused in Asp.Net
The close I could get is using some JavaScript. Add a script for onfocus event of TextBox, and there pass the ID of selected or just focused textbox to a hiddenfield.
When clicking the button, get the value from this hiddenfield which contains the current focussed textbox.
HTML Code:
<head>
<script type="text/javascript">
function btnclick()
{
alert(document.getElementById('hid').value);
}
function fcs(txt)
{
document.getElementById('hid').value = txt;
}
</script>
</head>
<body>
<input type="text" id="txt1" onfocus="fcs(this.id);" /><br />
<input type="text" id="txt2" onfocus="fcs(this.id);" /><br />
<input type="text" id="txt3" onfocus="fcs(this.id);" /><br />
<input type="text" id="txt4" onfocus="fcs(this.id);" /><br /><br />
<input type="button" id="btn" onclick="btnclick()" value="Check" />
<input type="hidden" id="hid" />
</body>
Re: How find in which text box is focused in Asp.Net
Just a slight modification. Because the controls may be dynamic and of an unknown quantiy, you may want to attach the javascript via DOM.
Loop through getElementsByTagName ('*'), for each element, set
element.onfocus = fcs(this.id);
Do this in the body onload (javascript).