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
精彩评论