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"];
Related
When I update a value in database, it doesn't show anymore in my drop-down-list. I update my page to not show this value in my drop-down-list anymore. Now I refresh the page automatically after removing this item so you can't choose that item again but I want to show in a label that "This value was removed!".
But it won't appear after the page is refresh.
protected void btnBevRekeningVerwijderen_Click(object sender, EventArgs e)
{
B.RekeningVerwijderen(Convert.ToInt32(ddlRekeningVerwijderen.SelectedValue));
Page.Response.Redirect(Page.Request.Url.ToString(), true);
lblRekeningMakenInfo.ForeColor = System.Drawing.Color.Green;
lblRekeningMakenInfo.Text = "The value is removed!";
}
The label won't show if you refresh the page (could be simpler if it didn't), however now you can use Session or Cookie:
if(Session["removed"] == true)
{
lblRekeningMakenInfo.Text = "The value is removed!";
Session["removed"] = null;
}
When removing you should set the session:
Session["removed"] = true;
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
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 alphabetic filter consist of 26 dynamically created link button on selecting any link button it is filtering the name of user's on the basis of alphabet and changing its color to orange to make it different from other linkbuttons it is working fine but if there are more number of user associated with a particular alphabet and on applying filter it is filtering the user on the basis of that alphabet and showing them in a list view on clicking the data pager next page or any other page number the link button changes its color to default color but i want to keep that highlighted until and unless other link button is selected
my code
protected void Page_Init(object sender, EventArgs e)
{
// Adding Dynamically linkbuttons for all alphabets(i.e. A-Z)
for (char asciiValue = 'A'; asciiValue <= 'Z'; asciiValue++)
{
LinkButton lbtnCharacter = new LinkButton();
lbtnCharacter.ID = "lbtnCharacter" + asciiValue;
divAlphabets.Controls.Add(lbtnCharacter);
// Setting the properties of dynamically created Linkbutton.
lbtnCharacter.Text = Convert.ToString(asciiValue);
lbtnCharacter.CssClass = "firstCharacter";
lbtnCharacter.ToolTip = "Show Tags starting with '" + Convert.ToString(asciiValue) + "'";
lbtnCharacter.CommandArgument = Convert.ToString(asciiValue);
lbtnCharacter.Command += new CommandEventHandler(lbtnCharacter_Command);
}
}
// For assigning default color to linkbutton text in page load
foreach (var ctrl in divAlphabets.Controls)
{
if (ctrl is LinkButton)
((LinkButton)ctrl).CssClass = "firstCharacter";
}
void lbtnCharacter_Command(object sender, CommandEventArgs e)
{
// Storing the values of pressed alphabet in viewstate.
ViewState["Selected_Character"] = e.CommandArgument;
LinkButton lbtnSelected = (LinkButton)divAlphabets.FindControl("lbtnCharacter" + e.CommandArgument);
lbtnSelected.CssClass = "firstCharacter highlighted";
txtTagFilter.Text = string.Empty;
BindTagList();
}
I hope I understood your question.
You are setting your Selected_Character item in the command handler and then setting the class of the button to highlight it. This only gets fired when the button is clicked, not when you move to the next page. Why not separate these two operations. Set the class of the link button on prerender if the Selected_Character matches. That way even when you page the link button will stay highlighted.
I would also set your selected character as a query string parameter, if someone copies and pastes a link to your page the button would not highlight and the correct data would not display.
Hope this helps.
Edit: Haven't tested the below but maybe it will get you started.
void lbtnCharacter_Command(object sender, CommandEventArgs e)
{
// redirect to self with tag as qs parameter
Response.Redirect(string.Format("{0}?tag={1}", Request.Url.GetLeftPart(UriPartial.Path), e.CommandArgument));
}
protected void Page_PreRender(object sender, EventArgs e)
{
if (Request.QueryString["tag"] != null) {
LinkButton lbtnSelected = (LinkButton)divAlphabets.FindControl("lbtnCharacter" + Request.QueryString["tag"]);
lbtnSelected.CssClass = "firstCharacter highlighted";
}
}
N.B You will also need to change your BindTagList to use the query string also. I'm assuming you call this in the page load event.
I have a start page with textboxes and I am sending the values entered in the textbox to another page using Cache on click of a next button.
Now I have a problem that when the user goes to the next page ad decides to go back again he should be able to do so and the values he entered in the textboxes should still be present.
Is there a way to do so...
My code for sending values is:
protected void Button4_Click(object sender, EventArgs e)
{
if (TextBox2.Text == "" || TextBox3.Text == "")
{
Label1.Text = ("*Please ensure all fields are entered");
Label1.Visible = true;
}
else
{
Cache["PolicyName"] = TextBox2.Text;
Cache["PolicyDesc"] = TextBox3.Text;
Response.Redirect("~/Addnewpolicy3.aspx");
}
}
and I receive this by on the next page as:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string pn = Cache["PolicyName"].ToString();
string pd = Cache["PolicyDesc"].ToString();
string os = Cache["OperatingSystem"].ToString();
}
}
It sounds like you want to take adavantage of Cross-Page postbacks which were added to ASP.NET in version 2.0
The following URL should offer some guidance
http://msdn.microsoft.com/en-us/library/ms178139.aspx
Cache is shared by all users. The code above will result in information being shared between users. If you want per-user temp storage you should use the Session instead. Other than that I can make 2 recommendations:
Proceed with Session, your approach is fine
Look at the Wizard control http://msdn.microsoft.com/en-us/magazine/cc163894.aspx
To make the values restore, in the controls simple do this:
<asp:TextBox ID="txtName" runat="server" text='<%=Session["Name"] %>'></asp:TextBox>
Pretend you got two pages, page1 and page2:
1st you search on page1 and get the result, then click into the link and enter 2nd page.
My suggestion is when you click on hyperlink/button on page1, you can put the textbox field value into session like
Session["key"] = txtbox.Text;
After you enter 2nd page, you can try to set session equal to itself in "Back" button action for bring back to page1.
Session["key"] = Session["key"].ToString();
Insert the code when you back from page2, get value from session and put into search form's textbox
txtbox.Text = Session["key"].ToString();