href click problem if i add runat=“server” attribute in asp.net

南楼画角 提交于 2019-12-11 19:31:06

问题


here is one href on aspx page

ASPX:

<a id="hrfReport" href="<%=sApplicationURL%>Reports.aspx">Report</a>

CODE BEHIND:

sApplicationURL = "http://localhost/";

now if i click on href page redirected to the reports page but if i add runat="server" attribute then href click not worked.actually i want to make it disable on particular condition.

Does anyone know how to do this?


回答1:


You cannot use the <%=someVal%> construct in server tags. The compiler basically translates that to Response.Write(someVal).

You can use the <%#someVal%> construct, but you are required to DataBind() the value. For example:

ASPX:

<a id="hrfReport" href='<%#sApplicationURL + "Reports.aspx"%>' runat="server">Report</a>

CODE BEHIND:

sApplicationURL = "http://localhost/";
hrfReport.DataBind();

Alternatively...

If you have a server control, you can set its properties from the code behind as follows:

ASPX:

<a id="hrfReport" runat="server">Report</a>

CODE BEHIND:

hrfReport.HRef = sApplicationURL + "Reports.aspx";



回答2:


Put this in your ASPX page.

<asp:HyperLink runat="server" ID="hrfReport" NavigateUrl="~/Reports.aspx" Text="Report"></asp:HyperLink>



回答3:


Build the link and enable/disable it in the code behind:

Sub Page_Load()
  HyperLink1.NavigateUrl = sApplicationURL & "Reports.aspx"
  HyperLink1.Enabled = "false"
End Sub

Then just add your link on the page:

<asp:HyperLink ID="HyperLink1" runat="server">Report</asp:HyperLink>



回答4:


Without using the HyperLink control, you can use a href:

<a href="#" runat="server" id="lnkLogout" onserverclick="lnkLogout_Click"><i class="fa fa-sign-out fa-fw"></i> Logout</a>

Code behind:

Protected Sub lnkLogout_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkLogout.DataBinding
    FormsAuthentication.SignOut()
    HttpContext.Current.Session.Abandon()
    Try
        Response.Redirect("../Login/login.aspx")
    Catch ex1 As ThreadAbortException
        Thread.ResetAbort()
    End Try
End Sub


来源:https://stackoverflow.com/questions/5241285/href-click-problem-if-i-add-runat-server-attribute-in-asp-net

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!