Reputation: 7557
I am trying to use <%=%>
in server controls but it isn't working.
This is my code:
<input type="button" runat="server" value="<< Previous" id="btnPrevious"
onclick="location.href='<%=PreviousLink %>'"/>
PreviousLink
is property defined in page.
When I view the page the whole expression is written as it without being evaluated.
Upvotes: 0
Views: 91
Reputation: 17614
What you can do is use an ASP.NET inline expression to set the value during the loading of the page.
First, add a property in the code-behind of your page.
protected string InputValue { get; set; }
In the Page_Load
event, set the value of the property.
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
this.InputValue = "something";
}
}
Finally, add an inline expression in your page markup like this:
<input type="text" name="txt" value="<%= this.InputValue %>" />
This will let you set the value of the input
element without making it a server-side tag.
Upvotes: 0
Reputation: 11
If you remove runat server
attribute then <% %> expression will get evaluated properly.
Check this question
Why will <%= %> expressions as property values on a server-controls lead to a compile errors?
Upvotes: 1
Reputation: 16144
Try this:
Using jQuery:
<input type="button" runat="server" value="<< Previous" id="btnPrevious"/>
<script>
$(function () {
$("#<%=btnPrevious.ClientID%>").on("click", function (event) {
window.location.href = '<%=PreviousLink %>';
});
});
</script>
Using JavaScript:
function onButtonClick() {
window.location.href = '<%=PreviousLink %>';
}
<input type="button" runat="server" value="<< Previous" id="btnPrevious" onclick="onButtonClick();"/>
.aspx.cs (C# code):
public string PreviousLink = "http://stackoverflow.com/";
Upvotes: 1
Reputation: 2782
That should work too:
<script type="text/javascript">
function setLocation() {
location.href = '<%=PreviousLink %>';
}
</script>
<input type="button" runat="server" value="<< Previous" id="btnPrevious" onclick="setLocation();"/>
Upvotes: 0
Reputation: 4987
You can't. You can define it in your controller code:
btnPrevious.OnClientClick = "location.href='" + PreviousLink + "'"
Upvotes: 1