How do I fire a server side button click event from javascript?

Last post 05-13-2008 2:45 PM by NC01. 4 replies.

Sort Posts:

  • How do I fire a server side button click event from javascript?

    05-13-2008, 9:56 AM
    • Loading...
    • joynee
    • Joined on 02-14-2008, 3:26 PM
    • Posts 116

    here is my server side event.plz can any one help me how to call this function using javascript. 

     

    protected
    void update_week(object sender, EventArgs e)

    {

    DataSet month_dataset = (DataSet)Session["month_dataset"];

    LinkButton b = (LinkButton)sender;

    int week = Convert.ToInt16(b.Text.Substring(4,1));

    GridView2.Visible = true;

    GridView3.Visible = true;ETA_funtions.etaclass obj = new ETA_funtions.etaclass();

    weeknum = week;

    Hashtable hshtable = obj.get_strt_end_dates(weeknum, Convert.ToDateTime(monthlbl.Text));

    TimeSheet.wkenddt = (int)hshtable["wkendate"];

    TimeSheet.wkstdt = (int)hshtable["wkstdate"];

    TimeSheet.noofdays = (wkenddt - wkstdt) + 1;fstdayofmonth = obj.getfirstday(Convert.ToDateTime(monthlbl.Text));

    bind_gridviews(weeknum);

    update.Visible =
    true;

    set_all_weeks_white();

    b.BackColor = System.Drawing.Color.White;

    }

  • Re: How do I fire a server side button click event from javascript?

    05-13-2008, 10:16 AM
    • Loading...
    • pixelsyndicate
    • Joined on 07-04-2003, 12:56 PM
    • W. MI transplant in N. TX
    • Posts 1,082

    Using the AJAX ScriptManager will help you do this. It will provide a PageMethod class from JavaScript which will allow you to call your STATIC WEBMETHODs in your codebehind.

    See the example @ http://www.geekzilla.co.uk/View30F417D1-8E5B-4C03-99EB-379F167F26B6.htm

     

    "A common mistake that people make when trying to design something completely foolproof was to underestimate the ingenuity of complete fools." ~ Douglas Adams

    http://pixelsyndicate.com/ps/
  • Re: How do I fire a server side button click event from javascript?

    05-13-2008, 10:26 AM
    • Loading...
    • joynee
    • Joined on 02-14-2008, 3:26 PM
    • Posts 116

    thanx for reading my post.i understood your reply. but is there any other way to do this.

  • Re: How do I fire a server side button click event from javascript?

    05-13-2008, 11:43 AM
    • Loading...
    • pixelsyndicate
    • Joined on 07-04-2003, 12:56 PM
    • W. MI transplant in N. TX
    • Posts 1,082

    joynee:

    thanx for reading my post.i understood your reply. but is there any other way to do this.

    not that I am aware of.  Javascript tends to be used on the clientside of the application (browser) and doesn't 'see' server-side methods without some AJAX process. 

    "A common mistake that people make when trying to design something completely foolproof was to underestimate the ingenuity of complete fools." ~ Douglas Adams

    http://pixelsyndicate.com/ps/
  • Re: How do I fire a server side button click event from javascript?

    05-13-2008, 2:45 PM
    Answer
    • Loading...
    • NC01
    • Joined on 08-26-2005, 7:33 PM
    • Posts 7,461
    • TrustedFriends-MVPs

    The only ways to call a server-side method from client-side is to do a PostBack or use AJAX. Here are some options for executing a PostBack:


    If update_week is a button event handler you could do this:
    <script type="text/javascript">
    <!--
    function updateWeekClientSide()
    {
     document.getElementById('<%= Button1.ClientID %>').click();

     // Or: document.getElementById('<%= Button1.ClientID %>').fireEvent('onclick');
    }
    // -->
    </script>

    If not you could do this.
    <script type="text/javascript">
    <!--
    function updateWeekClientSide()
    {
     <%= GetPostBackEventReference(this, "UpdateWeek") %>;
    }
    // -->
    </script>

    and in the CodeBehind:
    private void Page_Load(object sender, System.EventArgs e)
    {
     if ( this.IsPostBack )
     {
      // Place any code that needs to be executed ONLY on a post-back here.
      
      string eventArgument = (this.Request["__EVENTARGUMENT"] == null) ? string.Empty : this.Request["__EVENTARGUMENT"];
      if ( eventArgument == "UpdateWeek" )
       update_week(null, EventArgs.Empty);
     }
    }

    NC...

Page 1 of 1 (5 items)