简体   繁体   中英

aspx page open a hyperlink via a button

I've read a few answers, and tried a bit of different code from them, but haven't been able to solve this. I am a newbie at this and it seems like quite a simple thing to do, apologies if the answer is really obvious, just need it to work really.. So far, I have an aspx page in Visual Studio 2010 / it was created by someone else, and has both JavaScript and cSharp code behind it. What I would like to happen is that, an 'Edit' button appears on the page in a table in the correct place, and as a result of pressing it, another page is called allowing the editing (of contacts - it's an internal website).

I can get the link working OK as a 'normal' hyperlink, using this line of code:

    var editcontact = '<td align = "right" colspan = "40" class = 
"contactBorder"><a href="WebForm1.aspx?supplier_id=' + 
dataRow.supplier_id + '">Open Contact(s) For Edit</a></td>';

edit contact then gets put in a table like structure (maybe it's "form" given the HTML tags):

finishedcontacts += openrow + contactid + contactname + 
contactjobtitle + contactlocation + contacttelephone + 
contactemail+ editcontact + '</form></tr>'; 

That's OK but it's not very 'nice', what I would like is an actual edit button, I can get the button to appear, but the last time I tried it pressing it does nothing:

var editcontact = '<asp:button id = "editImage4" runat="server" 
Text="Click me" 
OnClientClick="window.open(\'WebForm1.aspx\\?supplier_id=\' + dataRow.supplier_id + \', \'WebForm1\');" />';

Any ideas on how to get this working OK from a button?

Here is some other code I tried, which was to separate out the generation of the url from the calling code, but none of them seemed to work:

//var url = string.Format("{0}?supplier_id={1}", "../WebForm1.aspx", dataRow.supplier_id);
//var url = string.Format("{0}?supplier_id={1}", @"../WebForm1.aspx", dataRow.supplier_id);
//var url = 'string.Format("{0} supplier_id={1}", "WebForm1.aspx",'+ dataRow.supplier_id +')';
var url = 'WebForm1.aspx?supplier_id= '+ dataRow.supplier_id +'';
var supplierid1 = dataRow.supplier_id;

Any ideas what be greatly appreciated. This is my first ever post..

如果要使用按钮重定向,请使用 Response.Redirect(url),并且此按钮必须是服务器端控件。

In the end, a colleague helped me out with this. As other posters identified there was some problems with the URL, and especially with the escapes. Anyway below is the code in the unlikely event anyone needs something similar.

buttoncode = '';

var editcontact = '' + buttoncode + '';

And we added a function (which should probably be called a method or whatever): function OpenContactForEdit(supplier_id) { window.location = "WebForm1.aspx?supplier_id=" + supplier_id; }

Comments: This bit is very important: onClick="OpenContactForEdit(\\'' + dataRow.supplier_id + '\\')"

Note exactly how we needed to escape (and 'end escape') the single quotes. I believe that was quite a big part of why, what I was doing was not working.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM