locked
passing current date to hyperlink RRS feed

  • Question

  • User-1212847090 posted

    Hi There, 

    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

    Sunday, May 10, 2020 12:46 AM

All replies

  • User-1330468790 posted

    Hi ben2203,

     

    Using <%# Syntax

    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.

     

    Using <%= Syntax

    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.

    .aspx Page:

     <form id="form1" runat="server">
            <div>
                <asp:HyperLink ID="hyperlink2" Text="loaded via Server control" NavigateUrl='<%# "HyperLinkDateTimeNow.aspx?loaddate=" + DateTime.Now.ToString("MM/dd/yyyy")%>' runat="server" />
                <br />
                <a id="hyperlink3" href='HyperLinkDateTimeNow.aspx?loaddate=<%= DateTime.Now.ToString("MM/dd/yyyy") %>'>loaded via HTML tag</a>
            </div>
            <div>
                <asp:Label ID="ForDisplayDate" runat="server" Text="Current Date is: "></asp:Label>
                <asp:Label ID="DisplayDate" runat="server"></asp:Label>
                <br />
                <asp:Button ID="ClearBtn" runat="server" OnClick="ClearBtn_Click" Text="Clear" />
            </div>
        </form>

    Code behind:

     protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    hyperlink2.DataBind();
                }
    
    
                if (Request.QueryString["loaddate"] != null)
                {
                    DisplayDate.Text = Request.QueryString["loaddate"];
                }
    
    
    
            }
    
            protected void ClearBtn_Click(object sender, EventArgs e)
            {
                DisplayDate.Text = "";
            }

    Demo:

     

    Hope this can help you.

    Best regards,

    Sean

    Monday, May 11, 2020 2:18 AM