1

I am trying to redirect to error page if the Listview is empty. I did try below workaround but doesn't seems to redirect.

protected void lvDetils_ItemDataBound(object sender, ListViewItemEventArgs e)
    {
        if (e.Item.ItemType == ListViewItemType.EmptyItem)
        {
            Response.RedirectToRoutePermanent("NotFound");
        }
    }

What is wrong with this? Is it possible to do like that?

leppie
  • 115,091
  • 17
  • 196
  • 297
CoreLean
  • 2,139
  • 5
  • 24
  • 43
  • does LvDetils.Items.Count==0 work for this purpose? – David Mar 31 '13 at 14:32
  • I have not tried this. But in which event should i use? I am using ObjectDataSource to bind the listview. but what is wrong with my piece of code? – CoreLean Mar 31 '13 at 14:47

2 Answers2

2

try this below code instead of your code

if (e.Item.ItemType == null)

Edit

or try this code

if (lvDetils.Items.Count==0)

New Edit:

But you have another option is .use lvDetils_ItemCreated

protected void lvDetils_ItemCreated(object sender, ListViewItemEventArgs e)
    {
        if (e.Item.ItemType == ListViewItemType.EmptyItem)
        {
            Response.RedirectToRoutePermanent("NotFound");
        }
    }
Ramesh Rajendran
  • 37,412
  • 45
  • 153
  • 234
0

Ok.I have got it done finally with the status code too. Thanks Ramesh Rajendran.

Below is the code.

protected void lvEnglishMovieDetils_ItemCreated(object sender, ListViewItemEventArgs e) 
    {
        if (e.Item.ItemType == ListViewItemType.EmptyItem)
        {
            Response.RedirectToRoutePermanent("NotFound");
        }
    }

And in my 404.aspx i worte below code is below link

public partial class Custom404 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Page.Title = "Page Not Found";
    }

    protected override void Render(HtmlTextWriter writer)
    {
        base.Render(writer);
        Response.StatusCode = 404;
    }
}

Source of information for status code:

how to set 404 status code for the page

Community
  • 1
  • 1
CoreLean
  • 2,139
  • 5
  • 24
  • 43