input losing src - asp.net

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>

Related

random number post 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.

How do I retrieve HTML POST data for manipulation in ASP.NET WebForms?

I have the following html:
<html>
<body>
<form runat="server">
Name: <input type="text" name="name" />
<br />
<input type="submit" name="submit" />
</form>
</body>
</html>
How do I retrieve the value in the "name" textbox posted back to the webserver to manipulate in ASP.NET WebForms?
(I know about the ASP.NET built-in controls and the possibilities with them, but I am looking for a "clean" solution without the use of built-in ASP.NET controls)
If you can't, or don't want to use asp.net textboxes, then you can retrieve the name of a regular html textbox like this:
string nameTextPosted = Request.Form["name"];
Just note that textboxes created in this manner will not automatically persist their values across postbacks like asp.net textboxes will.
Simplest solution would be to turn it into a server-side component and access it by it's name. e.g.
<asp:TextBox Id="Name" runat="server"></asp:TextBox>
...
string name = Name.Text;
Unless you have other reasons not to use a component, you'd only be making things much more difficult on your part for no justification.
ASP.net includes Html server controls for backward compatibility for just someone like you fond of html. make your html tags server controls by adding the runat="server" and id properties and you are able to access them inside your server side code with their id.
<form runat="server">
Name: <input type="text" name="name" id="name" runat="server" />
<br />
<input type="submit" name="submit" id="name1" runat="server" />
</form>
Now after this you can control their behavior:
name.Value="Hellow World !"
You have to add id and runat="server" in each control. like this :
<input type="text" name="name" id="name" runat="server" />
Its better to use asp:TextBox like this :
<asp:TextBox ID="name" runat="server"></asp:TextBox>

Password-Box not showing any content

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)

HTML/ASP.NET: <input type="hidden" name="reference" value="ABC"/>

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".

How to store value of data binding expression into variable

I need to access the value of a bound item several times in a template. Right now my ListView template looks like this:
<asp:ListView ID="ListView1" runat="server" ItemPlaceholderID="plc"><br/>
<ItemTemplate><br/>
<input type="radio" class="myrating<%# DataBinder.Eval(Container.DataItem, "Day")%>" value="3" /><br/>
<input type="radio" class="myrating<%# DataBinder.Eval(Container.DataItem, "Day")%>" value="4" /><br/>
</ItemTemplate><br/>
<LayoutTemplate><br/>
<div id="plc" runat="server"><br/>
</div><br/>
</LayoutTemplate><br/>
<EmptyDataTemplate><br/>
No data</EmptyDataTemplate><br/>
</asp:ListView><br/>
Under certain conditions I may have dozens of radio button so repeatedly calling <%# DataBinder.Eval(Container.DataItem, "Day")%> seems to be inefficient.
I would like to assign the value of that expression to a variable and then use this variable instead so my template would look something like this
<ItemTemplate><br />
<%String ClassName = "myrating" + <%# DataBinder.Eval(Container.DataItem, "Day")%><br />
<input type="radio" class="<%=ClassName %>" value="3" /><br />
<input type="radio" class="<%="ClassName" value="4" /><br />
</ItemTemplate><br />
This example doesn't compile but I hope you are getting the idea.
You can give your page a public variable MyRating.
Now you can assign the variable in the expression binding Syntax:
<ItemTemplate>
<%# MyRating = "myrating" + <%# Eval(Container.DataItem, "Day")%>
//Use the variable inside the binding(!) block
<%#MyRating
</ItemTemplate>
I usually bind to lists of view-objects. That way I can access view properties directly.
<ItemTemplate>
<%# MyType = (MyType)Container.DataItem
<%# MyRating.Average %>
<%# MyRating.Count %>
</ItemTemplate>
Hope this helps :-)
You can use OnItemDataBount event and work with DataItem as with variable there.
I personally consider setting HTML element values in the OnItemDataBound event to be messier than in the ASP code. I also didn't like setting the variable value using a data-binding expression since it seems to cause the value to be output in the HTML.
Here's a similar way to do it based on the other answers:
1. Create a protected field in the code-behind to use as your variable.
protected string className;
2. Use a data-binding expression to assign to the variable.
<asp:Literal runat="server" Visible="false" Text="<%# className = "myrating" + DataBinder.Eval(Container.DataItem, "Day") %>" />
Make sure to do this inside the hidden server-side tag so that the result of the expression does not appear in the resulting HTML.
3. Use the variable inside data-binding expressions in the ASP code.
<ItemTemplate><br />
<input type="radio" class="<%# className %>" value="3" /><br />
<input type="radio" class="<%# className %>" value="4" /><br />
</ItemTemplate><br />
Make sure to use data-binding expressions to access the variable. Other expression types only seem to see the default value of the variable.
I think the main disadvantage of this approach is the use of the field, which would ideally be scoped to the ItemTemplate element.
You can define public/protected variables in the code-behind and assign those in data binding expressions.
If you don't have access to the code-behind (e.g. when you are modifying the markup of a compiled application), you can declare variables in a script block set to run on server, e.g.:
<script runat="server">
YourNamespace.Rating current;
</script>
Then in your control's binding templates:
<HeaderTemplate>
<%# (current = (YourNamespace.Rating)Eval("Day")) == null ? "" : "" %>
</HeaderTemplate>
Put it in the HeaderTemplate if you only want it to get executed once per databinding.
The == null ? "" : ""-part, is to prevent any generated cruft html (e.g. the ToString value of the assigned value), from being rendered in the browser.

Resources