0

I have to create a javascript which contains an url in code behind page using C#. But the url parameter inside javascript doesn't have correct format after generated by C#.

Example:

Url parameter: http://google.com

Javascript: javascript:dnnModal.show('http://google.com',false,365,206,false)

C# code:

string link = "http://google.com?popUp=true";
string googleIcon = "<a href='javascript:dnnModal.show('" + link +',false,365,206,false)'><img border='0' src='~/Icons/gIcon.png'></a>";

After generated from code behind the page view the url incorrect format. There is the code of googleIcon after I am using "View Select Source" to view the code of aspx page:

<a href="javascript:dnnModal.show(" http:="" google.com?popup="true',false,365,206,false)'"><img src="~/Icons/gIcon.png" border="0"></a>

The hyperlink on icon just show this when I move the mouse over it:

javascript:dnnModal.show(

The url is lost and the remind string is lost too.

I need some help on my issue to show the way how to pass an url parameter into javascript using C#.

3 Answers 3

2

Should be like this,

string googleIcon = "<a href=\"javascript:dnnModal.show('" + link + "',false,365,206,false)'\"><img border='0' src='~/Icons/gIcon.png'></a>";
Sign up to request clarification or add additional context in comments.

2 Comments

But the link or url is lost its format after generated on aspx page.
I'm trying, wait a minute.
1

You are not escaping the strings properly

string googleIcon = "<a href='javascript:dnnModal.show(\"" + link +"\",false,365,206,false)'><img border='0' src='~/Icons/gIcon.png'></a>";

Comments

1

I agree with two other answers, but you should try to encapsulate these kind of tasks in a user control maybe. but if that's not possible I suggest to use System.Web.UI.HtmlControls instead, since it will give you more flexibility.
Something like this:

            HtmlLink myHtmlLink = new HtmlLink();
            myHtmlLink.Href = @"javascript:dnnModal.show(\"" + link +"\",false,365,206,false)";
            HtmlImage myImage = new HtmlImage();
            myImage.Src = "~/Icons/gIcon.png";
            myImage.Border = 0;
            myHtmlLink.Controls.Add(myImage);  

I like this approach more because Asp.net is responsible for creating DOM, which means that you will be safe and you're guaranteed to get a valid XHTML result.

1 Comment

You have go as far as my idea. This is very good too, but I am trying to add a javascript with a longer string, and this google icon hyperlink just a specific string inside a bigger string. But your answer is very good for me to cover my research on this issue. Thank too much.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.