When I attempt to have CheckBoxList1.SelectedItem.Value, it gives me errors. I am a bit confused and looking through this Murach ASP.Net book but I do not see any answers. I am wanting to have what is selected in my ListBox and what is selected in my CheckBox and have them added. Any help?
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
int ucost = int.Parse(DropDownList1.SelectedItem.Value);
int ccost = int.Parse(//what does in here???);
ttbox.Text = ccost.ToString();
}
protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
TextBox1.Text = "";
TextBox2.Text = "";
TextBox3.Text = "";
TextBox4.Text = "";
ttbox.Text = "";
}
protected void ttbox_TextChanged(object sender, EventArgs e)
{
}
protected void DropDownList1_SelectedIndexChanged1(object sender, EventArgs e)
{
}
protected void CheckBoxList1_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
This is a an update. They say a picture is worth a thousand words.
If you want to get all selected checkbox, you can use:
var selected = CheckBoxList1.Items.Cast<ListItem>().Where(x => x.Selected).ToList();
If you want to get only count of selected:
var count = CheckBoxList1.Items.Cast<ListItem>().Count(x => x.Selected)
If you want to get last or first element, you can use First or Last linq methods.
Related
Is there a way to create a class or something that can contain all these different textboxes. I just want to know if this is the most efficient way I can code this.
private void textBox1_Click(object sender, EventArgs e)
{
textBox1.Text = "";
}
private void textBox2_Click(object sender, EventArgs e)
{
textBox2.Text = "";
}
private void textBox3_Click(object sender, EventArgs e)
{
textBox3.Text = "";
}
private void textBox4_Click(object sender, EventArgs e)
{
textBox4.Text = "";
}
private void textBox5_Click(object sender, EventArgs e)
{
textBox5.Text = "";
}
Don't create separated event handlers for each TextBoxes. Instead, create a combined event for all the TextBoxes: textBox1..textBox5.
Then
private void textBoxs_Click(object sender, EventArgs e)
{
if (sender is TextBox box) box.Text = "";
}
You can add the event handler to all the TextBoxes on load, eg
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
foreach(var tb in this.Controls.OfType<TextBox>())
{
tb.Click += (sender, eventArgs) => ((TextBox)sender).Text = "";
}
}
I'm using two buttons for "on" and "off". When I press "On" button, it passes a string value "1", and pressing the "Off" button passes the string value "2".
How can I do this within one button? By default button should be off, when I press it should on and when i press again it should off.
public partial class _Default : System.Web.UI.Page
{
SerialPort ardo;
protected void Page_Load(object sender, EventArgs e)
{
ardo = new SerialPort();
ardo.PortName = "COM5";
ardo.BaudRate = 9600;
}
protected void Button1_Click(object sender, EventArgs e)
{
string stop = "1";
ardo.Open();
ardo.Write(stop);
ardo.Close();
}
protected void Button2_Click(object sender, EventArgs e)
{
string On = "2";
ardo.Open();
ardo.Write(On);
ardo.Close();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
//consider initially its stopped
if(Session[“currentState”] == null)
Session[“currentState”] = “1”;
if(Session[“currentState”].ToString() == “1”)
{
Session[“currentState”]= “2”;
}
else
{
Session[“currentState”]= “1”;
}
ardo.Open();
ardo.Write(Session[“currentState”].ToString());
ardo.Close();
}
it set to hidden text box.
then you can check if statement it is pressed or not
protected void Button1_Click(object sender, EventArgs e)
{
// hf means hidden textbox
if(hf.text==1){
// your on code
hf.text=0;
}
if(hf.text==0){
//your off code
hf.text=1;
}
}
Declare a string variable
private string isOn = "2";
and then on the button click event you can handle it like
protected void Button1_Click(object sender, EventArgs e)
{
if(isOn.Equals("2"))
isOn = "1";
else
isOn = "2";
}
Imagine I have a button which has " OnClick="GoBack" " I want it to go to the previous page, how will C# function code look like?
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
ViewState["RefUrl"] = Request.UrlReferrer.ToString();
}
}
protected void GoBack_Click(object sender, EventArgs e)
{
object refUrl = ViewState["RefUrl"];
if (refUrl != null)
{
Response.Redirect((string)refUrl);
}
}
I have a form on a .aspx page with a RadioButtonList. I populate the list using an ArrayList and BindData(). When I try to get the selected value of the list I get a null object.
protected void Page_Load(object sender, EventArgs e)
{
q_LBL.Text = "What is the right answer?";
ArrayList options = new ArrayList();
options.Add("a");
options.Add("b");
options.Add("c");
options.Add("d");
options.TrimToSize();
options_RBL.DataSource = options;
options_RBL.DataBind();
}
protected void submit_BTN_Click(object sender, EventArgs e)
{
fb_LBL.Text = options_RBL.SelectedValue;
}
try this
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
{
q_LBL.Text = "What is the right answer?";
ArrayList options = new ArrayList();
options.Add("a");
options.Add("b");
options.Add("c");
options.Add("d");
options.TrimToSize();
options_RBL.DataSource = options;
options_RBL.DataBind();
}
}
protected void submit_BTN_Click(object sender, EventArgs e)
{
fb_LBL.Text = options_RBL.SelectedValue;
}
hello i m doing a very simple Asp.net application project
namespace WebApplication1
{
public partial class WebUserControl1 : System.Web.UI.UserControl
{
market m = new market();
protected void Page_Load(object sender, EventArgs e)
{
}
protected void button_clickSell(object sender, EventArgs e)
{
float price = float.Parse(this.BoxIdPrezzo.Text);
m.insertProd("xxx", 10, "yyy");
m.addOfferForProd("ooo", 5, "gggg");
m.insertProd(this.BoxIdDescrizione.Text,price,this.BoxIdUtente.Text);
String s;
m.outMarket(out s);
this.Output.Text = s; //the output here work good
this.Output.Visible = true;
}
protected void button_clickView(object sender, EventArgs e)
{
String s;
m.outMarket(out s);
this.Output.Text = s; // here seem to have lost the reference to product why?
this.Output.Visible = true;
}
}
}
the problem is that when i click on button1 which call button_clickSell everything works good but when i click on button2 which call button_clickView products seem to not be anymore in the Market object, but this is pretty strange because in market object i have a list of product and m.outMarket in the first time work propely.
That is because of how pages work. Every time you make a request or a post-back to the page the values will be lost in that variable.
You will need to hold that in a session or something similar.
Here is a very basic example of using a session.
protected void Page_Load(object sender, EventArgs e)
{
if (Session["Collection"] == null)
{
Session["Collection"] = new List<int>();
}//if
}
protected void button_clickSell(object sender, EventArgs e)
{
List<int> collection = (List<int>)Session["Collection"];
collection.Add(7);
collection.Add(9);
}
protected void button_clickView(object sender, EventArgs e)
{
List<int> collection = (List<int>)Session["Collection"];
collection.Add(10);
}
you can view this post on MSDN: ASP.NET Session State Overview
Session should be used when information is required across the
pages. Now the matter for the two buttons lying on the same page. So
ViewState is Best option.
protected void Page_Load(object sender, EventArgs e)
{
if (ViewState["Collection"] == null)
{
ViewState["Collection"] = new List<int>();
}//if
}
protected void button_clickSell(object sender, EventArgs e)
{
List<int> collection = (List<int>)ViewState["Collection"];
collection.Add(7);
collection.Add(9);
}
protected void button_clickView(object sender, EventArgs e)
{
List<int> collection = (List<int>)ViewState["Collection"];
collection.Add(10);
}