Repeater paging problem

V

Viktor Popov

Hi,

I'm trying to do the following but I can't understand what's wrong. Could
you help me here!
I do paging with a Repeater like this:

DataSet ds = new DataSet();
dad.Fill(ds, "MyOffers");
if(ds.Tables[0].Rows.Count==0)
Delete.Enabled=false;

PagedDataSource objPds = new PagedDataSource();
objPds.DataSource = ds.Tables[0].DefaultView;
objPds.AllowPaging = true;
objPds.PageSize = 14;
int CurPage;
int FirstPage=1;
int LastPage=objPds.PageCount;
if (Request.QueryString["Page"] != null)
CurPage=Convert.ToInt32(Request.QueryString["Page"]);
else
CurPage=1;
objPds.CurrentPageIndex = CurPage-1;
lblCurrentPage.Text = "No?aieoa ?: " + CurPage.ToString() + " io iaui " +
objPds.PageCount.ToString();
if(!objPds.IsFirstPage)
lnkFirst.NavigateUrl=Request.CurrentExecutionFilePath +"?Page=" +
Convert.ToString(FirstPage);
if(!objPds.IsFirstPage)
lnkPrev.NavigateUrl=Request.CurrentExecutionFilePath + "?Page=" +
Convert.ToString(CurPage-1);
if(!objPds.IsLastPage)
lnkNext.NavigateUrl=Request.CurrentExecutionFilePath + "?Page=" +
Convert.ToString(CurPage+1);
if(!objPds.IsLastPage)
lnkLast.NavigateUrl=Request.CurrentExecutionFilePath + "?Page=" +
Convert.ToString(LastPage);
MyRepeater.DataSource=objPds;
MyRepeater.DataBind();

On this page I have a DELETE_BUTTON. When I have more than 14 results(more
than 1 page) and when I push the Button it doesn't work. When I go to the
last page and push the button the code in ON_CLICK_METHOD works. I can't
understand why. Could you help me here?

Thanks a lot!

Viktor
 
V

Viktor Popov

Thanks for the reply!

Here it is. I do loop through the RepeaterItems and check if a CheckBox
control is checked:
private void Delete_Click(object sender, System.EventArgs e)
{
Session["queryStr"]=",";
int f=0;
foreach(RepeaterItem item in MyRepeater.Items)
{
CheckBox cb1 = item.FindControl("cb") as CheckBox;
CheckBox cb2 = item.FindControl("lbl") as CheckBox;
if(cb1.Checked==true)
{
Session["queryStr"]=Session["queryStr"].ToString()+cb2.Text+",";
f++;
}
}
if(f==0)
return;
Response.Redirect("dltad.aspx");
}
If a CheckBox from a row is Ckecked The Session["queryStr"]=the id of the
element which must be deleted and forwards to delete them in onether form.

Thank you!

Viktor
 
V

Viktor Popov

The problem is that when I check a CheckBox in the first page and teh
second(not the last). the button doesn't delete elements. When I check a
CheckBox in the last page, everything works fine. What could be this?

Thank you!
 
R

Robert Koritnik

this really is a nasty codebehind code that's in no way conforming to nice
and clean Asp.Net application. Huh. You are probably having problems because
of the whole web app concept. Actually even this code doesn't clear a thing.

So let's try going step by step.

1. first set a break point on the Session["queryStr"]=","; sentance in your
delete click event handler and debug the whole thing and you'll see what's
going on. Step thru the code and check if the items are there. Maybe you
didn't recreate them on the postback or something. And BTW: why do you have
to redirect to some other page to delete the items?

For start debug the delete click event handler and check if the foreach gets
inside. It shouldn't

If it does send the ASPX portion of the repeater declaration.
 
R

Robert Koritnik

this really is a nasty codebehind code that's in no way conforming to nice
and clean Asp.Net application. Huh. You are probably having problems because
of the whole web app concept. Actually even this code doesn't clear a thing.

So let's try going step by step.

1. first set a break point on the Session["queryStr"]=","; sentance in your
delete click event handler and debug the whole thing and you'll see what's
going on. Step thru the code and check if the items are there. Maybe you
didn't recreate them on the postback or something. And BTW: why do you have
to redirect to some other page to delete the items?

For start debug the delete click event handler and check if the foreach gets
inside. It shouldn't

If it does send the ASPX portion of the repeater declaration.
 
V

Viktor Popov

I don't understand why do you mean that it is a nasty code behind. Could you
explain me in details? And please, if you have a better solution to this
problem, let me know. I started to learn C# 2 months ago and I am not so
good so please help me if you can.
This is a web form in which I use Repeater control to show elements from a
table:
EstateID Description Sender Price SQMeters
=========================================
1 nice house VP $10.000 300m
2 prettygood RK $10.000 400m

