C # code method call with <li> onclick
I have a listing, one with 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">Foobar_4</a></li>
</ul>
Now the method that I want to call when I click on the tab (s) updates the UpdatePanel, so the gridview is displayed.
I know this should have something to do with AJAX, but I don't know how to move on ...
so basically: how to call a C # method using AJAX?
+5
2 answers
<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)
senderis an object that represents <li>...</li>in HTML. He called something like HtmlControl.
You need to specify senderfor this type.
var myLI = (HtmlControl)sender;
// do stuff with `myLI`
+7
In addition, you can call the method on the client side:
- declare your method as public static bool
- put attribute [WebMethod] for this method
- Call method from js
Code example:
<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>
+1