I have a one grid view, on row command event i am redirecting on another page with query string that contains a current page index of grid view.
On redirected page i have a back button, when i click on this button i want redirect previous page with specified page index
For example:
suppose i am on my page-1 and current page index of grid view is 15 and on row command event i am redirecting on page-2.when click "back button" it must be redirected to page-1 with page index of 15 of grid view.
My code is as below:
Code of page that contains grid view (Page-1)
if (e.CommandName.ToLower() == "application")
{
Response.Redirect("view-msme-em-1-with-print.aspx?pageIndex=" + i , false);
}
Code of page that contains Button(Page-2)
protected void iBtnBack_Click(object sender, ImageClickEventArgs e)
{
Response.Redirect("searchMSMEApplication.aspx?pageIndex=" + Request.QueryString["pageIndex"].ToString() );
}
Code on page load event that contains gird view(page-1)
protected void Page_Load(object sender, EventArgs e)
{
fillGridOnLoad(); // it fills a grid view with data
grvEm2Application.PageIndex = Convert.ToInt32(Request.QueryString["pageIndex"].ToString());
}
when i click a "Back" on "page-2" it is redirecting to page-1 but page index is not as i set. is any thing missing?
Try below code
protected void Page_Load(object sender, EventArgs e)
{
grvEm2Application.PageIndex = Convert.ToInt32(Request.QueryString["pageIndex"].ToString());
fillGridOnLoad(); // it fills a grid view with data
}
Just need to set PageIndex before data bind
Related
I have many aspx pages which are a process to create a final form. eg,
Select Item Type Page -> Select item color page -> Select item quantity page -> Complete Page
Each page have a next and a previous button and when I move from the first page to the second page and the user clicks the previous button returning to the first, the control's states will be reset losing all the user's input
I thought of saving the controls in a session this way:
protected void btn_Next_Click(object sender, EventArgs e)
{
if (validateForm() == true)
{
Session["test1"] = RadioButtonList1;
Response.Redirect("nextpage.aspx");
}
}
And loading it this way:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
if (Session["test1"] != null)
RadioButtonList1 = (RadioButtonList)Session["test1"];
getInfo();
}
}
However, I selected the 2nd item in the radiobuttonlist1 and when i move to the 2nd page and back to the 1st, it didn't load the exact state of the radiobuttonlist1. The debugging shows it went into the RadioButtonList1 = (RadioButtonList)Session["test1"]; code
Can you try this :
Set the value attribute for each list-item and retrieve the selected value in a session like this
Session["test1"] = RadioButtonList1.SelectedItem.Value;
and while loading
RadioButtonList1.SelectedValue = (String) Session["test1"];
I have a Grid and on top of the grid, I like am showing a label as such:
<asp:Label ID="lblMsg" runat="server" ForeColor="Red"></asp:Label>
On delete of a row in my grid I have the following code:
protected void RadGrid1_DeleteCommand(object sender, GridCommandEventArgs e)
{
if (!(User.IsInRole("Administrator")))
{
lblMsg.Text = "Must be an Admin in delete.";
return;
}
NOTE: The debugger does go to where I have the label text displayed but it simply does not display on the page. :
lblMsg.Text = "Must be an Admin in delete.";
NOTE: If I have the same code in the page load, the label text shows up fine on the page Also DO NOT have (!IsPostBack){} in my code.
Therein lies your issue. When your page is posting back the label is getting assigned the value:
lblMsg.Text = "Must be an Admin in delete.";
But Page_Load gets called again. So your Page_Load should look like this
protected void Page_Load(object sender, EventArgs e) {
if(!IsPostBack) {
//Populate my page
}
}
This will hold true for almost every page you write in ASP.NET
I have buttons in one Column called Type. When the user clicks on the button it should be disabled. This works fine in the first page but it doesnt on the second,third,fourth pages.
I have 10 rows in my page and e.CommandArgument gets the row number.
I believe the buttons are populated in the gridview from 0 to 9 and e.CommandArgument is 1-10. Thats why I have (e.CommandArgument) - 1 and it works find in the first page.
The things that in the second page the next buttons are again 0-9 but my e.CommandArgument is 11-20. Any ideas?
protected void GridViewType_RowCommand(object sender, GridViewCommandEventArgs e)
{
Button btnVote = (Button)GridViewType.Rows[Convert.ToInt32(e.CommandArgument) - 1].FindControl("btnVote");
btnV ote.Enabled = false;
}
try the following
protected void GridViewType_RowCommand(object sender, GridViewCommandEventArgs e)
{
GridViewRow row = (GridViewRow)(e.CommandSource);
Button btnVote = (Button)row.FindControl("btnVote");
btnVote.Enabled = false;
}
kindly check this :
want to Find Control in gridview on RowCommand event
I'm having a hard time figuring this out and I hope you guys would help me.
I have a page called Index.aspx with a DropDownList that is a separate UserControl class (because it will be used in other pages). Here's the code for that:
UcSelecionarLocal.ascx:
<%# Control Language="C#" AutoEventWireup="true"
CodeBehind="UcSelecionarLocal.ascx.cs"
Inherits="QuickMassage.uc.UcSelecionarLocal" %>
<asp:DropDownList ID="ddlLocais" runat="server"
CssClass="span4 dropdown-toggle" AutoPostBack="true">
</asp:DropDownList>
UcSelecionarLocal.ascx.cs:
public partial class UcSelecionarLocal : UserControl {
protected void Page_Load(object sender, EventArgs e) {
if (!this.IsPostBack) {
PreencherLocais();
}
}
private void PreencherLocais() {
ddlLocais.Items.Clear();
ddlLocais.Items.Add(new ListItem("Selecione", "0"));
ControleLocal controle = new ControleLocal();
DataTable tab = controle.ListarLocais();
foreach (DataRow row in tab.Rows) {
ddlLocais.Items.Add(new ListItem(row["Descricao"].ToString(),
row["ID"].ToString()));
}
}
}
This control is placed in Index.aspx and loads its values correctly. The form that it's contained in, has the action set to agendamentos.aspx. When I change the ddlist, the page is submitted to the forms action page, as it should be.
On the other page the problems begin: I get the parameters posted to this page and one of them is the ddlist value. In the immediate window, I check the value and it's there, let's say that it is 1.
To make long story short, I have this code:
agendamentos.aspx.cs:
protected void Page_Load(object sender, EventArgs e) {
DropDownList locais = ObterComponenteListaLocais();
try {
locais.SelectedIndex =
int.Parse(HttpContext.Current.Request["ucSelLocal$ddlLocais"]);
}
While debugging, I see that locais.SelectedIndex is -1. After the assignment it remains -1. The page loads and then I change the ddlist value again to 2. When debugging the same code above, I see that the locais.SelectedIndex is now 1. Again, setting it to 2, as it would normally be, produces no effect. If I change the ddlist again to 3, the SelectedIndex becomes 2 and does not take the value 3.
In other words: the value of the index in a newly loaded page is the value of the page that was loaded before.
Could you guys help me?
This is because the Page_Load event is firing in your page before the user control is loading. Do this:
public partial class UcSelecionarLocal : UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
}
public void PreencherLocais()
{
ddlLocais.Items.Clear();
ddlLocais.Items.Add(new ListItem("Selecione", "0"));
ControleLocal controle = new ControleLocal();
DataTable tab = controle.ListarLocais();
foreach (DataRow row in tab.Rows)
{
ddlLocais.Items.Add(new ListItem(row["Descricao"].ToString(), row["ID"].ToString()));
}
}
}
Then in your aspx page:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
this.idOfYourUserControl.PreencherLocais();
DropDownList locais = ObterComponenteListaLocais();
try {
locais.SelectedIndex =
int.Parse(HttpContext.Current.Request["ucSelLocal$ddlLocais"]);
}
}
Also because your question is a little confusing, an important note is that Page_Load fires before data is captured from controls that post back data. So that's a bad place to get their information because it will be what it was previously. That's why you need to create a function that fires on something like a button click that will execute after the controls data have been loaded.
I have a datagrid where a row can be deleted (using ajax). I'm have problem with the pager in the following scenario:
Lets say my PageSize is 10, I have 101 rows so that's 11 pages with the last page with an single element. Let no assume that I'm on page 10 (PageIndex=9) and delete a row. Then I go to the 11'th page (who's now empty and doesn't really exist). ASP now shows me the EmptyDataTemplate and no pager so I can't go back.
My approach (which isn't working) is to detect this scenario and step one page back:
public void Bind()
{
gridMain.DataBind();
}
public void SetPage(int page)
{
gridMain.PageIndex = page;
gridMain.DataBind();
}
protected void ldsGridMain_Selecting(object sender, LinqDataSourceSelectEventArgs e)
{
selectArgs = e;
e.Result = (new EnquiryListController()).GetEnquiryList(OnBind(this), supplier);
}
protected void ldsGridMain_Selected(object sender, LinqDataSourceStatusEventArgs e)
{
totalRows = selectArgs.Arguments.TotalRowCount;
//Detect if we need to update the page:
if (gridMain.PageIndex > 0 && (gridMain.PageSize * gridMain.PageIndex + 1) > totalRows) SetPage(gridMain.PageIndex - 1);
}
protected void gridMain_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
SetPage(e.NewPageIndex);
}
I can see that SetPage is called with the the right page index, but the databind doesn't seem to called as I still get the EmptyDataTemplate.
When you delete the item, you need to rebind the data, so that the pager resizes.
Not just call databind, you have to reset the datasource and call databind. It doesn't keep the data in the datasource between post backs.
Before you do that, make sure you reset the pageIndex to one that is in scope on the new set.
SetPage(gridMain.PageIndex - 1) might be incorrect. You have to calculate what the last page will be for the current row count. Current PageIndex-1 can be out of range.
Rebind the grid in SetPage with updated datasource and set pageindex. check if RecordCount/PageSize is less than NumberofPages in pager or current Page index then decrease the page index as per RecordCount/PageSize .
Can't you just use something like:
public void SetPage(int page)
{
gridMain.PageIndex = Math.Max(page, gridMain.PageCount);
gridMain.DataBind();
}