Link to home
Start Free TrialLog in
Avatar of tektician
tektician

asked on

ListView does not cause postback when calling ItemCommand

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

Avatar of jinn_hnnl
jinn_hnnl
Flag of Netherlands image

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

Avatar of tektician
tektician

ASKER

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).
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
ASKER CERTIFIED SOLUTION
Avatar of tektician
tektician

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial