简体   繁体   English

如何从javascript调用方法后面的代码?

[英]how can i call a code behind method from javascript?

hello guys i have w method that changes the opacity of an image using on click event and saves this image in a folder. 大家好,我有w方法,该方法使用on事件更改图像的不透明度并将该图像保存在文件夹中。 what im trying to do is that i want to call this method using onmouseup event and preview that img automatically in a div . 我想做的是,我想使用onmouseup事件调用此方法,并在div中自动预览该img。 i have this code: 我有这个代码:

<asp:ScriptManager EnablePartialRendering="true" ID="ScriptManager1" runat="server"></asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional><ContentTemplate>
<input max="10" min="0" name="rangeInput" onmousemove="updateTextInput(this.value);" onmouseup="return showImg()" type="range" runat="server" />
<input id="txtOp" size="3" type="text" value="" runat="server" />
<asp:Button ID="bntChangeOpacity" runat="server" onClick="bntChangeOpacity_Click1" Text="Change Opacity" />
        <div id="placehere"></div>

</ContentTemplate>
</asp:UpdatePanel>

so here i want to replace the bntChangeOpacity_Click1 method to be called onmouseup event 所以在这里我想替换bntChangeOpacity_Click1方法,称为onmouseup事件

the code behind : 后面的代码:

public void bntChangeOpacity_Click1(object sender, EventArgs e)
    {
        string s = txtOp.Value;
        float ss = float.Parse(s);
        float opacityvalue = ss / 10;
        var img = ImageTransparency.ChangeOpacity(Image.FromFile(Server.MapPath("img1.jpg")), opacityvalue);
        img.Save(Server.MapPath("img2.jpg"));

    }

You have to make this as a webmethod innorder to call this from front end;change the method signature as like the following: 您必须将其作为网络方法来从前端调用它;更改方法签名,如下所示:

[WebMethod]
public static void bntChangeOpacity_Click1(object sender, EventArgs e)
{
    string s = txtOp.Value;
    float ss = float.Parse(s);
    float opacityvalue = ss / 10;
    var img = ImageTransparency.ChangeOpacity(Image.FromFile(HttpContext.Current.Server.MapPath("img1.jpg")), opacityvalue);
    img.Save(HttpContext.Current.Server.MapPath("img2.jpg"));

}

You can use PageMethods for that. 您可以PageMethods使用PageMethods Make your method static and decorate it with WebMethod attribute as below 使您的方法静态并使用WebMethod属性装饰它,如下所示

[System.Web.Services.WebMethod]
public static void ChangeOpacity()
    {
        string s = txtOp.Value;
        float ss = float.Parse(s);
        float opacityvalue = ss / 10;
        var img = ImageTransparency.ChangeOpacity(Image.FromFile(Server.MapPath("img1.jpg")), opacityvalue);
        img.Save(Server.MapPath("img2.jpg"));

    }

and call it like function changeOpacity() { PageMethods.ChangeOpacity(); 并像函数changeOpacity()一样调用它{PageMethods.ChangeOpacity(); } }

from javascript. 来自javascript。

you need to change bntChangeOpacity mark up as below. 您需要按以下方式更改bntChangeOpacity标记。

<asp:Button ID="bntChangeOpacity" runat="server" onClientClick="changeOpacity()" Text="Change Opacity" />

Don't forget to use ScriptManager on page. 不要忘记在页面上使用ScriptManager

Please find sample code of that 请找到该示例代码

http://www.codeproject.com/Articles/180355/Calling-a-code-behind-function-from-JavaScript http://www.codeproject.com/Articles/180355/Calling-a-code-behind-function-from-JavaScript

Otherwise you need to do Ajaxcall. 否则,您需要执行Ajaxcall。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM