[WebMethod]
public string LoadLayout()
{
try
{
List<XmlTag> lstXmlTags = new List<XmlTag>();
lstXmlTags = parser.GetXmlTags(filePath);
string script = "<script type=\"text/javascript\">alert(lstXmlTags );</script>";
System.Web.UI.ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "test", script, true);
}
catch (Exception ex)
{
throw ex;
}
}
I want to alert lstXmlTags for test purpose.Above code run locally but in server its get a problem.I think in server its doesn’t able to get filepath.So I want to alert what lstXmlTags returns or is it empty.So I add RegisterClientScriptBlock for showing result.But what I try gives me syntax error.I am not able to use RegisterClientScriptBlock.Thanks.
You seem to be mixing client side variables with server side. Here’s one way you could achieve that:
Things to notice here:
JavaScriptSerializerclass) to make sure that the server side variablelstXmlTagsis properly encoded before passing it to a cilent side function<script type="text/javascript">tags because you have passed true as last parameter of theRegisterClientScriptBlockfunction which means (I quote from the documentation): A Boolean value indicating whether to add script tags. So if you set this to true the method will already add those tags for you and you should not be manually adding them once again.JSON.stringifymethod inside the alert function. This method is natively built-into all modern browsers and allows to convert a javascript variable definition into a string so that you get a friendly display. But of course you could have directly used the variable if you wanted to:alert({0}[1].SomeProperty);will display some property of the second element in your collection.Remark: For debugging purposes it’s far better to use
console.logand a javascript debugging tool such as FireBug instead of alerting around with some modal windows.