I use the second form because when a user wants to delete I'm asking
there:"Are you sure?" If YES -> Delete, if NO-> BACK to the previous.

Thank you
 
V

Viktor Popov

Hi,
The problem is that when I'm on the first/second,etc(not the last) page of
the repeater and I push the Button I can't click it it doesn't move at all.
When I am on the last page and I click the button it moves and deletes what
I have marked.
Here it is the whole code of the page:

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace Estates
{
/// <summary>
/// Summary description for myads.
/// </summary>
public class myads : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label Message1;
protected System.Web.UI.WebControls.Label lgnnam;
protected System.Web.UI.WebControls.LinkButton LinkButton1;
protected System.Web.UI.WebControls.Repeater MyRepeater;
protected System.Web.UI.WebControls.Panel Panel1;
protected System.Web.UI.WebControls.Label lblCurrentPage;
protected System.Web.UI.WebControls.HyperLink lnkFirst;
protected System.Web.UI.WebControls.HyperLink lnkPrev;
protected System.Web.UI.WebControls.HyperLink lnkNext;
protected System.Web.UI.WebControls.HyperLink lnkLast;
protected System.Web.UI.WebControls.Panel Panel3;
protected System.Web.UI.WebControls.Label c;
protected System.Web.UI.WebControls.Button Delete;
protected System.Web.UI.WebControls.Panel Panel2;
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
if (Session["usrName"]==null)
{
Response.Redirect("..\\logon.aspx");
}
if (Session["usrRole"].ToString()=="user")
{
lgnnam.Text=Session["usrName"].ToString();
}
if (Session["usrRole"].ToString()=="admin")
{
Response.Redirect("../admin/admin.aspx");
}
int br=Int32.Parse(Session["cntM"].ToString());
if(br==0)
{
c.Text="("+br.ToString()+")";
}
else
{
c.Text="("+br.ToString()+")";
c.ForeColor=Color.White;
}
Session["queryStr"]=null;
Session["adEdit"]=null;
if(!Page.IsPostBack)
{
SqlConnection conn = new SqlConnection("Data Source=BLEK;Initial
Catalog=Estate; User ID=+++; Password=******");
SqlDataAdapter dad = new SqlDataAdapter("prMYOFFERS", conn);
dad.SelectCommand.CommandType=CommandType.StoredProcedure;
try
{
conn.Open();
dad.SelectCommand.Parameters.Add(new SqlParameter("@USRNAM",
SqlDbType.VarChar,20));
dad.SelectCommand.Parameters["@USRNAM"].Value =
Session["usrName"].ToString();
}
catch(Exception ex)
{
Session["status"]="At the moment you could not see the ads!";
conn.Close();
Response.Redirect("..\\info.aspx");
}
DataSet ds = new DataSet();
dad.Fill(ds, "MyOffers");
if(ds.Tables[0].Rows.Count==0)
Delete.Enabled=false;
Session["dataset"]= ds;
PagedDataSource objPds = new PagedDataSource();
objPds.DataSource = ds.Tables[0].DefaultView;
objPds.AllowPaging = true;
objPds.PageSize = 14;
int CurPage;
int FirstPage=1;
int LastPage=objPds.PageCount;
if (Request.QueryString["Page"] != null)
CurPage=Convert.ToInt32(Request.QueryString["Page"]);
else
CurPage=1;
objPds.CurrentPageIndex = CurPage-1;
lblCurrentPage.Text = "óÔÒÁÎÉÃÁ ?: " + CurPage.ToString() + " ÏÔ ÏÂÝÏ " +
objPds.PageCount.ToString();
if(!objPds.IsFirstPage)
lnkFirst.NavigateUrl=Request.CurrentExecutionFilePath +"?Page=" +
Convert.ToString(FirstPage);
if(!objPds.IsFirstPage)
lnkPrev.NavigateUrl=Request.CurrentExecutionFilePath + "?Page=" +
Convert.ToString(CurPage-1);
if(!objPds.IsLastPage)
lnkNext.NavigateUrl=Request.CurrentExecutionFilePath + "?Page=" +
Convert.ToString(CurPage+1);
if(!objPds.IsLastPage)
lnkLast.NavigateUrl=Request.CurrentExecutionFilePath + "?Page=" +
Convert.ToString(LastPage);
MyRepeater.DataSource=objPds;
MyRepeater.DataBind();
}
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.LinkButton1.Click += new System.EventHandler(this.LinkButton1_Click);
this.Delete.Click += new System.EventHandler(this.Delete_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void LinkButton1_Click(object sender, System.EventArgs e)
{
HttpContext.Current.Cache.Remove(Session["usrName"].ToString());
Session["user"]=null;
Session["usrName"]=null;
FormsAuthentication.SignOut();
Response.Redirect("..\\index.aspx");
}
private void Delete_Click(object sender, System.EventArgs e)
{
Session["queryStr"]=",";
int f=0;
foreach(RepeaterItem item in MyRepeater.Items)
{
CheckBox cb1 = item.FindControl("cb") as CheckBox;
CheckBox cb2 = item.FindControl("lbl") as CheckBox;
if(cb1.Checked==true)
{
Session["queryStr"]=Session["queryStr"].ToString()+cb2.Text+",";
f++;
}
}
if(f==0)
return;
Response.Redirect("dltad.aspx");
}
}
}
 
R

Robert Koritnik

Ok. About your "nasty" code... I didn't mean to offend you. I appologize for
that. Well the way I see your code it looks to me like bad asp.net. or even
better: bad C# code. Like multiple if's with same conditions... Instead:
if (something) blahblah():
if (something) yadayada();

use

if (something)
{
blahblah();
yadayada();
}

that's the first thing...

also overuse of Session collection... Iterating over Repeater items... etc
etc... Not a good code. But you're new you'll learn some things. Ok. Let's
get to my other message with maybe some code... ;)
 
