Can't see next page in gridview - asp.net

when I use " AllowPaging="True" " property in gridview and upload my webSite ,when I click next page , I can't see that and I see same record that I see already . point : I bind a dataBase on gridview,and I see that in gridview.above problem when I uplaod my website,happen . what is problem that I can't see next page of gridview ?

I am having a bit of trouble completing understanding your question. I think your are basically trying to get paging to work on a gridview.
In addition to setting "AllowPaging=True", you will need to hook into your paging event like so:
void GridView_PageIndexChanging(Object sender, GridViewPageEventArgs e)
{
e.NewPageIndex = e.NewPageIndex + 1;
DataBind();
}
Check it out on MSDN

you need to set the current page index property in Page index chaged event. then call the bind method in next line. then only you able to see the next page and it will work.
example
protected void SearchGrid_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
//set the current page index for grid
searchResultsGrid.PageIndex = e.NewPageIndex;
//Bind the result sets
BindSearchResults();
}

Custom control would help to avoid writing on page level event handlers

Related

Asp.net go to previous page

I have 5 dropdownlist in asp.net.User selects 5 dropdownlist after that click to button.Button sends page to another page.If i go previous page dropdownlist selectedvalues and datas lose(it displays default values without selected values)
I tried below
Response.Redirect("PreviousPage.aspx");
datas losing is there any solution ?
Instead of using Response.Redirect(), you can change the PostBackUrl of the button to the target page, or use Server.Transfer(). Once there you should be able to access the properties you need from the Page.PreviousPage object.
Example using Server.Transfer:
Page1.aspx.cs:
protected void SubmitButton_Click(object sender, EventArgs e)
{
Server.Transfer("Page2.aspx");
}
Page2.aspx.cs:
protected void Page_Load(object sender, EventArgs e)
{
var addressDropdownSelectedValue = ((Page1)Page).PreviousPage.AddressDropdown.SelectedItem.Value; //or SelectedValue
}
With the ScriptManager in the AJAX control toolkit (you get this in 4.0+, I think maybe even in 3.5), you can add history points. You add a history point and use that value to rebuild the page state.
http://msdn.microsoft.com/en-us/library/cc488548%28v=vs.140%29.aspx
You can also strongly type the previous page if you want to grab values from it. You do this with <%# PreviousPage %> directive. Then in code you could use Page.PreviousPage.FindControl

asp.net dynamic user control button click issue

I made researching about this subject I could not find proper answer.
In my default.aspx page, I have a treeview. Codes are in default.aspx like below:
protected void Page_Load(object sender, EventArgs e)
{
}
protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
{
Control ucont;
if (TreeView1.SelectedNode.Value == "Yeni Dönem")
{
ucont = LoadControl("usercontrols/yenidonem.ascx");
PlaceHolder1.Controls.Add(ucont);
}
else
{
ucont = LoadControl("usercontrols/tabloktar.ascx");
PlaceHolder1.Controls.Add(ucont);
}
}
I load user controls dnynmicaly. User controls are have button control. I can not fire user control's button click when I load it dynamcally. How can I solve this ?
Thanks.
First of all, I would not recommend adding control dynamically later than in Page_Load event. Other things to remember is that You should add it on each page load and assign unique ID value the control that does not change between postbacks.
In this case, the easiest way would be to always add both controls to the page and show appropriate one using Visibility property.
If that's not suitable for You, try to move the code from TreeView1_SelectedNodeChanged to the Page_Load event and load appropriate control on each postback until it should be changed to another one.
I haven't tested this, so if You have any issues when using thise answer, let me know in the comments and I'll try to help.

Paging in grid view

using paging in grid view but as i go through pagess i need to click on page links.. it should work on single click .. but it goes other page only after double click... any idea why..
simple and working
protected void grd_receipt_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
grd_receipt.PageIndex = e.NewPageIndex;
grd_receipt.DataBind();
}
simply call the databind after
Make sure your code to call DataBind() on your grid is in it's own method. You'll need to call it in two places.
On Page_load, if !IsPostBack, bind your grid.
When you're paging, set the new page index, then bind your grid.
This will eliminate your problem.

