开发者

I want a script to show a string that I have declared

How do I have a javascript popup showing a string? I declared a string:

string myString;
myS开发者_如何学编程tring = "hello world!";

protected void Button1_Click(object sender, EventArgs e)
{
    Page.ClientScript.RegisterStartupScript(Page.GetType(), "message",
        "window.alert('myString');", true);
}

But it shows myString instead of hello world!


Un oh you are using 'mystring' as a literal instead of using its Text value. Do it like this:

Page.ClientScript.RegisterStartupScript(Page.GetType(), 
             "message", 
             "window.alert("\'" + myString + "\'");",
             true);


I would recommend you to use String.Format

Page.ClientScript.RegisterStartupScript(Page.GetType(), "message",
    String.Format("window.alert('{0}');", myString),
    true);

String.Format will replace parameters {0}{1}...{n} for the variable in the index passed, more info on MSDN: String.Format Method

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