sywa08
sywa08

Reputation: 1

Handling click event without postback

I have a listview that populates with data. This listview is inside a user control which sits inside a page called Preferences.aspx. Today I am handling click event on each row meaning that involves posting back to server.

Now, I have to put another user control on Preferences.aspx because there are group of more settings which need to be presented separately to users. I have added the new user control inside a separate tab on the page. This new tab has to be the first one to show when user lands on Preferences.aspx.

Now the problem is that when user goes to second tab (user control with listview) and click on a row, a postback occurs. This puts the user on the first tab (newly added user control).

So I wonder how can I get click event on a row without having to postback?

Any ideas or suggestions are welcome.I am working in Asp.Net with C#.

The code is:

Markup inside the user control:

<asp:TableRow runat="server" id="trVisitor" CssClass='<%# GetRowClass()  %>' >

                   <asp:TableCell ID="tdPicture" runat="server" Width="10" onclick='<%# GetClickPostBack(Container.ItemIndex)  %>'>
                       <div style="margin-top:1px;">
                           <asp:Image ImageUrl=' <%# Page.ResolveUrl("~/" + Eval("Visitor.StatusImageUrl")) %>' visible='<%# historyFlag ? false : true %>' runat="server"/>

                           </div>

                   </asp:TableCell>

... and so on

Markup inside Preferences.aspx:

<ABC:ListControl runat="server" ID="visitorListControl" CanSelect="true" IsMine="true" Recurring="false" OnVisitorSelected="ListControl_VisitorSelected" />

And code behind is:

protected string GetClickPostBack(int itemIndex)
    {
        if (CanSelect)
            //return 0.ToString();
            return "javascript: " + Page.ClientScript.GetPostBackEventReference(this, VisitorRowPrefix + itemIndex) + "; return false;";
        else
            return string.Empty;

    }
    public void RaisePostBackEvent(string eventArgument)
    {
        if (eventArgument.StartsWith(VisitorRowPrefix))
        {
            HandleRowClick(Convert.ToInt32(eventArgument.Substring(VisitorRowPrefix.Length)));
        }
    }

    private void HandleRowClick(int index)
    {
        int CmgVisitorId = Constants.NotConfigured;
     //   bool IsHistoricVisitor = false;
      //  Visitor HistoricVisitor = new Visitor();
        // Mark only the clicked row

... and so on.

Upvotes: 0

Views: 3289

Answers (2)

Volearix
Volearix

Reputation: 1593

If I am understanding the issue properly, try an update panel. Check out the documentation here.

<asp:UpdatePanel runat="server">
    <ContentTemplate>
        <asp:TableRow runat="server" id="trVisitor" CssClass='<%# GetRowClass()  %>' >
           <asp:TableCell ID="tdPicture" runat="server" Width="10" onclick='<%# GetClickPostBack(Container.ItemIndex)  %>'>
               <div style="margin-top:1px;">
                   <asp:Image ImageUrl=' <%# Page.ResolveUrl("~/" + Eval("Visitor.StatusImageUrl")) %>' visible='<%# historyFlag ? false : true %>' runat="server"/>
               </div>
        </asp:TableCell>
    </ContentTemplate>
</asp:UpdatePanel>

Upvotes: 0

Yair Nevet
Yair Nevet

Reputation: 13013

Hook up to the grid button's click event using JavaScript/jQuery, and prevent theirs default behaviour (post-back) by 2 possible means:

  1. return false;
  2. e.preventDefault (jQuery only)

Example (using jQuery):

$('.button').click(function(event){
      event.preventDefault();
      //Write your client-side logic here
});

Description: If this method is called, the default action of the event will not be triggered.

Upvotes: 2

Related Questions