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.
Related
I am trying to use the server control for an asp checkbox:
<asp:CheckBox ID="generalInformation"
ClientIDMode="Static"
class="SetupChecklist"
name="generalInformation"
CssClass="SetupChecklist"
runat="server" />
However it does not seem to be retaining the class, which I need it to do. Am I missing something?
Here is how it renders on the web page:
<input type="checkbox"
name="ctl00$ContentPlaceHolder1$generalInformation"
id="generalInformation">
ah.......
this is the problem:
runat="server" checkbox is render to span + input...
the span gets the class not the input....
<span class="SetupChecklist" class="SetupChecklist" name="generalInformation">
<input id="generalInformation" type="checkbox" name="generalInformation" /></span>
You might want to use the CssClass property instead:
<asp:CheckBox ID="generalInformation"
ClientIDMode="Static"
name="generalInformation"
CssClass="SetupChecklist"
runat="server" />
Edit: I've only just seen that you've already tried it. Ok, CssClass actually applies the class to a span that wraps the check box, so try this instead:
generalInformation.InputAttributes("class", "SetupChecklist");
It will render the HTML like below.
<span class="SetupChecklist" class="SetupChecklist" name="generalInformation">
<input id="generalInformation" type="checkbox" name="generalInformation" />
</span>
Assuming that you are trying to design the input tag.
Now your style sheet can be like below for the <input> tag
<style type="text/css">
.SetupChecklist input
{
border:0px;
}
</style>
Originally, the span tag was occupying the class information.
remove the class attribute and just use the CssClass
I have a small websolution that needs the user to input a password. I have two input boxes
<input type="password" runat="server" id="m_txtPassword1"/>
If I set some chars to the Value-property of the control like this:
m_txtPassword1.Value="someChars";
The password box is rendered empty. No bullets are shown. If I look into the rendered html-source, also no value-tag has been rendered. If I change the type to
<input type="text" runat="server" id="m_txtPassword1"/>
the chars are shown. Is this by design? How can I disable this feature?
Please note, I don't want to put a real password into the value-property, I only want to show the user that there is already a password set, and this is IMO done best with some 8 bullets in the input-control. But for this, I need the possibility to set the value-property of the control.
Update
For all, having the same problem: I have tried to declare <asp:textbox id="m_txtPassword1" runat="server" TextMode="Password" /> with the same result. Also m_txtPassword1.Attributes["value"]="someChars" has not helped.
It seems that this is realy not possible.
As a workaround, I declared the password-boxes as plain html without the runat="server" and have set the value-property in markup (via two properties from the code-behind). Not nice but I really want to show the user that he has already entered a password.
Another workaround would be to set the value through javascript on load.
This is by default. You cannot set a password.
I make this works,
<html>
<head>
<script type="text/javascript">
function alertValue()
{
alert(document.getElementById("password1").value);
}
function setpassword()
{
password1.value="someChars";
}
</script>
</head>
<body>
<form>
<input type="password" id="password1" value="" />
<input type="button" id="button1" onclick="alertValue()" value="Show default value" />
<input type="button" id="button2" onclick="setpassword()" value="Set value" />
</form>
</body>
</html>
Try this:
http://jsbin.com/ocexo5
It is by design, for security reasons - so that the password is not in the
HTML in plain text.
Now, you could write out javascript to set the value property of the textbox
on the client, of course this would still mean that you have the password in
plain text in the HTML.
Here is example of the page:
Markup:
<script type="text/javascript">
function setPwd()
{
var Pwd = "<%=Pwd %>";
var txtText = document.getElementById("MainContent_txtText");
txtText.value = Pwd;
}
</script>
<input type="password" id="txtText" runat="server"/>
And code-behind:
public partial class _Default : System.Web.UI.Page
{
public string Pwd;
protected void Page_Load(object sender, EventArgs e)
{
Pwd = "password";
ClientScript.RegisterStartupScript( this.GetType(),"somescript","setPwd();",true);
}
}
Textbox11.Attributes.Add("Value",whatever_your_password_is)
I just wanna ask if there's a possibility to change:
<input type="hidden" name="reference" value="ABC"/>
into this:
<input type="hidden" name="reference" value="any values I want"/>
where I can set any values behind .cs/C# - making it dynamically. The payment gateway I'm using requires and I can't find a way to included an ASP.NET control ( ?)
and I'm needing your suggestions/comments about it. Thanks.
PS. <asp:HiddenField ID="reference" runat="server" Value="ABC" /> is not working because the payment gateway specifically needs the 'name' property.
I know this is an old post, but for anyone looking to solve this issue now - If you add runat="server" to the input, the name will be changed (e.g. MainContentArea_ctl00_ctl01_ctl01_amount). ClientIdMode="Static" will only help for the ID.
To get around this:
In your html page use a Literal :
<asp:Literal runat="server" ID="litInputAmount"></asp:Literal>
In the code behind file, assign a string to the Text attribute of the Literal This string should be the html as you would like it to be. The correct value can also be added for the value field:
litInputAmount.Text = String.Concat("<input id='inputAmount' type='hidden' name='amount' value='", Price.ToString(), "'>");
This will then be compiled as:
<input id="inputAmount" type="hidden" value="224.4" name="amount">
This will give the information to the payment gateway with the correct name, but your value can be managed dynamically. Repeat for any other values that need to be added before sending.
You can just put runat="server" on the control to access it from your code behind:
<input type="hidden" name="reference" id="reference" runat="server" />
Then, in your code behind:
void Page_Load(object sender, EventArgs e)
{
// ...
reference.Attriutes["value"] = "any values I want";
// ...
}
Note that in this case, the "id" attribute is required because when you have runat="server", the id attribute is used to specify the name of the generated variable.
You can use standard input of type hidden as if you are working with static HTML or Razor, and rely on the <%= expression, which is evaluated at render time rather on DataBind() time as the <%# expressions would.
This way, you can have a normal html, for which you can have ASP.NET WebFroms generate the hidden input's value for you server side, without actually having to mark the input with runat="server" or using <asp:HiddenInput control. See the example below, which should do the job:
<input type="hidden" id="add-to-wishlist-url" value='<%= YourServerSideExpressionHere.Execute() %>' />
Of course, this approach is not one size fits all, but seems like the closest to the meet the requirement described 7 years ago...
//<input type="hidden" name="__EVENTTARGET" id="__EVENTTARGET" value="" />
protected string GetVariableValue(string AspxPage, string inputTagName)
{
ra migirad
string RegPattern = string.Format("(?<=({0}\".value.\")).*(?=\"./>)", inputTagName);
Regex regex = new Regex(RegPattern, RegexOptions.IgnoreCase);
Match match = regex.Match(AspxPage);
if (string.IsNullOrEmpty(match.Value))
{
RegPattern = string.Format("<input[^>]*{0}[^>]*value=\"([^\"]*)\"", inputTagName);
regex = new Regex(RegPattern, RegexOptions.IgnoreCase);
match = regex.Match(AspxPage);
return match.Groups[1].Value;
}
return match.Value;
}
Apply the parameter:
ClientIDMode="Static"
For example:
<asp:HiddenField ID="reference" runat="server" Value="ABC" ClientIDMode="Static" />
with this, the "ID" with maintain exactly as "reference".
If I use the following code without runat="server" the input's src works fine and I see the image coming through.
<div><input id="testButton" type="image" src="<%=TestButtonImageUrl %>" onserverclick="RedirectTest" /></div>
url is https://fpdbs.paypal.com/dynamicimageweb?cmd=_dynamic-image
But if I put the runat="server" in, for some reason, I get this for the url:
<div><input id="testButton" type="image" src="<%=TestButtonImageUrl %>" onserverclick="RedirectTest" runat="server" /></div>
url is http://localhost/%3C%=lButtonImageUrl%20%%3E
You cannot use the <%= %> syntax with server controls (including standard HTML elements with runat="server"). You have two choices:
Access the control in the code behind (as an HtmlInputControl) and assign the src attribute using the Attributes property: imageControl.Attributes["src"] = value;
Assign the attribute using the databinding syntax (src="<%# %>") and call imageControl.DataBind() from the code behind
Maybe I'm missing something. But runat server tag does not support code expression.
When you add runat="server to that html tag, Asp.Net converts it from string to HtmlControl - in this case of type HtmlInputImage. You can see this happen by adding:
<%= testButton.GetType() %>
Then the only thing you need to do is set the Src-property, which, contrary to other comments, you CAN do in inline aspx - no need for a code-behind file:
<%
testButton.Src = "/content/logo.png";
%>
<input id="testButton" type="image" runat="server" src="" onserverclick="RedirectTest" />
You need to set the Src-property BEFORE the actual input, which is a bit non-intuitive, the reason is that the code is run at render-time, so if the setting of Src-property is after the control, it is too late.
if jQuery is an option than you could try this:
<script type="text/javascript">
$(function() { $('#<%=testButton.ClientID %>').attr('src', '<%=TestButtonImageUrl %>'); });
</script>
...
<div><input id="testButton" runat="server" type="image" onserverclick="RedirectTest" /></div>
Update:
Another option is to create a HttpHandler with processing like this
public void ProcessRequest(HttpContext context)
{
var testButtonImageUrl = "https://fpdbs.paypal.com/dynamicimageweb?cmd=_dynamic-image";
context.Response.Redirect(testButtonImageUrl);
}
add in web.config path to handle image.img or whatever and update aspx
<div><input id="testButton" runat="server" type="image" src="image.img" onserverclick="RedirectTest" /></div>
How this code should be (asp.net standard)?
<label for="jobno">Job#</label>
<input id="TextBox_JobID" name="jobno" runat="server"/>
<input type="button" value="Show Job" class="fbutt"
id="Button_showJob" onserverclick="Button_showJob_ServerClick"
runat="server" />
</p>
<p>
<asp:Label ID="Label_error" runat="server" class="error"></asp:Label></p>
I think the for attribute is not ok, or not written in correct way?
The value of the for attribute must match the id of a form control (such as an input element or a select element), not the name.
As the textbox is marked runat="server", I would suggest using the ClientID property of the control:
<label for="<%=TextBox_JobID.ClientID%>">Job#</label>
Then if you use master pages/user controls etc, you can be sure it will always contain the right value.
It should probably read
<label for="TextBox_JobID">Job#</label>