开发者

How can i find which anchor has been clicked for download in destination page with out using query string

开发者 https://www.devze.com 2023-01-31 21:44 出处:网络
I am having a web form initially which have href as follows <a href=\"downloadInfo.aspx\">ACH File Management System Trail(msi)</a>

I am having a web form initially which have href as follows

   <a href="downloadInfo.aspx">ACH File Management System Trail(msi)</a>
   <a href="downloadInfo.aspx">ACH File Management System Trail(zip)</a>

These are my two anchor tags when i click on this i will redirect both to a same page where user has to fill details and a mail will be send to the user for the given mail id. Wh开发者_StackOverflow社区en the user clicks on mail i would like to have the download for which he opted to download. If msi means i would like to prompt msi file to be downloaded and if zip it should be downloaded

I need this to be worked with out using query-string


You'll need JavaScript. First, on the choose page, add an id attribute to each <a> whose value would tell you what file to download. Then use JavaScript (jQuery does this easily) to hijack the anchor tags:

preventDefault() on an <a> tag

The hijack is an onclick event on each anchor tag that tells the client to not following the href. Instead it reads the anchor's id and href values and either:

  1. sets the id as a cookie and then sets the window.location to the anchror's href OR
  2. posts the id value as a form field to the anchor's href

The receiving page reads the cookie or form field, respectively, to determine what file to serve for download.

Update

<!DOCTYPE html>
<html>
<head>
  <script src="http://code.jquery.com/jquery-1.4.4.js"></script>
</head>
<body>
    <a id="ach-msi" href="downloadInfo.aspx">ACH File Management System Trail(msi)</a><br/>
    <a id="ach-zip" href="downloadInfo.aspx">ACH File Management System Trail(zip)</a>
    <script>
    $("a").click(function(event) {
        event.preventDefault();
        var id = $(this).attr('id');
        var href = $(this).attr('href');
        /* this uses the cookie option */
        document.cookie = 'download-file=' + id;
        window.location = href;
    });
    </script>
</body>
</html>


I don't think this is going to be possible, or at least reasonably feasible, without making use of the query string. At the very least, it's likely going to be very browser-dependant. The form elements in question aren't actively "doing" anything on the "form", they're just links rendered by the browser. Clicking on them does nothing more than inform the browser that it should load the URL in the href. So all of the information you need is going to have to be in that URL.

Why the aversion to using query string parameters? This is precisely their purpose, passing a behavior flag to a dynamic resource (page).

Edit: One idea that may work within your requirements it to replace these HTML anchors with LinkButton server controls. Then each can have its own event handler on the postback which can maintain state (which one was clicked) server-side and do a Response.Redirect (or perhaps even Server.Transfer) to the desired destination.

Conversely, you could write a JavaScript function that performs a POST to the destination page with a form value containing which one was clicked and call that function (passing it the value) in the onClick event for those anchor tags. Then the destination page will just need to read the POST value.

But these are considerably more obtuse than just using a query string value.

0

精彩评论

暂无评论...
验证码 换一张
取 消