我在 asp.net 页面中使用 ScriptManager。而且我无法从 c# 代码中使用 javascript 代码。我尝试了脚本管理器,更新面板......但没有解决。或者我不能。
我想在代码隐藏中调用 javascript 代码。
代码隐藏:
{
public partial class index : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnGetir_Click1(object sender, EventArgs e)
{
RunCMD();
}
public void RunCMD()
{
if (FileUpload1.HasFiles)
{
foreach (HttpPostedFile btnUpload in FileUpload1.PostedFiles)
{
string ext = Path.GetExtension(btnUpload.FileName);
if (ext == ".tab" || ext == ".TAB" || ext == ".kml" || ext == ".KML")
{
string dosya = btnUpload.FileName;
string cmd = "cmd";
string enter = "/c";
string ad = dosya.Remove(dosya.Length - 4);
string exe =
" ogr2ogr -f \"GeoJSON\" -t_srs WGS84 " +
"C:\\json\\" + ad + ".json " +
"C:\\" + dosya;
try
{
Process p = new Process();
p.StartInfo = new ProcessStartInfo(cmd, enter + exe);
p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
p.Start();
p.WaitForExit();
}
catch (Exception)
{
}
string json = ReadJSON("C:\\json\\" + ad + ".json");
ScriptManager.RegisterStartupScript(
this,
this.GetType(),
"deneme",
"deserialize('" + json + "');",
true);
}
else
{
}
}
}
}
public string ReadJSON(string jsonPath)
{
FileStream fs = new FileStream(jsonPath, FileMode.Open, FileAccess.Read);
StreamReader sr = new StreamReader(fs);
string WillReturn = "";
try
{
WillReturn = sr.ReadToEnd();
return WillReturn;
}
catch (Exception ex)
{
WillReturn = null;
return WillReturn;
}
finally { sr.Close(); fs.Dispose(); }
}
}
}
在javascript代码中:
<script>
function deserialize(json) {
// var element = document.getElementById('text');
var type = document.getElementById("formatType").value;
var features = formats['in'][type].read(json);
var bounds;
if (features) {
if (features.constructor != Array) {
features = [features];
}
for (var i = 0; i < features.length; ++i) {
if (!bounds) {
bounds = features[i].geometry.getBounds();
} else {
bounds.extend(features[i].geometry.getBounds());
}
}
vectors.addFeatures(features);
map.zoomToExtent(bounds);
var plural = (features.length > 1) ? 's' : '';
//element.value = features.length + ' feature' + plural + ' added';
} else {
//element.value = 'Bad input ' + type;
}
}
</script>
在html代码中:
<asp:Button ID="btnGetir" runat="server" OnClick="btnGetir_Click1" Text="Button" />
<asp:FileUpload ID="FileUpload1" runat="server" />
反序列化功能不起作用。我怎样才能成功呢?