sidhewsar
sidhewsar

Reputation: 135

call code behind method from javascript

I have a code behind method

protected void RadTreeView1_NodeClick(object sender,Telerik.Web.UI.RadTreeNodeEventArgs e)
{
}

I need to call this This RadTreeView1_NodeClick method from Pageload in javascript What would I do? Please help me!

Upvotes: 0

Views: 590

Answers (3)

speti43
speti43

Reputation: 3046

Use Javascript:

__doPostBack("radtree","args");

You can call on document ready, if you use jquery:

$(function(){
   __doPostBack("radtree","args");
});

Or use body onload, and create a function which executes the __doPostBack.

C#:

protected void Page_Load(object sender,EventArgs e)
{
    if(Request["__EVENTTARGET"]=="radtree")
    {   
       var arguments = Request["__EVENTARGUMENT"]; // this will be "args"
       RadTreeView1_NodeClick(pass parameters);           
    }
}
protected void RadTreeView1_NodeClick(object sender,Telerik.Web.UI.RadTreeNodeEventArgs e)
{
}

Upvotes: 0

Delphi.Boy
Delphi.Boy

Reputation: 1226

C# methods can be called from Javascript, but have to be PageMethods. A PageMethod, is defined just like any method in your code-behind. The only requirement is that, you should add [WebMethod] attribute before method's signature. Then, your C# method can be called with PageMethod object from Javascript. Please note that EnablePageMethod property of the ScriptManager on your page should be set to true.

For more info visit see this example: http://www.singingeels.com/Articles/Using_Page_Methods_in_ASPNET_AJAX.aspx

Regarding your RadTreeview, I believe that it has client API, and you should be able to do whatever you want using it. Please see RadTreeview client examples and look at the source code.

Upvotes: 2

Steven Lemmens
Steven Lemmens

Reputation: 158

I guess triggering the click would be easiest. So I would do something like (using jQuery):

$("#RadTreeView1").click(); or $("#RadTreeView1").trigger('click');

You should replace RadTreeView1 with the ClientID of the button though.

Upvotes: 0

Related Questions