日期:2014-05-18  浏览次数:20423 次

js怎样调用C#里面的函数?
我的aspx页面里面写了js,想要调用c#里面的一个函数,但是总是出错。请大家帮帮我

JS:

 <script>
function opendialog()
{
  var someValue=window.showModalDialog ("cate.aspx","","dialogWidth=800px;dialogHeight=800px;status=no;help=no;scrollbars=yes")
  PageMethods.BindToDropList();
}
 </script>

aspx:

  <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true"> </asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
  <ContentTemplate>
  <asp:DropDownList Style="z-index: 0" ID="dropCate" runat="server" OnSelectedIndexChanged="dropCate_SelectedIndexChanged" AutoPostBack="True">
  </asp:DropDownList>
  <asp:DropDownList Style="z-index: 0" ID="dropCate1" runat="server">
  </asp:DropDownList>
  </ContentTemplate>
  </asp:UpdatePanel>
  </td>
  <td align="left">
  <input id="Button1" type="button" value="全部招聘岗位" onclick="opendialog()" />
  </td>

c#:
private void BindToDropList()
{
string strConn = ConfigurationSettings.AppSettings["connectionstring"];
SqlConnection con=new SqlConnection(strConn);
String strSQL="select * from Company_Post_Sort";
SqlCommand cmdCate = new SqlCommand(strSQL, con);
con.Open();
SqlDataReader dtr = cmdCate.ExecuteReader();
this.dropCate.DataSource = dtr;
this.dropCate.DataTextField = "sort";
this.dropCate.DataValueField = "id";
this.dropCate.DataBind();
  dropCate.Items.Insert(0, new ListItem("----请选择---", "0"));
  dropCate.SelectedIndex = 1;
dtr.Close();
string strClass = "select * from Company_Post where sort_id = " +this.dropCate.SelectedValue;
SqlCommand cmdClass = new SqlCommand(strClass, con);
dtr = cmdClass.ExecuteReader();
this.dropCate1.DataSource = dtr;
this.dropCate1.DataTextField = "post";
this.dropCate1.DataValueField = "id";
this.dropCate1.DataBind();
  dropCate1.Items.Insert(0, new ListItem("----请选择---", "0"));
dropCate1.SelectedIndex = 1;
dtr.Close();
con.Close();

}

------解决方案--------------------
把C# code 写道webservice中,ajax调用

可以用jQuery ajax 简单
也可以用微软自己的机制,scriptmanager
不懂就去网上搜相关知识就可以了