I have a disabled text box thats' value only increments by one once a button is clicked, the problem is that it goes from 1 to 2 and thats it. I want it to increment everytime i push the button.
namespace StudentSurveySystem
{
public partial class AddQuestions : System.Web.UI.Page
{
int num = 1;
protected void Page_Load(object sender, EventArgs e)
{
QnoTextBox.Text = num.ToString();
}
protected void ASPxButton1_Click(object sender, EventArgs e)
{
num += 1;
QnoTextBox.Text = num.ToString();
}
}
}
Postback intializes the variable num to 1 again and you do not get the expected increamented result, you better you textbox value and store the value in ViewState.
protected void ASPxButton1_Click(object sender, EventArgs e)
{
num = int.Parse(QnoTextBox.Text);
num++;
QnoTextBox.Text = num.ToString();
}
Using ViewState
public partial class AddQuestions : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
ViewState["Num"] = "1";
}
protected void ASPxButton1_Click(object sender, EventArgs e)
{
QnoTextBox.Text = ViewState["Num"].ToString();
int num = int.Parse(ViewState["Num"].ToString());
ViewState["Num"] = num++;
}
}
Related
How can I get the values declared on variables through out the page? Now in my code, button click (btn_details_Click) gets student_id and dept_id as null.
public partial class TaskDetails : System.Web.UI.Page
{
private int student_id;
private int dept_id;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
student_id = Convert.ToInt32(Session["selected_student_id"]);
dept_id=GetDeptID(student_id );
}
}
protected void btn_details_Click(object sender, EventArgs e)
{
Session["selected_studentID"] = student_id;
Session["selected_DEPT_ID"] = dept_id;
Response.Redirect("Page2.aspx");
}
}
I have declared a variable globally and it is getting its value from page load function but the variable is not returning the value correctly in the button clicked function.
string id = "1";
protected void Page_Load(object sender, EventArgs e)
{
id = "3";
}
protected void Button1_Click(object sender, EventArgs e)
{
label.text = id;
}
Output
label.text = "1"
why is it so?
well when you click postback runs and assigns value to 3, so you will get only 3 unless you do the page load assignment when it is not postback
string id = "1";
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
id = "3";
}
}
protected void Button1_Click(object sender, EventArgs e)
{
label.text = id;
}
Goal: Keep value of int (professorIndex) reachable when I get to the button click method (btnUpdateAvailability_Click())
Problem: The value gets set correctly initially, then somehow goes to 0
What ive tried: Starting variable at class level. Getting rid of any other references to it, including commenting out where it was set to 0
What am I missing?
C#:
public partial class SiteMaster : MasterPage
{
private int professorIndex;
protected void Page_Load(object sender, EventArgs e) {
//some stuff
}
protected void cbUpdateAvailability_Click(object sender, EventArgs e)
{
CheckBox cbSender = (CheckBox)sender;
professorIndex = getProfessorIndexCB(cbSender.ClientID);
//at this point, professorIndex is 1, which is what I want/expect
}
public int getProviderIndexCB(string cbSender)
{
//professorIndex = 0;
switch (cbSender)
{
case "chkOnOff1":
professorIndex = 0;
break;
case "chkOnOff2":
professorIndex = 1; //This is the one that is triggered
break;
}
return professorIndex;
}
protected void btnUpdateAvailability_Click(object sender, EventArgs e)
{
//at this point, professorIndex is 0, no clue why. It should be one
}
Below a simple demo to store a value you want to persist in a Session and get it back on PostBack.
public int professorIndex = 0;
protected void Page_Load(object sender, EventArgs e)
{
//check for postback
if (!IsPostBack)
{
//set the index
professorIndex = 10;
//store the index in a session
Session["professorIndex"] = professorIndex;
}
}
protected void Button1_Click(object sender, EventArgs e)
{
//check if the session exists
if (Session["professorIndex"] != null)
{
//cast the session back to an int
professorIndex = (int)Session["professorIndex"];
}
//show result
Label1.Text = $"The professor index is {professorIndex}.";
}
I have an asp.net project with two web forms. I would like to pass all items of listbox3(which is home page) to textbox that is in statistics page.I try following code but didnt work.
Home page:
protected void Button5_Click(object sender, EventArgs e)
{
Response.Redirect("Statistics.aspx?ListBox3");
}
Statistics Page
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
TextBox3.Text = ListBox3.Items;
}
}
You can't refer any controls which are not in the current page with its ID.
So it may required to keep the items from ListBoxin Session or pass it through QueryString
Home Page
protected void Button5_Click(object sender, EventArgs e)
{
string Items = string.Empty;
foreach(var item in ListBox3.Items)
{
Items += item + ",";
}
//Session["ListBox3"] = Items;
Response.Redirect("Statistics.aspx?ListBox3=" + Items);
}
Statistics Page
protected void Page_Load(object sender, EventArgs e)
{
TextBox3.Text = Request.QueryString["ListBox3"].ToString();
//TextBox3.Text = Session["ListBox3"].ToString();
}
protected void Button5_Click(object sender, EventArgs e)
{
string allItems;
for(int i = 0; i < ListBox3.Items.Count; i++)
allItems += ListBox3.Items[i].ToString() + "--";
Response.Redirect("Statistics.aspx?ListBox3=" + allItems.SubString(0, allItems.Length - 2));
}
Access it using
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
TextBox3.Text = Request.QueryString["ListBox3"];
}
}
I am using Asp.net(C#), and SQL Server. In web application I want 5 digit Invoice number in text box, and after clicking on button the gridviews data to be saved in database, and then invoice number in the text box should be increment by one...and so on. I used this code =>
protected void Page_Load(object sender, EventArgs e)
{
double ID = 00001;
txtinvoiceno.Text = Convert.ToString(ID);
and
protected void btnsavef1_Click(object sender, EventArgs e)
{
ID++;
var no = Convert.ToInt32(ID);
txtinvoiceno.Text = Convert.ToString(no);
}
but it shows in the output only single digit 1, and increment it only once time i.e.2, and further increment is not working.
Anyone have any idea on this??
Thanks & Regards,
I doubt this will even compile, as within btnsavef1_Click the variable ID is not defined. You probably want ID to be an instance-variable:
class MyClass {
private int ID = 0;
protected void Page_Load(object sender, EventArgs e)
{
txtinvoiceno.Text = this.ID.ToString("D5");
}
protected void btnsavef1_Click(object sender, EventArgs e)
{
this.ID++;
txtinvoiceno.Text = this.ID.ToString("D5");
}
}
Furthermore you won´t actually store the formatted number. Simply store the number as integer and do the formatting within your events-code using ToString and format-specifiers.
Try this :
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack == false)
{
int ID = 1;
txtinvoiceno.Text = ID.ToString("D5");
}
}
protected void btnsavef1_Click(object sender, EventArgs e)
{
int ID = Convert.ToInt16(txtinvoiceno.Text );
ID++;
txtinvoiceno.Text = ID.ToString("D5");
}
}
You´re looking for formatting, which is very easy in your case:
//DONE: int, not double - do you want to deal with round-up errors?
//DONE: ID should be a field, not a local variable
private int ID = 0;
protected void Page_Load(object sender, EventArgs e)
{
// whenever you want 5 digits - just put 5 zeroes when formatting
txtinvoiceno.Text = ID.ToString("00000");
...
}
protected void btnsavef1_Click(object sender, EventArgs e)
{
ID += 1;
// once again: want 5 digits - put 5 zeroes when formatting
txtinvoiceno.Text = ID.ToString("00000");
}
A better choice, IMHO, is to convert ID into property and hide formatting there:
private int m_ID;
public int ID {
get {
return m_ID;
}
set {
m_ID = value;
txtinvoiceno.Text = m_ID.ToString("00000");
}
}
...
protected void Page_Load(object sender, EventArgs e) {
// Just assign
ID = 0;
...
}
protected void btnsavef1_Click(object sender, EventArgs e) {
// Just increment
ID += 1;
}
Try this :
public partial class Form2 : Form
{
private double ID=0;
public Form2()
{
InitializeComponent();
}
protected void Page_Load(object sender, EventArgs e)
{
double ID = 00001;
txtinvoiceno.Text = Strings.Format(ID, "000000")
}
protected void btnsavef1_Click(object sender, EventArgs e)
{
ID++;
var no = Convert.ToInt32(ID);
txtinvoiceno.Text = Strings.Format(no, "000000")
}
}
you need to format only result number after increment.
protected void btnsavef1_Click(object sender, EventArgs e)
{
ID++;
var no = Convert.ToInt32(ID);
txtinvoiceno.Text = no.ToString().PadLeft(5, '0');
}
if ID=7
your result will be after Increment
will be 00008