I am developing my app in asp.net web forms. The textbox is set like this
<asp:TextBox ID="txt1" runat="server" Enabled="false" ></asp:TextBox>
and this is the corresponding HTML markup
<input name="txt1" type="text" value="1.0" id="txt1" disabled="disabled" />
It is not editable upto this point.
I enable Caret browsing in IE8 (press F7) and then this field becomes editable, though the text is grayed out and consequently gives a wrong feeling to the user that the field is editable. This does not happen if I mark the textbox as readonly, but I do not want to mark it as a readonly field. Any suggestions on how to have the textbox in disabled mode when in Caret Browsing.
Edit1: I am not looking for a solution which would change IE settings/registry, am looking for a programmatic solution as my site is a public facing website
Edit2: View states are enabled for the page and for the controls
I can get you started quickly with this script:
<script> document.onkeyup = KeyCheck;
function KeyCheck() { alert(document.getElementById("txt1").value); } </script> </script>
Put this all the way on top of the html.
Now you get hold of the keypress.
When in caret mode, IE seems to ignore the text's javascript events
Alright. I gave you a Hint to build up on. (Thanks for the -1)
Here is the HTML code that you are looking for. Make sure to populate the initialVal using ASP code.
<script>
document.onkeyup = KeyCheck;
var initialVal="1.0"
function KeyCheck()
{
if(document.getElementById("txt1").value != initialVal) {
document.getElementById("txt1").value = initialVal;
return false;
}
}
</script>
<input name="txt1" type="text" value="1.0" id="txt1" disabled="disabled" />
Related
I have converted ASP.NET WebForms DropDownList to dynamic loading with the great Select2 library. When I use loading of remote data, I need to use a
<input type="hidden" id="foo" />
that I'll turn into Select2 box with
$(document).ready(function () {
$("#foo").select2(...);
});
The problem is that when I'm trying to incorporate this into ASP.NET page and set the initial value from a query parameter, I can either use
<input type='hidden' id='foo' value='<%=Request["#id"] %>' class='bigdrop'/>
which I don't like, because I'll have logic in my markup, or
<asp:HiddenField runat="server" ID="foo" ClientIDMode="Static"/>
and set the value in codebehind, but I am unable to set the class for the input.
Which approach should I use?
there is no CssClass property on HiddenField! But I solved it in jQuery -
$(document).ready(function () {$("#foo").addClass('myclass'); }
Answer by "Axarydax"
I cant Implement html 5 on asp:textboxes, everything I found on the internet was implemented on input: not on asp:textboxes
now I found a very useful validator for textboxes, however, I can't make it work with asp:textbox
here are the codes:
<script>
$(document).ready(function () {
$('form').h5Validate();
});
</script>
<input id="name" name="name" type="text" placeholder="Bob" title="Your name is required." required />
I want to make it work with asp:textbox
asp:textbox is a server side tag and after client request, server will return html with <input>; so all options for <input> also can be used for asp:textbox.
i have a system that generates unique numbers for reference..here is my function in jquery:
function random() {
return Math.floor(Math.random() * 100000000);
}
function generateRandomNumber() {
$("#GenerateCode").val(random());
}
window.onload = function () {
generateRandomNumber();
}
here is my mark up(asp):
<div id="step3" class="formwiz">
<h4>Step 3: Reference Number</h4>
<br clear="all" />
<p>
<label>Reference Number</label>
<span class="field">
<input type="text" id="GenerateCode" name="GenerateCode" class="smallinput" disabled="disabled" /> <small style="color: red">Please write the numbers at the back of your check</small>
</span>
</p>
</div>
this function works fine with html (input)...but when i shifted from input to ast textbox, the numbers are not posted in the textbox...do i need to use getelementbyid.?
please help...
Try using a class instead so change the class attribute to:
class="smallinput random"
and change the javascript to:
$(".random").val(random());
I would take a wild guess that you're actually using ASP.NET and not classic ASP and you try to change the text box to:
<asp:TextBox id="GenerateCode" runat="server" CssClass="smallinput" Enabled="False" />
In such case, the generated ID might be different than what you give it, if the textbox is part of control, placeholder or something like this.
To ensure same ID use ClientIDMode and set it to Static:
<asp:TextBox id="GenerateCode" runat="server" ClientIDMode="Static" CssClass="smallinput" Enabled="False" />
Now the jQuery code should work fine again.
I have a set of websites that basically do the exact same thing in terms of functionality, the only thing that varies is the css and how the html is laid out.
Is there a way to generate the html from a database (ie retreiving the html from a table) and then catching the controls (like buttons, textboxes etc) from the code behind?
UPDATE:
Here is an example of what I want to acheive:
<html>
<div id="generatedContent" runat="server">
<!-- the following content has been generated from the database on the page load event -->
<div>
This is a textbox <input id="tb1" runat="server" type="text" />
This is a button <input type="submit" id="btn1" runat="server" value="My Button" />
</div>
</div>
</html>
This is the code behind
var tb1 = new HtmlControls.HtmlInputText();
tb1 = FindControl("tb1");
var btn1 = new New HtmlControls.HtmlInputSubmit();
btn1 = FindControl("btn1");
Now obviously since the html has been generated from the database (ie after the page has already been initialised) the FindControl method will return null.
For static HTML content, you can easily add your content to your form by using LiteralControl like that :
Page.Controls.Add(new LiteralControl("<b>content from db</b>"));
EDIT : Your code-behind code (FindControl) should work if you put them in a form tag with runat=server attribute. But beware, your content should be added in every postback. Also you can get the values of your form elements by Request["FormElementId"].
I am dynamically creating a CheckBox and setting the disabled property like this:
chk.Disabled = false.
This renders the following HTML:
<input type="checkbox" disabled="disabled" .../>
On the click of the checkbox, I have a JS function that tries to enable it by doing a:
//get reference to the checkbox
chk.disabled = false;
This does not work. Any help?
If your checkbox is disabled, your onclick wont be called
What you're trying to do is a bit odd. You're trying to enable a checkbox by clicking on the checkbox, which is disabled to start with. So, the onclick will not be registered until the checkbox is actually enabled.
Try the below to see what I mean.
<html>
<body>
<input id="cb" type="checkbox" disabled="disabled" onclick="this.disabled=!this.disabled;" />
<label for="cb">Click me</label>
<input type="button" value="Click me instead!" onclick="cb.disabled=!cb.disabled;" />
</body>
</html>
Hope that helps you out.
How are you dynamically creating the check box?
Keep in mind that ASP.NET will change the name of the check box you give it, especially if you add it on the code behind.
What you need to do is send to your JS function the clientId, which is the id the HTML item will get when render.