Mathias
Mathias

Reputation: 119

Update object on masterpage through ajax webmethod

How can i update a repeater on my masterpage, through an ajax webmethod? I'm having trouble finding the masterpage in my webmethod.

Edit: Is there a better way to do this? Basicly i want to update the repeater after running my ajax webmethod login script.

MasterPage Jquery:

<script type="text/javascript">
$(document).ready(function () {
    $('#btn_logout').click(function () {
        $.ajax({
            type: "POST",
            url: "Webmethods.aspx/logIn",
            data: '{username: "' + username + '", password: "' + password + '"}',
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (response) {
                // UPDATE REPEATER DATA
            }
        }
    }
</script>

MasterPage Repeater:

<asp:Repeater ID="Repeater_Menu" runat="server">
<ItemTemplate>
</itemTemplate>
</asp:Repeater>

Edit: Not a single answer to this relatively simple question?

Upvotes: 1

Views: 462

Answers (1)

Ann L.
Ann L.

Reputation: 13965

Unless things have changed significantly, WebMethods are static methods. So they will not have access to any properties or fields of the page they are part of - and that includes the objects on the master page.

Another obstacle to this is that things done with AJAX on the server do not affect the HTML that has already been rendered to the client. To change the appearance on the client, you will need to use javascript (presumably jQuery).

What you might be able to do is this:

  1. Create a Repeater control in code
  2. Create a template programmatically (quite do-able, but possibly complex)
  3. Bind it to your data source
  4. Render it to a string
  5. Send the rendered HTML back down to the client
  6. Replace the previous rendering of the Repeater with the new HTML.
  7. Re-create any client-side event handling you might be doing.

Upvotes: 1

Related Questions