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.
Related
I want to add value in input element, if session variable's value is not null
<input id="sessionInput" type="hidden" value='<%# Session["UserLogin"] != null ? Session["UserLogin"].ToString() : ""; %>' />
But it is not working.
Error
) expected
You have a semicolon at the end. When using a Binding expression those are not needed.
<input id="sessionInput" type="hidden" value='<%# Session["UserLogin"] != null ? Session["UserLogin"].ToString() : "" %>' />
And you have to call DataBind(); in the Page Load if you want the values to be bound to the Input.
Use the newer : which html encodes the value.
<input id="sessionInput" type="hidden" value='<%: Session["UserLogin"] %>' />
Using the HTML markup
<form id="form" runat="server">
<input id="donkey" type="text" placeholder="monkey" runat="server" />
</form>
I hoped to get the entered value in code behind by
String s = Request.Form["donkey"];
but it only produces null value. When I investigate the data structure I get something like $ctl00$main$donkey so I know it's there. After a while, I simply switched to
<form id="form" runat="server">
<asp:TextBox id="donkey" type="text" runat="server"></asp:TextBox>
</form>
but I still wonder how to reference the object from server-side if I for some reason won't switch to ASP-component.
If you want to access to the value using request.form, add name attribute to input tag and remove runat attribute.
<input id="donkey" name="donkey" type="text" />
Otherwise use
<asp:TextBox ID="donkey" type="text" runat="server"></asp:TextBox>
and on cs
string s = donkey.Text;
if you want to get value of input use like this
String s = donkey.value;
I'm not sure about ASP.net but for a regular form field to submit properly it should have a name attribute. That would be the key that you could then lookup the form value.
Just donkey.Value will return the value from text input which should have runat="server". It will create an object of System.Web.UI.HtmlControls.HtmlInputText.
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>
For example in my datalist if Eval("OptionJ").Tostring = Null I would like the function GetVisible to set visibility of the radio button to false like so:
<input name="Q<%#Eval("ID")%>" type="radio" value="J" visible="<%# GetVisible(Eval("OptionJ").ToString()) %>">
<%#Server.HtmlEncode(Eval("OptionJ").ToString())%>
</option><br />
I then have a codebehind function like so:
Protected Function GetVisible(ByVal Evalresult As String) As String
If Evalresult = Nothing Then
Return "False"
Else
Return "True"
End If
End Function
I have also tried checking EvalResult = String.empty
In the outputted html the visible status is being set to false...
<input name="Q3" type="radio" value="J" visible="False">
But it is still displayed on the page!
Please can you let me know how to get this working? Thanks in advance for your time reading and any answers posted.
Try this one:
<input name="Q3" type="radio" value="J" visible="false" runat="server">
Visible property works only for ASP.NET Server control but here you are using Html Input Control.
So one approach is that add runat="server" attribute in this control to if you want to continue with visible property and second one is that add style="visibility:hidden" attribute for HTML input control as given below:
<input name="Q3" type="radio" value="J" style="visibility:hidden">
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".