How to use the FindControl function to find a dynamically generated control?

I have a PlaceHolder control inside of a ListView that I am using to render controls from my code behind. The code below adds the controls:
TextBox tb = new TextBox();
tb.Text = quest.Value;
tb.ID = quest.ShortName.Replace(" ", "");
((PlaceHolder)e.Item.FindControl("ph_QuestionInput")).Controls.Add(tb);
I am using the following code to retrieve the values that have been entered into the TextBox:
foreach (ListViewDataItem di in lv_Questions.Items)
{
int QuestionId = Convert.ToInt32(((HiddenField)di.FindControl("hf_QuestionId")).Value);
Question quest = dc.Questions.Single(q => q.QuestionId == QuestionId);
TextBox tb = ((TextBox)di.FindControl(quest.ShortName.Replace(" ","")));
//tb is always null!
}
But it never finds the control. I've looked at the source code for the page and the control i want has the id:
ctl00_cphContentMiddle_lv_Questions_ctrl0_Numberofacres
For some reason when I look at the controls in the ListViewDataItem it has the ClientID:
ctl00_cphContentMiddle_lv_Questions_ctrl0_ctl00
Why would it be changing Numberofacres to ctl00? Is there any way to work around this?
UPDATE:
Just to clarify, I am databinding my ListView in the Page_Init event. I then create the controls in the ItemBound event for my ListView. But based on what #Womp and MSDN are saying the controls won't actually be created until after the Load event (which is after the Page_Init event) and therefore are not in ViewState? Does this sound correct?
If so am I just SOL when it comes to retrieving the values in my dynamic controls from my OnClick event?
UPDATE 2:
So i changed the code i had in my Page_Init event from:
protected void Page_Init(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
//databind lv_Questions
}
}
to:
protected void Page_Init(object sender, EventArgs e)
{
//databind lv_Questions
}
And it fixed my problem. Still a little confused as to why I want to databind regardless of whether it's a postback or not but the issue is resolved.
It looks like you're adding your textbox to a Placeholder control... but then you're searching a ListViewDataItem container for it later.
Seems to me that you need to search for the Placeholder first, and then search it for the textbox.

When GRidView's row is in edit mode, textboxes don't display current values

1) I noticed that if we don’t bind GridView to object data source control, then when user puts GridView into edit mode, we have to handle GridView.RowEditing event (else we get an exception ) and in this event put GridView’s row into editing mode. Is there a reason why GridView doesn’t automatically put a row into edit mode?
2) When we manually bind GridView to one of DataSet’s tables and user puts a row into edit mode, row’s columns will replace fields with text boxes. But for some reason these text boxes don’t display current field values, but instead they don’t display any text at all. What am I doing wrong?
3) I’ve also handled gridView.RowUpdated event, so I could put row back into non-edit mode, but to no effect. I even tried by pressing Edit button of some other row, but row still wouldn’t go out of edit mode. Any ideas what I’m doing wrong?
protected void gvwEmployees_RowUpdated(object sender, GridViewUpdatedEventArgs e)
{
e.KeepInEditMode = false;
}
Thanx
When not using a DataSource control with a GridView or other data-bound control which hide the complexity of the manual data-binding you must manually handle RowEditing, RowUpdating, and RowDeleting etc. With the built in data model and automatic binding the GridView handles these events for you.
You haven't posted your RowEditing code, but i suspect that you are not setting the GridViews EditIndex to the NewEditIndex and are not rebinding, this is probably why you are not seeing current data.
protected void gvwEmployees_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView.EditIndex = e.NewEditINdex;
BindData();
}
The same is true for your RowUpdating event. You will have to manually update your data, then set the EditIndex to -1, this will put your GridView back into ReadOnly mode. Keep in mind that e.OldValues, e.NewValues and e.Keys properties of the GridViewUpdateEventArgs are not populated when binding manually. This mean you'll have to take care of the update yourself by using e.RowIndex which is the index of the edited row.
protected void gvwEmployees_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
GridView.EditIndex = -1;
BindData();
}

Resources