Page index is not working - c#

Help me ..my page index is not working in visual studio.
my page load is as follows:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
CustomerView.DataSource = Customer.GetAll();
CustomerView.DataBind();
}
}
protected void CustomerView_PageIndexChanging(object sender, System.Web.UI.WebControls.GridViewPageEventArgs e)
{
int newPageNumber = e.NewPageIndex + 1;
CustomerView.DataSource = Customer.GetAll();
CustomerView.DataBind();
}
what am i doing wrong my page index in not working.

Try this. I think you have to set the GridView's PageIndex property manually.
protected void CustomerView_PageIndexChanging(object sender, System.Web.UI.WebControls.GridViewPageEventArgs e)
{
CustomerView.PageIndex = e.NewPageIndex;
CustomerView.DataSource = Customer.GetAll();
CustomerView.DataBind();
}

Related

Web Forms 4.5 - Repeater.Items is empty on Postback

I have a Repeater which has it's data set up on Page_Load
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
myRepeater.DataSource = data;
myRepeater.DataBind();
}
}
When I come to save my data on postback, myRepeater.Items contains zero elements when I know there are several.
protected void btnSave_Click(object sender, EventArgs e)
{
....
// why does this contain zero elements>
foreach (RepeaterItem item in myRepeater.Items)
{
Any suggestions as to what may be the issue?
In this case, binding the repeater even on postback fixed the issue
protected void Page_Load(object sender, EventArgs e)
{
myRepeater.DataSource = data;
myRepeater.DataBind();
}

Rows adding as many times as page refreshes

I am inserting some content in database on button click event every thing is working fine while insertion of the Data.
The problem is I just refreshed the page after the button click then I noticed that after the button click Data is inserting as many time as I refreshes the page.
How can I stop this ?
Here is my Button Code :
protected void btn_AddEdu_Click(object sender, EventArgs e)
{
hfTab.Value = "edu";
if (ValidateAddEdu())
{
emp_edu.InsertEdu(Session["empcd"].ToString(), ddl_degree.SelectedValue.ToString(), txt_eduterms.Text, ddl_institute.SelectedValue.ToString(), txt_edupassyear.Text, txt_edugrade.Text, ddl_sponsor.SelectedValue.ToString());
int imagefilelength = fileupload_edu.PostedFile.ContentLength;
byte[] imgarray = new byte[imagefilelength];
HttpPostedFile image = fileupload_edu.PostedFile;
image.InputStream.Read(imgarray, 0, imagefilelength);
edu_attach.InsertEduAttachment(Session["empcd"].ToString(),ddl_degree.SelectedValue.ToString(),imgarray);
lbl_eduerr.Text = "Added";
lbl_eduerr.ForeColor = System.Drawing.Color.Green;
BindEduGrid();
}
}
Add following code in your .cs page
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
Session["CheckRefresh"] = Server.UrlDecode(System.DateTime.Now.ToString());
}
}
protected void Page_PreRender(object sender, EventArgs e)
{
ViewState["CheckRefresh"] = Session["CheckRefresh"];
}
protected void btn_AddEdu_Click(object sender, EventArgs e)
{
if (Session["CheckRefresh"].ToString() == ViewState["CheckRefresh"].ToString())
{
hfTab.Value = "edu";
if (ValidateAddEdu())
{
emp_edu.InsertEdu(Session["empcd"].ToString(), ddl_degree.SelectedValue.ToString(), txt_eduterms.Text, ddl_institute.SelectedValue.ToString(), txt_edupassyear.Text, txt_edugrade.Text, ddl_sponsor.SelectedValue.ToString());
int imagefilelength = fileupload_edu.PostedFile.ContentLength;
byte[] imgarray = new byte[imagefilelength];
HttpPostedFile image = fileupload_edu.PostedFile;
image.InputStream.Read(imgarray, 0, imagefilelength);
edu_attach.InsertEduAttachment(Session["empcd"].ToString(),ddl_degree.SelectedValue.ToString(),imgarray);
lbl_eduerr.Text = "Added";
//Add this line
Session["CheckRefresh"] = Server.UrlDecode(System.DateTime.Now.ToString());
lbl_eduerr.ForeColor = System.Drawing.Color.Green;
BindEduGrid();
}
}
}

Session Variable error bach booking

