Thursday, October 18, 2012

ASP.NET : Update Data via JavaScript using ICallback

This sample will show how to update data via JavaScript.
Three Step to Show you

Step 1 : Implement Interface ICallbackEventHandler.
Step 2 : Declare public string to get GetCallbackEventReference.
Step 3 : Call it by assign to OnClientClick Button.

Code behine

public partial class ICallback_CS : System.Web.UI.Page,ICallbackEventHandler
{
    public string CallbackScript = string.Empty;
    private string CallbackArg = string.Empty;

    protected void Page_Load(object sender, EventArgs e)
    {
        CallbackScript = Page.ClientScript.GetCallbackEventReference(Page, "arg", "onSuccessCallback", "context","onErrorCallback",false);
    }

    public string GetCallbackResult()
    {
        return string.Format("Update {0} Complete.", CallbackArg);
    }

    public void RaiseCallbackEvent(string eventArgument)
    {
        CallbackArg = eventArgument;
    }
}

HTML

<body>
    <form id="form1" runat="server">
    <div >
        <asp:TextBox ID="txt1" runat="server"></asp:TextBox>
        <br />
        <asp:Button ID="btCallback" runat="server" Text="UpdateData" OnClientClick="GetString(); return false;" />
    </div>
    </form>
</body>
<script type="text/javascript">
    function GetString() {
        var arg = $("#txt1").val();
        var context = "";
        <%=CallbackScript %>;
    }
    function onSuccessCallback(e){
        alert(e);
    }
    function onErrorCallback(e){
        alert(e);
    }
</script>

No comments:

Post a Comment