Sunday, July 17, 2011

Calling Server Function using JavaScript

 

Default.aspx
----------------
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default5.aspx.cs" Inherits="Default5" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:Label ID="lblmsg" runat="server"></asp:Label>
    </div>
    </form>
</body>
</html>
Default.aspx.cs
-------------------
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;
public partial class Default5 : System.Web.UI.Page
{
    public string click()
    {
        lblmsg.Text = "This is function";
        string msg = lblmsg.Text;
       
        ClientScript.RegisterClientScriptBlock(GetType(), "", "<script>alert('" + msg.ToString() + "');</script>");
       
        return msg;
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        StringBuilder sb = new StringBuilder();
        sb.Append("bool i = document.getElementById('<%=btnsubmit.ClientID%').value");
        sb.Append("if(i == true)");
        sb.Append("{");
        sb.Append("'" + click() + "'");
      
        sb.Append("}");
        sb.ToString();
        ClientScript.RegisterClientScriptBlock(GetType(), "clientscript", "<script>alert('" + click() + "');</script>");
    }
}

functio

0 comments:

Post a Comment