I set up a session variable on the Booking form.aspx as so:
protected void confirmImageButton_Click(object sender, ImageClickEventArgs e)
{
Session["confirmBooking"] = "confirm";
Session["beachBach"] = beachBachRadioButtonList.SelectedItem.Text;
}
and I transfer to my other page as so:
protected void Page_Load(object sender, EventArgs e)
{
{
if (Session["beachBach"] != null)
{
numberOfBeachBookingInteger += 1;
beachBachLabel.Text = numberOfBeachBookingInteger.ToString();
}
I'm trying to add 1 to the beachBach session variable whenever user press the confirm button....however, when i start to debug it, instead of adding 1, it add 2 to the label.
Can someone please help me out.. thanks
try this
protected void Page_Load(object sender, EventArgs e) {
{
if(!IsPostBack)
{
if (Session["beachBach"] != null)
{
numberOfBeachBookingInteger += 1;
beachBachLabel.Text = numberOfBeachBookingInteger.ToString();
}
}
protected void Page_Load(object sender, EventArgs e)
{
{
if (Session["beachBach"] != null)
{
numberOfBeachBookingInteger += 1;
beachBachLabel.Text = numberOfBeachBookingInteger.ToString();
}
Well what happens after we come to this page ?? Do we have any event that causes postback in this page??
If there is than definitely it will add 1 again to your session .
Try using !IsPostBack property .

c# trying to access something from another method

i am new to this so hope u guys bear with me . i am trying to insert into database the URL directory of the filePathImage upon btnDone.
Partial Codes:
protected void AjaxFileUpload1_UploadComplete(object sender, AjaxControlToolkit.AjaxFileUploadEventArgs e)
{
EnsureDirectoriesExist();
String filepathImage = (#"Images/Story/" +txtTitle.Text + "/" + e.FileName);
AjaxFileUpload1.SaveAs(Server.MapPath(filepathImage));
}
protected void btnDone_Click(object sender, EventArgs e)
{
act.ActivityName = dropListActivity.SelectedItem.Text;
act.Title = txtTitle.Text;
act.FileURL = filepathImage;
daoStory.Insert(act);
daoStory.Save();
}
i got a problem with filePathImage in act.FileURL = AjaxFileUpload1.filePathImage; Any advise or solutions will be grateful
try below, when upload complete you can put your path in to session and take that session path when you needed.
protected void AjaxFileUpload1_UploadComplete(object sender, AjaxControlToolkit.AjaxFileUploadEventArgs e)
{
// your Code
Session["filepathImage"] = filepathImage ; // put the path in session variable
}
protected void btnDone_Click(object sender, EventArgs e)
{
if(Session["filepathImage"]!=null)
{
string filepathImage = Session["filepathImage"] as string;
// your code ...
}
}

Textbox in apsx.cs file won't update a changed text

I have a web application consisting of an aspx-file.
On page load two textboxes are filled with data (a "username" and a "password"). This works.
On a button click it should save the textboxes' text. But for some reason the text of the textboxes isn't updated if I have changed it manually meanwhile (by typing in some letters with my keyboard).
Why is that? And how can I tell my program to regard my changes?
My code is:
protected void Page_Load(object sender, EventArgs e)
{
CredentialsManager cm = new CredentialsManager();
TextBox_Benutzername.Text = cm.Username;
TextBox_Passwort.Text = cm.Password;
}
protected void Button_Speichern_Click(object sender, EventArgs e)
{
CredentialsManager cm = new CredentialsManager();
cm.setCredentials(TextBox_Benutzername.Text, TextBox_Passwort.Text);
}
EDIT:
It works with this improvement:
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
CredentialsManager cm = new CredentialsManager();
TextBox_Benutzername.Text = cm.Username;
TextBox_Passwort.Text = cm.Password;
}
}
For further information, see answers below. Thanks everyone!
Try checking for a postback -
private void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
CredentialsManager cm = new CredentialsManager();
TextBox_Benutzername.Text = cm.Username;
TextBox_Passwort.Text = cm.Password;
}
}
Your Page_Load code will currently run after every button click (or postback), and overwrite the values you have manually added.
Try this,
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack){
CredentialsManager cm = new CredentialsManager();
TextBox_Benutzername.Text = cm.Username;
TextBox_Passwort.Text = cm.Password;
}
}
You are assiging the value to the textboxes on every page load instead of firt page load.
Change the Page_Load method to :
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
CredentialsManager cm = new CredentialsManager();
TextBox_Benutzername.Text = cm.Username;
TextBox_Passwort.Text = cm.Password;
}
}
I think the problem is that you are creating a new CredentialsManager each and every time that the page is loaded (I assume that a new CredentialsManager has an empty Username and Password fields). You should only do that on new page loads, and not when the page is refreshed because of a button click. That is determined with the Page.IsPostBack property, so you moght need to do:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
CredentialsManager cm = new CredentialsManager();
TextBox_Benutzername.Text = cm.Username;
TextBox_Passwort.Text = cm.Password;
}
}

Categories