V

Viktor Popov

Thank you for your advice. I am not offended. i just wanted to know what is
wrong. I'll correct the if statements and I'll try to avoid the Session
variables. Before I had done that repeater looping I was using for a while a
delete button on every row.But when a user has to delete hundrets messages
it isn't so useful. Thats why I tried to do it in this way with check boxes
and looping through the Repeater.
I hope that you could help me to find the mistake:) Thank you for the time
you spare for me.
Best regards,

Viktor
 
R

Robert Koritnik

Ok. First about your "authentication". You are somehow triing to do
something that's already there out of the box. User Forms authentication and
you won't have to take care of the user checking on every page. Ok. You
don't have to check for authentication on your pages, because asp.net does
that for you. So when you get to any page that is under security, you may be
shure the user is authenticated. Check some samples in MSDN. Maybe you'll
have to create your own security principal to satisfy your needs.

About checkboxes. The way things should be done: Create checkboxes inside
your repeater but handle event OnCheckedChange. In that handler you take
care of creating that comma separated checkbox texts. Event will fire only
on those that will be changed from false to true. ok. If you change the page
before deleting, the selected ones should stay there. So on creation you
should parse on the serverside that string (make a private property that
saves its value to ViewState instead of Session) and if the text is in it,
check on creation that checkbox.

About delete button. Action events ALWAYS happen AFTER changed events so
your comma separated string property will already be ready when your
delete_click event fires. Ok. How to avoid your two page process? Instead of
client-server back and forth data transfers, you create a LinkButton to
handle this. Ok. Serverside click will fire normally. And when it does just
delete the records that were selected. But IN the codebehind (the best place
would be Page_Load event handler) you add to your linkbutton an attribute to
be like this:

