从Javascript调用C#方法 [英] Calling C# Method from Javascript

查看:88
本文介绍了从Javascript调用C#方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的朋友们,



我在页面中使用了一个下拉列表框,但是onSelectedIndexChanges在IE10中不起作用。

所以我有一个放置按钮并调用按钮通过javascript单击相同的功能。

但我有问题。请检查以下代码。



 Javascript 
< script type = text / javascript >
function funcall()
{
<% = Page.ClientScript.GetPostBackEventReference(Button1, String .Empty) %> ;
}
< / script >

代码:
< asp:DropDownList runat = < span class =code-keyword> server ID = cmb_category 姓名 = cmb_category AutoPostBack = true

OnChange = funcall(); >
< asp:ListItem = 文字 = > < / asp:ListItem >
< asp:ListItem = B 文本 = B > < / asp:ListItem >
< asp:ListItem = C 文字 = C > < < span class =code-leadattribute> / asp:ListItem >
< / asp:DropDownList >
&升t; asp:按钮 ID = Button1 runat = server 文字 = 按钮 onclick = Button1_Click / >

代码背后:

protected void Button1_Click(object sender,EventArgs e)
{
if(cmb_category.SelectedValue!=)
{
\\code
}
}





但由于__dopostback未定义,我面临问题。

解决方案



如果您的页面上有 jQuery ,那么它很简单

  function  funcall()
{
var id = ' <%= Button1.ClientID%> ;;


('#< span class =code-string>' + id).click();
}


嘿那里,



首先删除 AutoDostBack =true DropDownList的属性。



然后,你可以使用代码建议的解决方案3中的 tanweer ,或者你也可以试试这个:



  function  funcall()
{
document .getElementById(' <%= Button1.ClientID%>')。click();
}





希望有所帮助。



Azee


Dear Friends,

I have used a dropdownlist box in page but onSelectedIndexChanges is not working in IE10.
So I have a placed button and calling the button click same function via javascript.
But I have from problem. Kindly check the below code.

Javascript 
<script type="text/javascript">
        function funcall()
        {
            <%= Page.ClientScript.GetPostBackEventReference(Button1, String.Empty) %>;
        }
</script>

Code:
<asp:DropDownList runat="server" ID="cmb_category" Name="cmb_category" AutoPostBack="true"

OnChange="funcall();">
<asp:ListItem Value="A" Text="A"></asp:ListItem>
<asp:ListItem Value="B" Text="B"></asp:ListItem>
<asp:ListItem Value="C" Text="C"></asp:ListItem>
</asp:DropDownList>
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />

Code Behind:

protected void Button1_Click(object sender, EventArgs e)
    {
        if (cmb_category.SelectedValue != "")
        {
\\code
}
}



But I am facing problem as __dopostback is undefined.

解决方案

Hi,
If you have jQuery on your page then it is so simple

function funcall()
       {
           var id= '<%= Button1.ClientID%>;


('#'+id).click(); }


Hey there,

first remove AutoPostBack="true" property of the DropDownList.

then, you can use the code suggest by tanweer in solution 3, or you can also try this:

function funcall()
        {
            document.getElementById('<%= Button1.ClientID%>').click();
        }



Hope it helps.

Azee


这篇关于从Javascript调用C#方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