从一个html按钮的值调用一个c#方法 [英] call a c# method from Value of a html button

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

问题描述

大家好,



i希望从html buton的值调用ac#函数而不添加任何javascript

< input type = < span class =code-string>  button runat =   server id =   test name =   test  value  = ' <%= Resource.Language(_ Description)%>' /> 



但它没有调用我的一个资源类上的根文件夹上的语言函数,但如果我在asp标签中使用相同的文本,就像这样

< asp :标签ID =   test runat =   server><%= Resource.Language( _Description  )%>  



this正在调用我的函数并返回字符串,但同样的事情不在于按钮。



等待您的回复。

谢谢

解决方案

用于从html按钮调用服务器端功能,你需要这样调用。



你需要将该函数设置为static并使用webmethod属性进行标记。



然后在您的设计端编写javascript函数,该函数将触发ajax请求从您的代码后面调用该函数作为你使用jquery / javascript ajax请求从你身边调用任何webservice函数。



你的javascript函数看起来像这样。



< script type =text / javascript>

函数DeleteData(){


.ajax({

类型:POST,

url:Def ault.aspx / DeleteData,

数据:{},

contentType:application / json; charset = utf-8,

dataType:json,

成功:函数(msg){

alert(你的函数调用成功);

//如果你的Codebehind方法返回一些内容,那么将使用msg的.d属性访问该数据或结果

//像这样... < br $>


// alert(你的方法返回数据是......+ msg.d);

},

错误:function(msg){

alert(调用你的函数时出错了。);

}

} );

返回false;

}

< / script>





和你的代码背后的功能......







[WebMethod]

public static void DeleteData()

{

//你的代码

}

hi all,

i want to call a c# function from html buton's value without adding any javascript

<input type="button"  runat="server" id="test" name="test" value='<%=Resource.Language("_Description") %>' />


But its not calling my Language function which is on root folder on one of my Resource Class but if i am using the same text in asp label like this

<asp:Label ID="test" runat="server><%=Resource.Language("_Description") %>


this is calling my function and returns string, but the same thing is not being on button.

Waiting for your kind reply.
thanks

解决方案

for calling server side function from html button you need to call like this.

you need to make that function static and mark that with webmethod attribute.

then write javascript function on your design side that will fire a ajax request to call that function from your code behind as you call any webservice function from your side using jquery/javascript ajax request.

Your javascript function look like this.

<script type="text/javascript">
function DeleteData() {


.ajax({
type: "POST",
url: "Default.aspx/DeleteData",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
alert("Your function called sucessfully");
//if your Codebehind method return something then that data or result will be accessed using .d attribute of msg
//like this...

//alert("Your method return data is ..." + msg.d);
},
error: function (msg) {
alert("There's some error in calling your function.");
}
});
return false;
}
</script>


and your code behind function like this...



[WebMethod]
public static void DeleteData()
{
//your code
}


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

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