0

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.

शेखर
  • 17,412
  • 13
  • 61
  • 117
Jack
  • 7,433
  • 22
  • 63
  • 107

5 Answers5

1

You can't. You can define it in your controller code:

btnPrevious.OnClientClick = "location.href='" + PreviousLink + "'"
iurisilvio
  • 4,868
  • 1
  • 30
  • 36
1

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/";
Kapil Khandelwal
  • 15,958
  • 2
  • 45
  • 52
1

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?

Community
  • 1
  • 1
0

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();"/>
dan radu
  • 2,772
  • 4
  • 18
  • 23
0

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.

शेखर
  • 17,412
  • 13
  • 61
  • 117