使用代码隐藏中的参数/值调用javascript方法

时间:2011-04-29 01:12:26

标签: javascript asp.net

有没有办法让我从代码隐藏中调用一个带有两个参数/值的javascript方法?请检查以下代码:

private void ddl_Eval_SelectedIndexChanged (Object sender, System.EventArgs e)
{

    dtEval = new DataTable();
    dtEval = data.GetEvaluation2();
    DataView dvEval = dtEval.DefaultView;

    string xmlDoc = String.Empty;

    foreach (DataRowView drvEval in dvEval)
    {
        if (drvEval.Row["EvaluationID"].ToString() == ddl_Eval.SelectedValue.ToString())
        {
            xmlDoc = drvEval.Row["EvalAnswers"].ToString();
        }
    }

    XDocument xdoc = new XDocument();
    xdoc = XDocument.Parse(xmlDoc);

    foreach (XElement child in xdoc.Root.Elements("Answer"))
    {
        //Response.Write(child.Attribute("item").Value + " " + child.Value + "<br />");

        //THIS IS WHERE I NEED TO PUT THE CODE SO I CAN SEND THE VALUE
        //OF THE ATTRIBUTE "item" AND THE VALUE OF THE child node itself.

    }

}

这是XML字符串:

<ANSWERS>
  <Answer item="ddl_3">A</Answer>
  <Answer item="ddl_8">A</Answer>
  <Answer item="ddl_13">Y</Answer>
  <Answer item="ddl_16">Y</Answer>
  <Answer item="ddl_19">Y</Answer>
  <Answer item="ddl_22">N</Answer>
  <Answer item="ddl_26">Y</Answer>
  <Answer item="ddl_30">N</Answer>
  <Answer item="ddl_34">Y</Answer>
  <Answer item="ddl_38">N</Answer>
  <Answer item="ddl_42">StarLight</Answer>
</ANSWERS>

这是我希望javascript更改select控件的选定值的页面。 Screenshot

有可能吗?我想发送这两个参数/值,以便javascript可以更改我在页面上的<select>控件的选定值。

1 个答案:

答案 0 :(得分:0)

更改数据列表

ddl_eval.items.clear()

foreach (XElement child in xdoc.Root.Elements("Answer"))
    {
       ddl_eval.items.add(child.Value,child.Attribute("item").Value);
    }

你不需要JavaScript - 我会像这样编码:

Response.write("<answers>")

foreach (XElement child in xdoc.Root.Elements("Answer"))
    {
       Response.Write(@"<answer item="""+child.Value+@""">"+child.Attribute("item").Value+"</answer>");
    }

Response.Write("</answers>");

另外,现代html风格是将所有标签都设置为小写(xml需要它。)


您应该使用this.Page.RegisterClientScriptBlock(key, script);

它可能看起来像这样:

 this.Page.RegisterClientScriptBlock("some unique name",@"
<script language=""JavaScript"">
   alert(""Hi, I'm some client script!"");
</script>");