Let me elaborate on this... I have the code below, there is a Page_Init (which I still don't understand why it fires more than once, but that is another story), and a Page_Load and I am checking for the "isPostBack" ... everything works great while I use my controls, radio button and drop down list, as well as Buttons; however, if I press the key, even accidentally, the "isPostBack" is reset to False. What am I doing wrong? Also, my AutoEventWireup="true".
Also, this is an .ascx file.
protected void Page_init(object sender, EventArgs e)
{
LoadPageText1();
paymntpnl1.Visible = true;
curbalpnl.Visible = false;
paymntpnl2.Visible = false;
paymntpnl3.Visible = false;
paymntpnlcc.Visible = false;
}
protected void Page_Load(object sender, EventArgs e)
{
LoadPageData();
getContractInfo();
step2lbl.BackColor = System.Drawing.Color.White;
nopmt = Convert.ToDecimal(numpmts.Text);
nopmt = nopmt * nopymts2;
sb.Clear();
sb.Append("$");
sb.Append(nopmt.ToString("#.00"));
nopymts.Text = sb.ToString();
ValidateCC();
chkNewCC();
bool crdcrd = credCard;
bool newcrd = nwCard;
if (!IsPostBack){
}
}
You're checking IsPostBack but you're still doing all the resetting before the check! And then the check makes no difference because it's an empty conditional block! You should be doing this:
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
// All the initial setup that you don't want to do again goes here.
}
// All the stuff you want to do on first load AND PostBack goes here.
}
Make sure you understand how conditionals work.
Related
I'm encountering a problem when working on our ASP.NET website:
I had a table hidden until a specified button was clicked. Once that button was clicked, the table - along with it's content - will be visible by that point-on. One of the contents of that table is a Calendar. The problem is this - whenever I switch the year in the Calendar, the table goes back to its hidden state.
I recognized this because I placed the table_Name.visible = false; property at Page_load
protected void Page_Load(object sender, EventArgs e)
{
my_Table_name.Visible = false;
}
I tried fixing it, so my first solution was this:
int counter = 0;
protected void Page_Load(object sender, EventArgs e)
{
if (counter == 0)
{
additional_Tabe.Visible = false;
counter += 1;
}
}
My solution didn't work.
My second solution is Regular Expression
My third solution is using Drop Down Lists to individually represent Month / Day / Year
The problem with my second and third solution is that I'll have to replace all Calendar my group-mates have placed with either of my solutions which is pretty much a tedious, but doable task.
I'm just wondering if there's a way to prevent the table from going back to it's hidden state while the user switches the year of the Calendar, select a date, or generally plays around with the Calendar.
Using the Page.IsPostBack property inside of Page_Load, https://learn.microsoft.com/en-us/dotnet/api/system.web.ui.page.ispostback?view=netframework-4.8 it returns true if: the page is being loaded in response to a client postback; otherwise, false. You can wrap your my_Table_name.Visible = false; in this conditional and only set it to false if it is a postback
Code:
protected void Page_Load(object sender, EventArgs e)
{
if(IsPostBack)
my_Table_name.Visible = false;
}
int counter = 0;
protected void Page_Load(object sender, EventArgs e)
{
if(!PostBack)
{
if (counter == 0)
{
additional_Tabe.Visible = false;
counter += 1;
}
}
}
Used this Code...
I have problem with richBox1 text disabling.
I've tryed richTextBox1.readonly = true; and richTextBox1.Enabled = false;
My code:
private void richTextBox1_TextChanged(object sender, EventArgs e)
{
richTextBox1.ReadOnly = !richTextBox1.ReadOnly;
}
Its disabling after one letter.
EDIT: And if disable I can still copy text but cant write there.
Honestly, disabling expected functionality is not something you should be doing. It is not good UI design.
The event TextChanged is fired every time the text changes (including writing or removing one letter). You can use Form's Load event (by double clicking the form on design time) :
private void Form1_Load(object sender, EventArgs e)
{
richTextBox1.ReadOnly = true;
richTextBox1.Enabled = false;
}
I'm trying to set the "txtMiles" textbox to focus after:
The form opens
When the "clear" button is clicked
I have tried using txtMiles.Focus(); but it doesn't seem to work for me.
CODE BEING USED ON THIS FORM
private void btnConvert_Click(object sender, EventArgs e)
{
//assigns variable in memory.
double txtMile = 0;
double Results;
try
{
// here is where the math happens.
txtMile = double.Parse(txtMiles.Text);
Results = txtMile * CONVERSION;
lblResults.Text = Results.ToString("n2");
txtMiles.Focus();
}
// if the user enters an incorrect value this test will alert them of such.
catch
{
//MessageBox.Show (ex.ToString());
MessageBox.Show("You entered an incorrect value");
txtMiles.Focus();
}
}
private void btnClear_Click(object sender, EventArgs e)
{
//This empties all the fields on the form.
txtMiles.Text = "";
txtMiles.Focus();
lblResults.Text = "";
}
private void btnExit_Click(object sender, EventArgs e)
{
// closes program
this.Close();
}
}
}
Thanks in advance for the help.
You should make sure your TabIndex is set and then instead of Focus(), try use Select(). See this MSDN link.
txtMiles.Select();
Also make sure there isn't a TabStop = true attribute set in the view file.
It's old, but someone could need this.
Control.Focus() is bugged. If it's not working, try workaround:
this.SelectNextControl(_controlname, true, true, true, true);
Change function parameters so they will work with your control, and remember about TabStop = true property of your control.
You already have your txtMiles focused after the clear-button click. As for the Startup, set txtMiles.Focus() in your load-method.
private void MilesToKilometers_Load(object sender, EventArgs e)
{
txtMiles.Focus();
}
using this solution worked perfectly...
txtMiles.Select();
protected void Page_Load(object sender, EventArgs e)
{
Panel1.Visible = True;
Panel2.Visible = false;
LoadQuestion(); // auto choose question from database and add into Panel1
LoadQuestion1(); // auto choose question from database and add into Panel2
}
When I start my program, my form automatically loads questions into my textbox and radio button list. I click link button2 to make my Panel1 visible = false and Panel2 visible = true to continue answering question. But after I clicked the link button 2 or 1, it will go back to the Page_Load() method and causes my questions keep on changing.
You should check if it's a postback. You want to execute this only on first load.
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack) {
Panel1.Visible = True;
Panel2.Visible = false;
LoadQuestion(); // auto choose question from database and add into Panel1
LoadQuestion1(); // auto choose question from database and add into Panel2
}
}
Source
This is because the Load event happens every time the server processes a request to your page.
There are two kinds of request: initial page load (when you go to the URL) and postback (when you click a button). What you do in your Page_Load method is kinda initialization, so it should be done only initially, but not during the postback.
protected void Page_Load(object sender, EventArgs e)
{
if( !IsPostBack )
{
// The code here is called only once - during the initial load of the page
}
// While the code here is called every time the server processes a request
}
Try:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Panel1.Visible = True;
Panel2.Visible = false;
LoadQuestion(); // auto choose question from database and add into Panel1
LoadQuestion1(); // auto choose question from database and add into Panel2
}
}
What is the best way to distinguish beteen "Refresh Post" or a "Real Post Back".
This is what I need to attain
protected void Button1_Click(object sender, EventArgs e)
{
if(PostBack && !Refresh)
{
//Do Something
}
}
I usually do a Response.Redirect to the same page in the postback event.
That way all my Page.IsPostBack are real Postbacks and not Refreshes
You could set a hidden input with a nonce value generated randomly every time the form is loaded (but not on postback), then check if the nonce value got sent twice. If it got sent a second time, it was a refresh.
you could try like
protected void Button1_Click(object sender, EventArgs e)
{
//your code of Click event
//..............
//...............
// and then add this statement at the end
Response.Redirect(Request.RawUrl); // Can you test and let me know your findings
}
Sample working code for the accepted answer
Add this line in designer
<input type="hidden" runat="server" id="Tics1" value="GGG" />
Add following lined in the code behind
public partial class WebForm1 : System.Web.UI.Page
{
long tics = DateTime.Now.Ticks;
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
this.Tics1.Value = tics.ToString();
Session["Tics"] = tics;
}
}
protected void Button1_Click(object sender, EventArgs e)
{
if (Session["Tics"] != null && Request["Tics1"] != null)
{
if (Session["Tics"].ToString().Equals((Request["Tics1"].ToString())))
{
Response.Write("Postback");
}
else
{
Response.Write("Refresh");
}
}
this.Tics1.Value = tics.ToString();
Session["Tics"] = tics;
}
}