How to optimize code if ViewState turned off? - asp.net

protected void Page_Init(object sender, EventArgs e)
{ }
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DropDownList1.DataSource = SomeObject.GetData();
DropDownList1.DataBind();
} }
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
var selvalue = DropDownList1.SelectedValue;
DoSomething(selvalue);
}
Please some body help me to make works properly this code if ViewState turned off.

If you turn the viewstate off, then you need to rebind the data on every trip to the server and before the page initializes.
Example of a dropdownlist without viewstate

Related

asp.net: Create Handles clause for control class

Is there a way that I force all TextBoxes (as an example) .. on an asp.net form, to go through the same routine, during a "postback" event or onchange event?
Here is example code.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
textBox1.TextChanged += new EventHandler(textBox_TextChanged);
textBox2.TextChanged += new EventHandler(textBox_TextChanged);
}
}
protected void textBox_TextChanged(object sender, EventArgs e)
{
}

Initial selection for a DropDownList overriding user selection

I'm trying to set an initial selection for a DropDownList by calling: drop.SelectedIndex = 5 in Page_Load.
This works, but then if I change the selection manually and want to save the form, I'm still getting the initial selection instead of the new selection when calling drop.SelectedValue. What's wrong?
You have forgotten you check if(!IsPostback). Otherwise you will select the 6th item again on postbacks before the SelectedIndexChanged event is triggered (or a button-click event):
protected void Page_Load(Object sender, EventArgs e)
{
if(!IsPostBack) // do this only on the initial load and not on postbacks
dropDwonList1.SelectedIndex = 5;
}
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
//set up data here
}
}
if (Page.IsPostBack)
{
//do page reload logic in here
}
protected void foo(object sender, EventArgs e)
{
//get your selected value here
}
Try this code
You should be using if(!IsPostback) in the Page_Load function.
protected void Page_Load(Object sender, EventArgs e)
{
if(!IsPostBack)
{
drop.SelectedIndex = 5;
//yourcode
}
}
Through this your problem will be solved

ASP.NET Profile

I have this:
protected void Page_Load(object sender, EventArgs e)
{
nome_Txt.Text = Profile.dados_pessoais.nome;
}
protected void save_Click(object sender, EventArgs e)
{
Profile.dados_pessoais.nome = nome_Txt.Text;
}
If Profile.dados_pessoais.nome is empty, nome_txt.Text is empty too. When I change nome_Txt.Text to teste for example, when I click on the button nome_Txt.Text is empty.
What am I doing wrong?
The Page_Load event run before the button click event so you always assign the text box to empty value.
To solve this, don't populate the textbox when you are in a Postback:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
nome_Txt.Text = Profile.dados_pessoais.nome;
}
}
As also stated in a comment, you probably have to save the profile after changing it otherwise it won't be saved when you next load the page:
protected void save_Click(object sender, EventArgs e)
{
Profile.dados_pessoais.nome = nome_Txt.Text;
Profile.Save()
}

Session Timeout manually

I have 2 pages on my dummy site in asp.net, (default.aspx and default2.aspx), On default.aspx, i created session like below
protected void Page_Load(object sender, EventArgs e)
{
Session["MySession"] = "WELCOME";
Session.Timeout = 1;
}
protected void Button1_Click(object sender, EventArgs e)
{
Response.Redirect("default2.aspx");
}
and on default2.aspx
if (Session["MySession"] != null)
Response.Write(Session["MySession"]);
else
Response.Write("Session Timed Out");
i was wondering that after 1 min the session will get erase, as i have timeout, but after one minute when i click on the button it redirected me to default2.aspx, and displayed a session value "WELCOME". can anyone tell me how to erase session value after particular duration
In your Default.aspx you have to check if it is not a post back otherwise the session will be initialized again for each button click
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.isPostBack())
{
Session["MySession"] = "WELCOME";
Session.Timeout = 1;
}
}
protected void Button1_Click(object sender, EventArgs e)
{
Response.Redirect("default2.aspx");
}

How to get event in another control event?

In my application, i am trying to get button_click event in another button click event, but it is not working.
My code is,
protected void btn1_Click(object sender, EventArgs e)
{
Response.Write("hi.....");
}
protected void btn2_Click(object sender, EventArgs e)
{
btn1.Click += new EventHandler(this.btn1_Click);
}
What is wrong with this?
I suppose you want to call the code of btn1_click event.
If this is the case, you simple call it as a method in your btn2_Click.
protected void btn2_Click(object sender, EventArgs e)
{
btn1_Click (sender, e);
}

Resources