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");
}
}
Related
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;
}
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
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++;
}
}
private void showCategory_Load(object sender, EventArgs e)
{
string categoryId = "100"
}
private void button1_Click(object sender, EventArgs e)
{
textBox1.Text = categoryId;
}
how to pass string categoryId from form load to button click?
Thank you.
If it's Windows Forms application than using the variable on the class level, not in the function
public partial class showCategory : Form
{
string categoryId = null;
private void showCategory_Load(object sender, EventArgs e)
{
this.categoryId = "100";
}
private void button1_Click(object sender, EventArgs e)
{
textBox1.Text = this.categoryId;
}
}
If it's Web Application - store the value the some state callection: ViewState, Session, HiddenField and so on..