Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

ListView does not cause postback when calling ItemCommand

Posted on 2008-10-29
5
8,322 Views
Last Modified: 2013-11-08
I'm having a problem with rebinding data in a ListView right after OnItemCommand is triggered from a button in the ItemTemplate.  When the button is clicked, I am deleting the data in the row (which successfully occurs) but when I try to rebind the data in the ListView nothing happens until the page is refreshed.

I know that a suggested solution is to have an ObjectDataSource and to just declare what to do upon deletion in there but I need a solution for data binding data in the code-behind.  (Note that I have the delete function working and only need a solution for the page to postback and to repopulate the ListView right after.
<asp:ListView ID="lv_PagePosts" runat="server" ItemPlaceholderID="itemContainer" OnItemCommand="lv_PagePosts_ItemCommand">
            <LayoutTemplate>
                <asp:PlaceHolder ID="itemContainer" runat="server"></asp:PlaceHolder>
            </LayoutTemplate>
            <ItemTemplate>
                
            ......................
 
<asp:LinkButton ID="lb_delete" runat="server" Font-Bold="false" Visible="false" CommandName="Delete">Delete</asp:LinkButton>
                
            </ItemTemplate>
        </asp:ListView>
 
 
 
protected void lv_PagePosts_ItemCommand(object source, ListViewCommandEventArgs e)
    {
 
        if (e.CommandName == "Delete")
        {
            GamingNetworkDBDataContext db = new GamingNetworkDBDataContext();
            Label lbl_postID = (Label)e.Item.FindControl("lbl_postID");
            int postID = Convert.ToInt32(lbl_postID.Text);
 
            var p = from pp in db.PagePosts
                    where pp.postID == postID
                    select pp;
            
            db.PagePosts.DeleteAllOnSubmit(p);
            db.SubmitChanges();
 
            //rebind the data in ListView
            Build_Post_List();
        }
 
    }

Open in new window

0
Comment
Question by:tektician
  • 3
  • 2
5 Comments
 
LVL 10

Expert Comment

by:jinn_hnnl
ID: 22829073
Hi,

It looks fine to me though, the problem might come from Page life circle. You dont have to use object datasource if you want to have full control of your business objects, and bind your ListView upon you dynamical changes.

My suggestion is: Rebind your ListView after every event is done by doing it in PreRender. In simple explanation: Page_Load -> button event -> PreRender

So you dont have to rebind after you delete

Hope this helps

JINN

 

  protected override void OnPreRender(EventArgs e)
    {
        base.OnPreRender(e);
        //rebind the data in ListView
        Build_Post_List(); 
     }

Open in new window

0
 

Author Comment

by:tektician
ID: 22829146
Hi JINN,

Thanks for the reply.

I tried overriding OnPreRender as you suggested by placing the the Build_Post_List() method in there but it didn't work.

What's strange is that when I placed a breakpoint in OnPreRender, it didn't even reach it when I clicked the linkbutton, yet the event for deleting the data was fired (and no postback occurred to update the ListView).
0
 
LVL 10

Expert Comment

by:jinn_hnnl
ID: 22829182
That's strange, because if the deleting event is fired then the postback has occurred (lv_PagePosts_ItemCommand).

First the Page_Load will be executed, then lv_PagePosts_ItemCommand, are you sure this event is fired ?

I have read your code a gain and I think putting this on PreRender is not ideal. Page load is a good place though, so what you did was right. (as you use PlaceHolder to add your contain dynamically)

Just make sure you rebind your List after every postback (outside of your !IsPostBack). Do you use UpdatePanel?

JINN
0
 

Accepted Solution

by:
tektician earned 0 total points
ID: 22829247
Got it!

Thanks for bringing up the UpdatePanel! I tried taking it out and finally my page gave me the following error;

           The ListView 'lv_PagePosts' raised event ItemDeleting which wasn't handled.

I placed an onItemDeleting method and it worked.  Typically this is used to handle an event that a cancellation of the delete method should happen.  Without this method defined, the 'Delete' command will not work.

I guess the fact that I had the ListView wrapped in an UpdatePanel kept the error from being shown in run-time.

Here is the code for future references:

<asp:ListView ID="lv_PagePosts" runat="server" ItemPlaceholderID="itemContainer" OnItemCommand="lv_PagePosts_ItemCommand" OnItemDeleting="lv_PagePosts_ItemDeleting">
            <LayoutTemplate>
                <asp:PlaceHolder ID="itemContainer" runat="server"></asp:PlaceHolder>
            </LayoutTemplate>
            <ItemTemplate>
 
...............................
            
<asp:LinkButton ID="lb_delete" runat="server" Font-Bold="false" Visible="false" CommandName="Delete">Delete</asp:LinkButton>
                
            </ItemTemplate>
        </asp:ListView>
 
 
protected void lv_PagePosts_ItemDeleting(object sender, ListViewDeleteEventArgs e)
    {
        // handle delete cancellation here
    }
 
    protected void lv_PagePosts_ItemCommand(object source, ListViewCommandEventArgs e)
    {
 
        if (e.CommandName == "Delete")
        {
            GamingNetworkDBDataContext db = new GamingNetworkDBDataContext();
            Label lbl_postID = (Label)e.Item.FindControl("lbl_postID");
            int postID = Convert.ToInt32(lbl_postID.Text);
 
            var p = from pp in db.PagePosts
                    where pp.postID == postID
                    select pp;
            
            db.PagePosts.DeleteAllOnSubmit(p);
            db.SubmitChanges();
 
            Build_Post_List();
        }
 
    }

Open in new window

0
 
LVL 10

Assisted Solution

by:jinn_hnnl
jinn_hnnl earned 500 total points
ID: 22829259
Yeah,

That what I was wondering too, when you have UpdatePanel, something will not showup when it's rendered back to the client as the partial page refresh, even the data has been changed or something has happened in the background.

I was kind of asking if you do use UP, make sure you have the right thing rendered and refresh accordingly

Glad that you worked it out

JINN
0

Featured Post

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question