I want to save radio button state and select the radio button again on reloading the page. I have written the following code but it is not working:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Question_1 : System.Web.UI.Page
{
public int index;
public bool flag = false;
protected void Page_Load(object sender, EventArgs e)
{
if(flag)
{
index = (int)Session["index"];
if (index == 5)
{
totallyagree.Checked = true;
}
else if (index == 4)
{
agree.Checked = true;
}
}
}
protected void next_Click(object sender, EventArgs e)
{
flag=true;
if (totallyagree.Checked)
{
Session["index"] = 5;
}
else if (agree.Checked)
{
Session["index"] = 4;
}
Response.Redirect("Question 2.aspx");
}
protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
{
//Session["index"] = RadioButtonList1.SelectedIndex;
}
}
Please help me with this issue.
did you try IsPostBack ?
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
index = (int)Session["index"];
if (index == 5)
{
totallyagree.Checked = true;
}
else if (index == 4)
{
agree.Checked = true;
}
}
}
Related
I want to access the latest checkstates and the latest set date value using a function called GetProfileFilter. Now my problem is I'm not able to fetch the latest values when either the checkbox.Checkstate is changed or the datetimepicker is changed.I'm setting the values based on whether the checkboxes are set and I intend to use the IBitWise value elsewhere.Note that at form load all the checkboxes are checked.In the form class I have nothing more than a few variable and the event handlers for buttons and checkboxes and date time pickers.Open to all suggestions!!
class Profile{
public bool GetProfileFilter()
{
if (frmInactive.btnApplyWasClicked == true || frmInactive.btnCancelWasClicked == false)
{
frmInactive.ShowDialog();
MessageBox.Show("Here");
if (frmInactive.chkCancel.Checked == true)
{
MessageBox.Show("Cancel");
IBitWise += Canceled;
}
if (frmInactive.chkDiscon.Checked == true)
{
MessageBox.Show("Discon");
IBitWise += Discontinued;
}
if (frmInactive.chkVoidwoRes.Checked == true)
{
MessageBox.Show("Voidedwo");
IBitWise += VoidedWoutRes;
}
if (frmInactive.chkVoidwRes.Checked == true)
{
MessageBox.Show("Voidedw");
IBitWise += VoidedWRes;
}
MessageBox.Show("Ibit value:" + IBitWise);
return true;
}
else
{
return false;
}
}
}
public partial class FilterView : Form
{
Utilities Utilities = new Utilities();
//FilterView filterView = new FilterView();
public FilterView()
{
InitializeComponent();
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
}
private void checkBox2_CheckedChanged(object sender, EventArgs e)
{
}
private void checkBox3_CheckedChanged(object sender, EventArgs e)
{
}
private void checkBox4_CheckedChanged(object sender, EventArgs e)
{
}
private void checkBox5_CheckedChanged(object sender, EventArgs e)
{
}
private void rsetbtn_Click(object sender, EventArgs e)
{
}
private void aplybtn_Click(object sender, EventArgs e)
{
callonload();
}
private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
{
}
private void dateTimePicker2_ValueChanged(object sender, EventArgs e)
{
}
Here is sample code with checkbox made public
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Profile profile = new Profile(this);
profile.GetProfileFilter();
}
}
class Profile
{
Form1 form1;
public Profile(Form1 form1)
{
this.form1 = form1;
}
public bool GetProfileFilter()
{
form1.checkBox1.Checked = true;
return true;
}
}
}
Title explains it all. I am using this code at the top. It works. But you will see my comments at the bottom where it does not do anything. Everything else in the section works, just not that last line.
for testing, I created a separate button with the same line, it worked, but In the text changed handler for that specific text box, it just doesn't do anything.
thanks for the help.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Visitor_Tracking
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//if (double.Pharse txtboxVisitorCode.TextChanged += )
{
txtboxVisitorCode.Focus();
imgCheck.ImageUrl = "/images/Blank.jpg";
}
}
protected void Button1_Click(object sender, EventArgs e)
{
imgCheck.ImageUrl = "/images/GreenCheck.jpg";
}
protected void txtboxVisitorCode_TextChanged(object sender, EventArgs e)
{
if (txtboxVisitorCode.Text == "1234")
{
imgCheck.ImageUrl = "/images/GreenCheck.jpg";
lblPlate.Visible = true;
txtboxPlate.Visible = true;
btnPlate.Visible = true;
txtboxPlate.Focus();
}
else
{
imgCheck.ImageUrl = "/images/RedX.jpg";
txtboxVisitorCode.Text = "";
}
}
protected void txtboxPlate_TextChanged(object sender, EventArgs e)
{
lblPlate.Visible = false;
txtboxPlate.Visible = false;
txtboxPlate.Text = "";
txtboxVisitorCode.Text = "";
btnPlate.Visible = false;
btnPlate.Visible = false;
//
//
//
// this line does nothing
imgCheck.ImageUrl = "/images/Blank.jpg";
//
//
}
}
}
I wish to know how to store a VALUE to an item in a listbox. I'm currently working out a worksheet for my studies, and I've been wondering how to do this. Basically I need to store values to the Food (such as broccoli, bread. For example: Broccoli has a value of 20 calories).
The user must not see the value, only the program can store it.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace MCAST_Calorie_Counter
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
lbAvailable.Items.Clear();
if (comboBox1.Text == "Vegetables")
{
lbAvailable.Items.Add("Broccoli");
lbAvailable.Items.Add("Carrots");
lbAvailable.Items.Add("Lettuce");
lbAvailable.Items.Add("Onions");
lbAvailable.Items.Add("Potatoes");
}
if (comboBox1.Text == "Meat")
{
lbAvailable.Items.Add("Chicken");
lbAvailable.Items.Add("Veal");
lbAvailable.Items.Add("Beef");
lbAvailable.Items.Add("Fish");
}
if (comboBox1.Text == "Legumes")
{
lbAvailable.Items.Add("Bread");
lbAvailable.Items.Add("Peanuts");
lbAvailable.Items.Add("Green Peas");
lbAvailable.Items.Add("Lentils");
}
}
private void btnAdd_Click(object sender, EventArgs e)
{
ListBox.SelectedObjectCollection highlightedItems = lbAvailable.SelectedItems;
foreach (var item in highlightedItems)
{
lbChosen.Items.Add(item);
}
if (lbAvailable.SelectedItems.Count > 0)
{
lbAvailable.Items.Remove(lbAvailable.SelectedItems[0]);
}
}
private void btnRemove_Click(object sender, EventArgs e)
{
ListBox.SelectedObjectCollection highlightedItems = lbChosen.SelectedItems;
foreach (var item in highlightedItems)
{
lbAvailable.Items.Add(item);
}
if (lbChosen.SelectedItems.Count > 0)
{
lbChosen.Items.Remove(lbChosen.SelectedItems[0]);
}
}
private void btnClear_Click(object sender, EventArgs e)
{
lbAvailable.Items.Clear();
lbChosen.Items.Clear();
if (comboBox1.Text == "Vegetables")
{
lbAvailable.Items.Add("Broccoli");
lbAvailable.Items.Add("Carrots");
lbAvailable.Items.Add("Lettuce");
lbAvailable.Items.Add("Onions");
lbAvailable.Items.Add("Potatoes");
}
if (comboBox1.Text == "Meat")
{
lbAvailable.Items.Add("Chicken");
lbAvailable.Items.Add("Veal");
lbAvailable.Items.Add("Beef");
lbAvailable.Items.Add("Fish");
}
if (comboBox1.Text == "Legumes")
{
lbAvailable.Items.Add("Bread");
lbAvailable.Items.Add("Peanuts");
lbAvailable.Items.Add("Green Peas");
lbAvailable.Items.Add("Lentils");
}
}
}
}
Thanks alot for your help!
You can always add custom objects that support ToString() to the ListBox.Items collection:
struct Record
{
int value;
string label;
public override string ToString()
{
return label;
}
}
Adding a custom object:
Record record = new Record();
record.value = 1;
record.label = "This text will appear in the ListBox";
listBox.Items.Add(record);
Retrieving it:
Record selectedRecord = (Record)listBox.SelectedItem;
Console.WriteLine(selectedRecord.value); // => 1
When trying to run my ASP.NET program i am getting this error message below:
Compiler Error Message: CS1061: 'ASP.about_aspx' does not contain a definition for 'btnRunReports_Click' and no extension method 'btnRunReports_Click' accepting a first argument of type 'ASP.about_aspx' could be found (are you missing a using directive or an assembly reference?)
Line 18 below seems to be the problem but i have no idea why.
Line 16: </p>
Line 17:
Line 18: <asp:Button ID="btnRunReports" runat="server" Text="Run Reports" onclick="btnRunReports_Click" />
Line 19:
Line 20: <p><asp:Button ID="IdSort" runat="server" Text="Sort" onclick="IdSort_Click" />
On the run reports button i have this following code:
protected void btnRunReports_Click(object sender, EventArgs e)
{
RunReport();
}
and then
public void RunReport()
{
Application.Lock();
// lb1 = (SaleList)Application["SaleList"];
TextReportGenerator trg = new TextReportGenerator(saleList);
trg.GenerateAllReport("report.txt");
Application.UnLock();
}
i have no idea how to fix this error, i have no errors in the .cs just on that line 18, some guidance would be much appreciated.
This is the About.aspx.cs
`using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using Antiques;
using AntiqueSale;
namespace Antiques
{
public partial class About : System.Web.UI.Page, ISaleManagerUI
{
SaleList saleList;
protected void Page_Load(object sender, EventArgs e)
{
try
{
if (!ReferenceEquals(null, Session["ID"]))
{
Sale sale = new Sale((string)Session["ID"], (DateTime)Session["Date"], (string)Session["Location"], (double)Session["Picth Cost"], (int)Session["Num Pitches"],
(bool)Session["Charity"], (string)Session["Charity Name"], (bool)Session["Catering"]);
saleList = (SaleList)Application["SaleList"];
saleList.addSale(sale);
Application["SaleList"] = saleList;
UpdateListbox();
}
}
catch (DuplicateIdException)
{
UpdateListbox();
}
lblerror.Text = null;
}
public void LoadData()
{
try
{
Application.Lock();
SerializeFileHandler sr = new SerializeFileHandler();
Application["Antiques Sale"] = sr.ReadSaleListFromFile("data.dat");
UpdateListbox();
Application.UnLock();
}
catch (FileNotFoundException)
{
lblerror.Text = "Error: Not found, must save first";
}
}
protected void Load_Click(object sender, EventArgs e)
{
LoadData();
}
public void AddData()
{
var response = base.Response;
response.Redirect("Default.aspx", true);
}
protected void btnAddBox_Click(object sender, EventArgs e)
{
AddData();
}
public void getSale()
{
}
public void UpdateListbox()
{
Application.Lock();
lb1.Items.Clear();
saleList = (SaleList)Application["SaleList"];
for (int i = 0; i < saleList.Count(); i++)
{
// ListItem lst1 = new ListItem(lb1.saleList(i).ToString(), i.ToString());
// lst1.Items.Add(lb1);
}
Application.UnLock();
}
protected void lb1_Init(object sender, EventArgs e)
{
UpdateListbox();
}
protected void Delete_Click(object sender, EventArgs e)
{
while (lb1.SelectedIndex != -1)
{
ListItem mySelectedItem = (from ListItem li in lb1.Items where li.Selected == true select li).First();
lb1.Items.Remove(mySelectedItem);
}
}
protected void lb1_SelectedIndexChanged(object sender, EventArgs e)
{
}
public void SaveData()
{
saleList = (SaleList)Application["SaleList"];
if (saleList.Count() != 0)
{
Application.Lock();
SerializeFileHandler sr = new SerializeFileHandler();
sr.WriteSaleListToFile((SaleList)Application["SaleList"], "data.dat");
Application.UnLock();
}
else
{
lblerror.Text = "Error: You need to enter data into the list";
}
}
protected void Save_Click(object sender, EventArgs e)
{
SaveData();
}
protected void IdSort_Click(object sender, EventArgs e)
{
SortData();
}
public void SortData()
{
// var item = lb1.getItem(0);
// var index = item.get_index();
// lb1.reorderItem(item, index - 1);
}
public void RunReport()
{
Application.Lock();
// lb1 = (SaleList)Application["SaleList"];
TextReportGenerator trg = new TextReportGenerator(saleList);
trg.GenerateAllReport("report.txt");
Application.UnLock();
}
protected void btnRunReports_Click(object sender, EventArgs e)
{
RunReport();
}
}
}
`
Your Page Directive is wrong. It needs to be something like this:
<%# Page Title="About Us" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeBehind="Antiques.About.aspx.cs" Inherits="Antiques.About" %>
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class expt2 : System.Web.UI.Page
{
double result ;
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
result = 0.0;
protected void Chkbxbd_CheckedChanged(object sender, EventArgs e)
{
if (Chkbxbd.Checked)
{
txtbxttl.Text = "" + 10000;
result += double.Parse(txtbxttl.Text);
}
else
result = result - 10000;
}
protected void Chkbxsfa_CheckedChanged(object sender, EventArgs e)
{
if (Chkbxsfa.Checked)
{
txtbxttl.Text = "" + 15000;
result += double.Parse(txtbxttl.Text);
}
else
result = result - 15000;
}
protected void btnttl_Click(object sender, EventArgs e)
{
txtbxttl.Text = "" + result;
}
}
In this code the individual value for checkbox is ok but when the total is made it becomes 0.
Please help me to fix it.
The "result" value is not persisted through postbacks. Either recalculate it always when you need the final result or store it in the viewstate.
result variable will not be preerved in numereous post backs because the nature of web application is state less so tore the result in a ViewState or Session variable as shown below.
public partial class expt2 : System.Web.UI.Page
{
double result ;
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
Session["result"] = 0.0;
}
protected void Chkbxbd_CheckedChanged(object sender, EventArgs e)
{
if (Chkbxbd.Checked)
{
txtbxttl.Text = "" + 10000;
result = double.Parse(Session["result"].ToString());
result += double.Parse(txtbxttl.Text);
Session["result"] = result;
}
else
{
result = double.Parse(Session["result"].ToString());
result = result - 10000;
Session["result"]= result;
}
}
protected void Chkbxsfa_CheckedChanged(object sender, EventArgs e)
{
if (Chkbxsfa.Checked)
{
txtbxttl.Text = "" + 15000;
result = double.Parse(Session["result"].ToString());
result += double.Parse(txtbxttl.Text);
Session["result"] = result;
}
else
{
result = double.Parse(Session["result"].ToString());
result = result - 15000;
Session["result"] = result;
}
}
protected void btnttl_Click(object sender, EventArgs e)
{
txtbxttl.Text = "" + Session["result"].ToString();
}
}