lbDeleteSelected.Attributes.Add("onclick", "javascript: if (!confirm('You
really blahblah...')) window.event.returnValue=false;");

This should do the trick...

Doing it this way, you won't need no Session variable, no iteration thru
items, no GET fields. Things will be faster and more efficient. Code will be
easier to understand and event driven.

Ok. One more hint. There is a possibility when a user presses the F5 button
the whole dela will happen again and the records are already deleted... What
could you do? After deletition, just put a sentance like this:
Response.Redirect(your_page_address)

You can obtain address from Request object, and if I'm not mistaken also
from the Response object. Or you can write it staticly.

One more hint. :) The code you do in your Page_load event... You have two
possibilities. You can inherit the Page class or create a web user control
(UserControl class) that will handle redirection for administrators. Ok. And
don't parse string into int and change it back to string. That makes your
code run slower. Rather use string.Equals() method... Session per se (I
think so, someone correct me if I'm wrong) isn't serialized. You don't have
to use notation Session["whatever"].ToString(). you can easily cast to
(string) if you've put a string inside of it. And creating so many session
variables for the same thing is nonsence. If you have more variables that
are somehow connected (like username, password, user display name, roles
collection, etc.) Just create an object that has all that and put that
object in Session. This way you wont have to operate with so many Session
variables, but with just one object.

I think there's enough hints for one message.

I suggest you to read a lot MSDN and some valuable internet sites like
www.dotnet247.com, www.gotdotnet.com, www.asp.net etc... You will see how
much you'll learn out of other people experience.
 
V

Viktor Popov

Hi again, Rebert!

Thank you very much for your hints. I'm going step by step and noe I have an
ASP.NET Application with Forms authentication and Role-based authorization.
With the checkboxes I don't understand some thing. What I should do in
OnCheckedChange? Could you show me with some code? And if I implement this
scenario will I solve my problem with this Delete button which I can't
click?

Thank you again!

Viktor
 
R

Robert Koritnik

The problem with your delete button is to me unknown. Probably some trivial
thing. This way thigs MUST work. Actually regardin Delete button there no
obviews change, except it already has all texts/IDs to delete.

Ok. OnCheckedChange...

first a property and then OnCheckedChange. Property can be string or any
other class that is serializable. The best way would be to create your own
and would work a s a collection or array/list

--------------------

private SomeSerializableList DeleteEntries
{
get
{
if (ViewState["deleteEntries"] == null) ViewState["deleteEntries"] =
new SomeSerializableList() ;
return (SomeSerializableList)ViewState["deleteEntries"];
}
}

private void checkbox_CheckedChange(object sender, EventArgs e)
{
CheckBox cb = ((CheckBox)sender);
if (cb.Checked)
{
if (!DeleteEntries.Contains(cb.Text)) DeleteEntries.Add(cb.Text);
}
else DeleteEntries.Remove(cb.Text);
}

private void delete_Click(object sender, EventArgs e)
{
if (DeleteEntries.Count > 0)
{
foreach(string s in DeleteEntries)
{
// do appropriate deletition - combining texts/IDs for WHERE ...
IN () or delete one by one
}
// either rebind the repeater or reload the same page with
Response.Redirct()
}
}

--------------------

Ok. On each postback (weather it's delete click or page change or anything
else on the page), checkboxes will get evaluated for change. All checked
will get into the list, all unchecked out of it. When delete button is
clicked (after confirm on the client), you just check for entries existance
and delete those...

You see... Code can be short and does the appropriate functionality.

A nice thing would be, that a user knows there are selected items on a non
displayed page...

BTW: Errors may be in the code. I wrote it out of my head.
 
V

Viktor Popov

Thank you again!
I'll try what you said and write you back:)

Regards,

Viktor
Robert Koritnik said:
The problem with your delete button is to me unknown. Probably some trivial
thing. This way thigs MUST work. Actually regardin Delete button there no
obviews change, except it already has all texts/IDs to delete.

Ok. OnCheckedChange...

first a property and then OnCheckedChange. Property can be string or any
other class that is serializable. The best way would be to create your own
and would work a s a collection or array/list

--------------------

private SomeSerializableList DeleteEntries
{
get
{
if (ViewState["deleteEntries"] == null) ViewState["deleteEntries"] =
new SomeSerializableList() ;
return (SomeSerializableList)ViewState["deleteEntries"];
}
}

private void checkbox_CheckedChange(object sender, EventArgs e)
{
CheckBox cb = ((CheckBox)sender);
if (cb.Checked)
{
if (!DeleteEntries.Contains(cb.Text)) DeleteEntries.Add(cb.Text);
}
else DeleteEntries.Remove(cb.Text);
}

private void delete_Click(object sender, EventArgs e)
{
if (DeleteEntries.Count > 0)
{
foreach(string s in DeleteEntries)
{
// do appropriate deletition - combining texts/IDs for WHERE ....
IN () or delete one by one
}
// either rebind the repeater or reload the same page with
Response.Redirct()
}
}

--------------------

Ok. On each postback (weather it's delete click or page change or anything
else on the page), checkboxes will get evaluated for change. All checked
will get into the list, all unchecked out of it. When delete button is
clicked (after confirm on the client), you just check for entries existance
and delete those...

You see... Code can be short and does the appropriate functionality.

A nice thing would be, that a user knows there are selected items on a non
displayed page...

BTW: Errors may be in the code. I wrote it out of my head.

--
RobertK
{ Clever? No just smart. }

Viktor Popov said:
Hi again, Rebert!

Thank you very much for your hints. I'm going step by step and noe I
have
an
ASP.NET Application with Forms authentication and Role-based authorization.
With the checkboxes I don't understand some thing. What I should do in
OnCheckedChange? Could you show me with some code? And if I implement this
scenario will I solve my problem with this Delete button which I can't
click?

Thank you again!

Viktor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Repeater paging problem 1
Problem using custom paging 0
repeater 0
pagination in repeater issue. 0
Problem with codewars. 5
Repeater control 1
Paging Problem 1
Paging 0

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top