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;
}
Related
My problem is losing value.I have a DataGrid with standart asp.net pagination. When I change page index, global variable named "id" loses its value. Help me Please.
int id = 0;
void Payments()
{
radioBtnList = GetData();
radioBtnList.DataBind();
}
protected void Page_Load(object sender, EventArgs e)
{
Payments();
Response.Write(id); // I get value 0 :(
}
protected void radioBtnList_Changed(object sender, EventArgs e)
{
id = int.Parse(radioBtnList.SelectedItem.Text);
}
protected void dgw_pagechange(object source, DataGridPageChangedEventArgs e)
{
dgw.CurrentPageIndex = e.NewPageIndex;
dgw.DataBind();
}
Instead of storing the ID in the page which will be reloaded when the page changes you should store it somewhere that will persist over the postback.
Examples might be:
The session object
A database
A text file
You can use ViewState like this.
int id = 0;
void Payments ()
{
radioBtnList = GetData();
radioBtnList.DataBind ();
}
protected void Page_Load (object sender, EventArgs e)
{
Payments();
Response.Write(ViewState["id"]);
}
protected void radioBtnList_Changed (object sender, EventArgs e)
{
id = int.Parse (radioBtnList.SelectedItem.Text);
ViewState["id"]=id;
}
protected void dgw_pagechange (object source, DataGridPageChangedEventArgs e)
{
dgw.CurrentPageIndex = e.NewPageIndex;
dgw.DataBind();
}
I have a gridview with records from a sql table. I have set up the webpage in a 3 tier architecture such that when I choose to edit a particular record from the gridview it gives me a formview populated with the details of the record I selected in the gridview.When I click the update linkbutton after changing some details on the formview, the gridview is displayed with a blank record of the selected row.I need this row on the gridview to have the data.
Please help.
My code behind.
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
BindGrid();
fvProfiles.Visible = false;
}
}
protected void BindGrid()
{
gvProfiles.DataSource = new UserBO().GetAllUsers();
gvProfiles.DataBind();
}
protected void ShowGrid()
{
fvProfiles.Visible = false;
gvProfiles.Visible = true;
btnAdd.Visible = true;
}
protected void HideGrid()
{
fvProfiles.Visible = true;
gvProfiles.Visible = false;
btnAdd.Visible = false;
}
protected void btnAdd_Click(object sender, EventArgs e)
{
HideGrid();
fvProfiles.ChangeMode(FormViewMode.Insert);
}
protected void InsertButton_Click(object sender, EventArgs e)
{
ShowGrid();
}
protected void InsertCancelButton_Click(object sender, EventArgs e)
{
ShowGrid();
}
protected void gvProfiles_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName =="Edit")
{
gvProfiles.Visible = false;
fvProfiles.Visible = true;
btnAdd.Visible = false;
fvProfiles.ChangeMode(FormViewMode.Edit);
odsProfiles.SelectParameters["UserId"].DefaultValue = e.CommandArgument.ToString();
odsProfiles.DataBind();
}
if (e.CommandName == "LogicalDelete")
{
int i = Convert.ToInt32(e.CommandArgument);
//call the userBO method to logically delete the record.
//new UserBO().DeleteUser(i);
}
if (e.CommandName == "Delete")
{
int i = Convert.ToInt32(e.CommandArgument);
//call the userBO method to delete the record.
new UserBO().DeleteUser(i);
}
}
protected void gvProfiles_RowEditing(object sender, GridViewEditEventArgs e)
{
BindGrid();
}
protected void UpdateButton_Click(object sender, EventArgs e)
{
ShowGrid();
}
protected void UpdateCancelButton_Click(object sender, EventArgs e)
{
ShowGrid();
}
protected void gvProfiles_RowUpdated(object sender, GridViewUpdatedEventArgs e)
{
BindGrid();
}
}
odsprofiles is an objectdatasource.
Try clearing the gv rows before databinding it like so:
protected void BindGrid()
{
gvProfiles.DataSource = new UserBO().GetAllUsers();
gvProfiles.Rows.Clear();
gvProfiles.DataBind();
}
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.
I greatly appreciate any help. I have 20+ buttons, each with a word, or a space or period. Each time I click on a button, the pre-existing word is wiped out and replaced with the new word. I need each word and/or space to remain in place until I click the "Clear" button.
Maybe this has been previously asked/answered under different search terms? I tend to believe I need to identify a string variable, but have no idea how to begin.
==============
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 frmSentenceBuilder : Form
{
public frmSentenceBuilder()
{
InitializeComponent();
}
private void frmSentenceBuilder_Load(object sender, EventArgs e)
{
}
private void btnA_Click(object sender, EventArgs e)
{
lblSentenceText.Text = btnA.Text;
}
private void btn_a_Click(object sender, EventArgs e)
{
lblSentenceText.Text = btn_a.Text;
}
private void btnAn_Click(object sender, EventArgs e)
{
lblSentenceText.Text = btnAn.Text;
}
private void btn_an_Click(object sender, EventArgs e)
{
lblSentenceText.Text = btn_an.Text;
}
private void btnThe_Click(object sender, EventArgs e)
{
lblSentenceText.Text = btnThe.Text;
}
private void btn_the_Click(object sender, EventArgs e)
{
lblSentenceText.Text = btn_the.Text;
}
private void btnman_Click(object sender, EventArgs e)
{
lblSentenceText.Text = btnman.Text;
}
private void btnwoman_Click(object sender, EventArgs e)
{
lblSentenceText.Text = btnwoman.Text;
}
private void btndog_Click(object sender, EventArgs e)
{
lblSentenceText.Text = btndog.Text;
}
private void btncat_Click(object sender, EventArgs e)
{
lblSentenceText.Text = btncat.Text;
}
private void btncar_Click(object sender, EventArgs e)
{
lblSentenceText.Text = btncar.Text;
}
private void btnbicycle_Click(object sender, EventArgs e)
{
lblSentenceText.Text = btnbicycle.Text;
}
private void btnbeautiful_Click(object sender, EventArgs e)
{
lblSentenceText.Text = btnbeautiful.Text;
}
private void btnbig_Click(object sender, EventArgs e)
{
lblSentenceText.Text = btnbig.Text;
}
private void btnsmall_Click(object sender, EventArgs e)
{
lblSentenceText.Text = btnsmall.Text;
}
private void btnstrange_Click(object sender, EventArgs e)
{
lblSentenceText.Text = btnstrange.Text;
}
private void btnlookedat_Click(object sender, EventArgs e)
{
lblSentenceText.Text = btnlookedat.Text;
}
private void btnrode_Click(object sender, EventArgs e)
{
lblSentenceText.Text = btnrode.Text;
}
private void btnspoketo_Click(object sender, EventArgs e)
{
lblSentenceText.Text = btnspoketo.Text;
}
private void btnlaughedat_Click(object sender, EventArgs e)
{
lblSentenceText.Text = btnlaughedat.Text;
}
private void btndrove_Click(object sender, EventArgs e)
{
lblSentenceText.Text = btndrove.Text;
}
private void btnSpace_Click(object sender, EventArgs e)
{
lblSentenceText.Text = " ";
}
private void btnperiod_Click(object sender, EventArgs e)
{
lblSentenceText.Text = btnperiod.Text;
}
private void btnexclam_Click(object sender, EventArgs e)
{
lblSentenceText.Text = btnexclam.Text;
}
private void btnClear_Click(object sender, EventArgs e)
{
lblSentenceText.Text = "";
}
private void btnExit_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
Use lb1SentenceText.Text+=whatever.Text;.
+= is equivalent to lbSentence.Text = lblSentenceText.Text + whatever.Text.
Basically, it appends or concatenates the right hand side string to the string on the left hand side. Hope it makes sense?
So:
string rhs="Hello " ;
string lhs = "World";
string rhs = rhs + lhs;//Hello World
Please be inspired. You have a lot of redundant code.
btnA.Click += AppendButtonText;
btn_a.Click += AppendButtonText;
...
private void AppendButtonText(object sender, EventArgs e)
{
var button = sender as Button;
if (button != null)
{
lblSentenceText.Text += button.Text;
}
}
On the button click events, change it to += instead of = (except in the clear button). This is equivalent to writing something = something + newValue;.
Try:
lb1SentenceText.Text = lb1SentenceText.Text + *something*.text
The += operator means add to, and the variable modified is equal to the added (Int, String) appended to the original value (x = 1; x += 3; x is now 4)
Try this:
button.Click += new System.EventHandler(ButtonClick);
button1.Click += new System.EventHandler(ButtonClick);
// And for each button, one of those.
private void ButtonClick(object sender, System.EventArgs e)
{
// Do whatever you want to do here, you can place the TEXT to be appended on the button, if so:
lb1SentenceText.Text += sender.Text;
}
//Simple.Create a global variable and within each button click event do this;
string yourStrVar = ""; //Must be global
yourStrVar+= ((Button)sender).Text
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);
}