Last post May 11, 2020 02:18 AM by Sean Fang
May 10, 2020 12:46 AM|ben2203|LINK
Simple question, I am trying to pass the current date as a querystring to a hyperlink outside of the gridcontrol on the page, how is this achieved?
For use within the grid I can use the following code find, however of course there are localization issues and the date isn't forming properly however that will be my next question.
<asp:HyperLink ID="hyperlink2" Text="loaded" NavigateUrl='<%# "loaded.aspx?loaddate=" + Server.UrlEncode(DateTime.Now.ToString("MM/dd/yyyy")) %>' runat="server" />
I am pretty amateur with asp.net so bear with me.
Many thanks in advance
May 11, 2020 02:18 AM|Sean Fang|LINK
For <%# syntax, it will be evaluated when the server control is
doing data-binding. That way, if you want to pass current date to hyperlink through <%# syntax, you should
explicitly call the method "DataBind()" for that HyperLink server control.
Another option is to use HTML tags. As you may know, you can not just use the
<%= syntax in the server control to call the function. Server control tags act not as HTML tags. Server tags are evaluated and created on the server long before <%= code on the page is evaluated. However, if you are directly
using HTML tags, you could pass the current date via <%= syntax.
Moreover, you should not use "Server.UrlEncode()" here since it will
encode the content which might make the
content not displaying correctly.
More details, you could refer to below codes.
<form id="form1" runat="server">
<asp:HyperLink ID="hyperlink2" Text="loaded via Server control" NavigateUrl='<%# "HyperLinkDateTimeNow.aspx?loaddate=" + DateTime.Now.ToString("MM/dd/yyyy")%>' runat="server" />
<a id="hyperlink3" href='HyperLinkDateTimeNow.aspx?loaddate=<%= DateTime.Now.ToString("MM/dd/yyyy") %>'>loaded via HTML tag</a>
<asp:Label ID="ForDisplayDate" runat="server" Text="Current Date is: "></asp:Label>
<asp:Label ID="DisplayDate" runat="server"></asp:Label>
<asp:Button ID="ClearBtn" runat="server" OnClick="ClearBtn_Click" Text="Clear" />
protected void Page_Load(object sender, EventArgs e)
if (Request.QueryString["loaddate"] != null)
DisplayDate.Text = Request.QueryString["loaddate"];
protected void ClearBtn_Click(object sender, EventArgs e)
DisplayDate.Text = "";
Hope this can help you.