<asp:TextBox ID="txtDate" runat="server" Value="<%= DateTime.Today.ToShortDateString() %>" />
Value="<%= DateTime.Today.ToShortDateString() %>" does not write date in txt field but whole string. What i am doing wrong?
using JavaScript and jQuery:
var now = new Date();
$('#txtDate').text(now.getDate() + '/' + now.getMonth()+ '/' + now.getYear());
or plain JavaScript:
var now = new Date();
document.getElementById('txtDate').value = now.getDate() + '/' + now.getMonth()+ '/' + now.getYear();
or in markup (using System.Web.UI.WebControls.TextBox.Text property, it has no Value property):
<asp:TextBox ID="txtDate" runat="server" Text="<%# DateTime.Today.ToShortDateString() %>" />
and after that call this.DataBind(); or not for page, but your TextBox's parent control.
See this similar question.
As you've seen, you can't use the <%= %> construct to set a property of a server control.
The usual way to set a property in markup is to use a <%# data-binding expression %>
Related
I'm trying to redirect 2 parameters:
<asp:HyperLink runat="server" id="h1" NavigateUrl='OrderFromSupplies.aspx?Gender=<%#Eval("Gender") %>&<%#Eval("Name")%>'><%#Eval("Name") %></asp:HyperLink><br/>
And that is the url I get:
http://localhost:31397/user/admin/OrderFromSupplies.aspx?Gender=%3C%#Eval(%22Gender%22)%20%%3E&%3C%#Eval(%22Name%22)%%3E
How can I do this?
You have to at least separate/isolate the code Eval() from the markup.
<asp:HyperLink runat="server" id="h1"
NavigateUrl='OrderFromSupplies.aspx?Gender=' +
<%#Eval("Gender")%>
+ '&' +
<%#Eval("Name")%>>
<%#Eval("Name")%>
</asp:HyperLink>
I am building a quick web app to display locations markers on google maps from sql server database. On my, aspx page I have a repeater control that displays the lat long of the locations in the database.
<asp:Repeater runat="server" ID="rptMarkers">
<ItemTemplate>
title: '<%# Eval("LocationName")%>'<br />
lat : '<%# Eval("Latitude") %>' <br />
long : '<%# Eval("Longitude") %>
</ItemTemplate>
<SeparatorTemplate>
,
</SeparatorTemplate>
</asp:Repeater>
Now what I need guidance with is , how to pass the values returned in both
'<%# Eval("Latitude")%>' and <%# Eval("Latitude") %> into a javascript array. So that I can use the values in the javascript array to build map markers. I know how to display the markers in google map with when I get the javascript lat long arrays. Only using the values from the repeater to build the javascript array is my challenge
I'm Just modifying your code .
take data in label control , like this
long : <asp:Label ID="lblLongitude" runat="server" Text='<%#Eval("Longitude") %>'></asp:Label><br />
lat : <asp:Label ID="lblLatitude" runat="server" Text='<%#Eval("Latitude") %>'></asp:Label><br />
then get all data in Js
$(document).ready(function () {
var Longitude= [];
var Latitude= [];
$('*[id^=rptMarkers_lblLongitude]').each(function () {
Longitude.push($(this).html());
});
$('*[id^=rptMarkers_lblLatitude]').each(function () {
Latitude.push($(this).html());
});
});
How are you binding data to the repeater? I guess you have some kind of data source. Try this:
<script type="text\javascript">
var location = [];
<% foreach(var elm in dataSource) %>
<%{ Response.Write("location.push({'lat': " + elm.Latitude + " , 'long': " + elm.Longitude + "});"); }%>
If you have to use a repeater, change the code like this:
<script type="text\javascript">
var location = [];
</script>
<asp:Repeater runat="server" ID="rptMarkers">
<ItemTemplate>
title: '<%# Eval("LocationName")%>'<br />
lat : '<%# Eval("Latitude") %>' <br />
long : '<%# Eval("Longitude") %>'
<script type="text\javascript">
location.push({'lat': '<%# Eval("Latitude") %>', 'long': '<%# Eval("Longitude") %>'});
</script>
</ItemTemplate>
<SeparatorTemplate>
,
</SeparatorTemplate>
Don't use a repeater. Create an anonymous array of values then use the JavaScriptSerializer to create the array.
say your dataset is called data & contains objects with properties as you've used above.
var j = new JavaScriptSeializer()
var result = j.Serialize(data.Select(i=>new { title=i.LocationName, lat=i.Latitude, lon=i.Longitude }));
Then in your JavaScript code block
var location = <%=result%>;
Even better would be to use the JSON.Net serializer (site, nuget) but that might be overkill for one small function
I have a repeater that builds a dropdown menu. There is a field that places the URL in the value attribute. The field is nullable in the database, so for items that do not have a URL the value is empty. I need to replace that with something even if it is just '#' so that the validation works.
Mark-Up
<ItemTemplate>
<option data-siteid="<%# DataBinder.Eval(Container.DataItem, "SiteID") %>" value="<%# DataBinder.Eval(Container.DataItem, "URL") %>">
<%# DataBinder.Eval(Container.DataItem, "Name") %>
</option>
</ItemTemplate>
Code Behind:
private void BindMakeList()
{
var makeList = this.repository.GetMakes();
rptDropDown.DataSource = makeList;
rptDropDown.DataBind();
}
How about:
value="<%# DataBinder.Eval(Container.DataItem, "URL") ?? "#" %>"
Try using String.IsNullOrEmpty in Value field
<%# String.IsNullOrEmpty(DataBinder.Eval(Container.DataItem, "SiteID")) ? "#": DataBinder.Eval(Container.DataItem, "SiteID") %>
I'm trying to turn on/off various RequiredFieldValidator controls when checkboxes are checked/unchecked, based on this question. But rather than having a separate js function for each checkbox I want to pass in the ClientID of the input to validate, something like this (only one INPUT here but you can see once it's working I can add more INPUTs without more js):
<asp:TextBox ID="txtSubject" runat="server" />
<asp:CheckBox ID="chkSubjectRequired" runat="server"
OnClick="updateValidator('<%= rfvSubject.ClientID %>');" />
<asp:RequiredFieldValidator ID="rfvSubject" ControlToValidate="txtSubject"
ErrorMessage="You must enter a subject." runat="server" />
<script>
function updateValidator(inputId) {
var enableValidator = !event.srcElement.checked;
var theInput = document.getElementById(inputId);
ValidatorEnable(theInput, enableValidator);
}
</script>
Currently that scriptlet txtSubject.ClientID isn't evaluated, just output directly. I'm sure this is simple but I just don't know the appropriate syntax.
How about adding it via the codebehind (or a script section):
checkSubjectRequired.Attributes.Add("onclick", "updateValidator(" +
txtSubject.ClientID + ")");
This explaination of ClientID may be helpful.
This is because; ASP.NET parser can not parse server tag "<% = %>" for a server side control (i.e. control made as runat='server').
Use the following:
<asp:TextBox ID="txtSubject" runat="server" />
<asp:CheckBox ID="chkSubjectRequired" runat="server"
OnClick="updateValidator('<%#txtSubject.ClientID %>');" />
<asp:RequiredFieldValidator ID="rfvSubject" ControlToValidate="txtSubject"
ErrorMessage="You must enter a subject." runat="server" />
<script>
function updateValidator(inputId) {
var enableValidator = !event.srcElement.checked;
var theInput = document.getElementById(inputId);
ValidatorEnable(theInput, enableValidator);
}
</script>
You could just do this:
<asp:TextBox ID="txtSubject" runat="server" />
<asp:CheckBox ID="chkSubjectRequired" runat="server"
OnClientClick="updateValidator(this.id);" />
<asp:RequiredFieldValidator ID="rfvSubject" ControlToValidate="txtSubject"
ErrorMessage="You must enter a subject." runat="server" />
<script>
function updateValidator(inputId) {
var enableValidator = !event.srcElement.checked;
var theInput = document.getElementById(inputId);
ValidatorEnable(theInput, enableValidator);
}
</script>
I have few controls on asp.net web form page and i want to link a code behind function to Hyperlink to resolve URL
HTML & Code Behind Example
<asp:HyperLink ID="hyplnkVideo" runat="server" NavigateUrl='<%# this.getVideoPageURL()%>'>
<div id="dAlbumCategory" class="AlbumCategoryIcon">
<asp:Image ID="Image1" ImageUrl='~/Images/gallery/Videos.png' runat="server" />
</div>
</asp:HyperLink>
protected String getVideoPageURL()
{
string url;
int PageID = Helper.GetPageIDbyName("Videos.aspx", Request["Language"]);
url = "~/en/Videos.aspx?PageID=" + PageID + "&Language=" + Request["Language"];
return url;
}
This Hyperlink control is not inside any grid view or repeater control. I tried several way but for some reason it doesn't call the function.
I would appreciate help in this regard
try in this way...
<% getVideoPageURL(); %>
<asp:HyperLink ID="hyplnkVideo" runat="server">
<div id="dAlbumCategory" class="AlbumCategoryIcon">
<asp:Image ID="Image1" ImageUrl='~/Images/gallery/Videos.png' runat="server" />
</div>
protected void getVideoPageURL()
{
string url;
int PageID = Helper.GetPageIDbyName("Videos.aspx", Request["Language"]);
url = "~/en/Videos.aspx?PageID=" + PageID + "&Language=" + Request["Language"];
hyplnkVideo.Attributes.Add("href", url);
}
This is running code.. so u can try it
Simply try this:
NavigateUrl="<%= getVideoPageURL() %>"
The appropriate tag would be <%=, not <%#, as the <%# and %> tags indicate that you want to run data binding code (as in, something you've returned from the database).
It has been a while since this question was asked and marked as resolved.
However for the sake of providing a more appropriate resolution please call DataBind on your page code-behind in page load or render.This will inject the necessary href string.