0

In my WebApplication I have some code like this.

$('.form-content').append('<ul>' + 
    '<li runat="server" OnClick="SelectUser" value="1">User 1</li>' +
    '<li runat="server" OnClick="SelectUser" value="2">User 2</li>' +
    '<li runat="server" OnClick="SelectUser" value="3">User 3</li>' +
'</ul>'); 

But, when I select the list item, the Event doesn't trigger. Is there any way to bind ASP.NET event to html control which is dynamically generated by JQuery/Javascript ?

I tried using WebMethod. But, It cannot update the ASP.Net controls since the method is static.

3
  • You can try with WebMethod and call that WebMethod from $.ajax and obviously that $.ajax can be called for any HTML control.. :) Commented Apr 14, 2015 at 9:35
  • Tell me one thing! SelectUser is a method on serverside?? Commented Apr 14, 2015 at 9:38
  • Yes... Its a server method. WebMethod doesn't help me, as I have lot of textboxes and gridviews to be filled by this function. I cannot set one by one by JQuery AJAX post result. WebMethod on the same page doesn't allow me to directly access the controls; Commented Apr 14, 2015 at 10:07

2 Answers 2

1

If all else fails you can try this, add hidden asp:button on your page and make sure that you have an onclick event attached to it.

<asp:Button ID="btnSelectUser" runat="server" Text="" OnClick="btnSelectUser_OnClick" />

then in your javascript add click events to your li

$("li").click(function() {    
   $("#btnSelectUser").trigger("click")
});

that should fire the serverside event.

The other thing that you will need to keep in mind is that you will need to place hidden labels on the page to save the selected values of the li

$("li").click(function () {
    $("#hiddenLabel").val("Get value from attr")
    $("#btnSelectUser").trigger("click")
});

then those values should also be available on the server.

This is by now means the best why to-do it, but it will work.

Sign up to request clarification or add additional context in comments.

1 Comment

Hi, Its working... Still I am searching for a better solution. Thanks for the solution :)
1

I have made a fiddle similar to your requirement.

$(document).ready(function () {
    $("#btnSubmit").on("click", function () {
        debugger
        addList();
        return false;
    });

    function addList() {
        $('.form-content').append('<ul>' +
            '<li runat="server"  value="1">User 1</li>' +
            '<li runat="server"  value="2">User 2</li>' +
            '<li runat="server"  value="3">User 3</li>' +

            '</ul>');
    }
    $(".form-content").on("click", "li", function () {
        SelectUser($(this));
    });



function SelectUser(obj) {
    alert("User Selected!");
    $.ajax({
        type: "POST",
        url: "./YourPage.aspx/SelectUser",
        data: ("ID=" + $(obj).prop("id")),
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            alert(textStatus);
        },
        success: function (result) {
            alert("success");
        }
    });
    return false;
}
});

You can do anything inside SelectUser(). Even Invoke the Serverside event.

2 Comments

You can call the SelectUser of server side from ajax. I have updated the code.
Yes. Its possible with an ASP.NET WebMethod. But the problem here is: Since WebMethod is static, It cannot access any of the controls on my page. Hence; It's useless... If I use ajax result to populate the data, Its a heavy task for me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.