开发者

Javascript in ASP.NET: Conditionally making href/http request?

I'm really new to ASP.NET and javascript so I'm rather lost on how to do a lot of things. But what I really need to do right now is this:

The website I'm working on has a link that makes makes an http request, and is located inside a grid, as such:

<a  target="_blank" href='<%# Eval ( "ServerURL","{0}Thing.aspx") %>'>

What I need to do is write a javascript function at the top that takes the place of this line, so that I can check another element in the grid, and only make the http request when that other element is of a certain value. I tried rewriting it as such:

<a  target="_blank" href='javascript:Call(<%# Eval ( "ServerURL","{0}Thing.aspx") %>);'>

and the function Call at the top:

<script type="text/javascript">
    function Call (url){
        ret开发者_如何学JAVAurn url;
    }
</script>

But all it does is open a new tab with about:blank for the url. What is it that I should be doing here?


rewrite it to

html

<a  target="_blank" onclick="return Call(this);" href='<%# Eval ( "ServerURL","{0}Thing.aspx") %>'>

javascript

<script type="text/javascript">
    function Call (elem){
        // make your checks here based on the element you clicked (the elem parameter)
        if (checks_passed)
         {
           return true; // when the onclick handler returns true the link action is followed
         }
        return false; // when the onclick handler returns false the action is cancelled
    }
</script>

demo at http://jsfiddle.net/efQ6R/


Try this one:

<a href='javascript:Call("<%# Eval ( "ServerURL","{0}Thing.aspx") %>");'>

note the parentheses inside href.

Then rewrite your JavaScript function:

function Call(url){
    window.open(url, "_blank");
}


You can also solve this within ASP.NET by using the RowDataBound event for GridView (or ItemDataBound event if you are using a DataGrid).

The event is called for each row that is bound to the gridview, and that gives you the ability to change any cells in the row based on the values of another cell, or based on the datakeys for the row, or any criteria you can come up with really.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