Call C# code-behind method with a <li> onclick
I got a li - list, one with an onclick:
<ul class="tabs">
<li><a href="#tab1">Foobar_1</a></li>
<li onclick="doMethod"><a href="#tab2">Foobar_2</a></li>
<li><a href="#tab3">Foobar_3</a></li>
<li><a href="#tab4"&开发者_开发知识库gt;Foobar_4</a></li>
</ul>
Now, the method I want to call when clicked on a tab (the li's), updates an UpdatePanel, so a gridview is shown.
I know it must have something to do with AJAX, but I ain't got a clue how to move on now...
so basically: how to call a c# method using AJAX?
<li runat="server" OnClick="DoMyOnClickCall">....</li>
Then
public void DoMyOnClickCall(object sender, EventArgs e)
{
// sender is the li dom element you'll need to cast it though.
}
To expand: (Update)
sender
is an object that represents the <li>...</li>
in HTML. It's called something like HtmlControl
.
You'll need to cast sender
to that type.
var myLI = (HtmlControl)sender;
// do stuff with `myLI`
Also, you can call the method from client side by:
- declare your method as public static bool
- put attribute [WebMethod] for this method
- Call the method from js
Sample code:
<script language="javascript">
function MyClientFunction() {
var liElement = $get("liElement").value;
PageMethods.doMethod(liElement,OnSuccess, OnFailure);
}
function OnSuccess(result) {
if(result) {
alert("Some error message!");
}
}
function OnFailure(error) {
}
</script>
精彩评论