I'm creating a simple patients billing program using Asp.net c#. Now, I have a dropdownlist with items "Ward" , "Semi Private" and "Private" If the user would choose ward the textbox below it would automatically be 700, if Semi Private it should be 1000 and if Private it should be 2000. I also have a textbox under it indicating how many days the patient stayed, the user is the one who should input the days, and for example he's from the ward room and the days he stayed was 3 then the computation should be 700 * 3. I also have a textbox that will display the answer. I hope u guys understand the things I explained above. So far, here are my codes:
Default.aspx
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
if (DropDownList1.Text == "Ward")
{
TextBox4.Text = "700";
}
if (DropDownList1.Text == "Semi Private")
{
TextBox4.Text = "1000";
}
if (DropDownList1.Text == "Private")
{
TextBox4.Text = "2000";
}
}
protected void Button1_Click(object sender, EventArgs e)
{
}
}
First of all add an attribute in your asp tag for dropdownlist like-
<asp:DropDownList ID="DropDownList1" runat="server" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" AutoPostBack="true">
Then in the event, write the following code-
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
string selected = DropDownList1.SelectedItem.ToString();
if(selected == "Ward")
{
int NetAmount = 700 * 3; //you can use any int variable in place of "3" as well
TextBox1.Text = NetAmount.ToString();
}
else if(selected == "Semi Ward")
{
TextBox1.Text = "1000";
}
else
{
TextBox1.Text = "2000"
}
}
That's it. Hope it will help.
Related
So I'm trying to make a simple banking website using C# and ASP.net and I'm just learning about Session Variables for the first time. I have a starting account balance of 1000 dollars and I want to transfer that to another page and have that balance updated via withdrawals or deposits and then update the balance. Heres my code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Project4.Forms
{
public partial class Services : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnSubmit_Click1(object sender, EventArgs e)
{
double userInput = double.Parse(txtAmount.Text);
Session["userInput"] = userInput;
Session["balance"] = 1000.00;
double balance = Convert.ToDouble(Session["balance"]);
Session["userAction"] = ddlSelectService.SelectedItem.Text;
if (ddlSelectService.SelectedItem.Text == "Withdrawl")
{
balance -= userInput;
}
else if (ddlSelectService.SelectedItem.Text == "Deposit")
{
balance += userInput;
}
else
{
}
Response.Redirect("Status.aspx");
}
}
}
I understand the root of my problem is probably the original Session["balance"] = 1000.00; but I'm not sure how to declare a starting amount and then it not affect the balance when I return to this page from the results page. Any help is appreciated, also as I said in the beginning this is a barebones simple atm website please don't suggest anything too crazy because I am a beginner.
I don't do asp.net, so this is just a guess, but maybe try only setting the default value if it's null, something like: if (Session["balance"] == null) Session["balance"] = 1000;. It also seems like this should be in the Page_Load event, not the btnSubmit_Click event, since it only has to happen once.
Finally, don't forget to update the session variable after setting the new balance:
protected void Page_Load(object sender, EventArgs e)
{
if (Session["balance"] == null) Session["balance"] = 1000;
}
protected void btnSubmit_Click1(object sender, EventArgs e)
{
double userInput;
if (!double.TryParse(txtAmount.Text, out userInput))
{
ClientScript.RegisterStartupScript(this.GetType(), "myalert",
"alert('Input must be a valid double');", true);
return;
}
Session["userInput"] = userInput;
double balance = Convert.ToDouble(Session["balance"]);
Session["userAction"] = ddlSelectService.SelectedItem.Text;
if (Session["userAction"] == "Withdrawl")
{
balance -= userInput;
}
else if (Session["userAction"] == "Deposit")
{
balance += userInput;
}
Session["balance"] = balance;
Response.Redirect("Status.aspx");
}
I have been developing a webpage which uses n number of dropdownlists which are binding dynamically inside a gridview. I want to perform operations based on the dropdownlist's selectedindexchanged event. I had done that and working good, but when I changed dropdownlist on second time It does postback but not calls the event.
You can see my code here
<%# Page Language="C#" AutoEventWireup="true" CodeFile="gridDropDownTest.aspx.cs"
Inherits="gridDropDownTest" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head runat="server"> <title></title></head><body> <form id="form1" runat="server"> <div> <asp:GridView ID="gridLedgeDetails" runat="server" OnRowDataBound="OnRowDataBound"
OnDataBound="gridLedgeDetails_DataBound"> </asp:GridView>
</div>
</form></body></html>
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
public partial class gridDropDownTest : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
databind();
}
public void databind()
{
DataTable dt = new DataTable();
dt.Columns.Add("Mode");
dt.Rows.Add("");
dt.Rows.Add("");
gridLedgeDetails.DataSource = dt;
gridLedgeDetails.DataBind();
}
protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DropDownList ddlMode = new DropDownList();
ddlMode.Width = 90;
ddlMode.Attributes.Add("style", "background-color:#ff6600;");
ddlMode.Items.Add("Regular");
ddlMode.Items.Add("Monthwise");
ddlMode.SelectedIndexChanged += new EventHandler(ddlMode_Indexchanged);
ddlMode.AutoPostBack = true;
ddlMode.ID = "ddlMode_";
e.Row.Cells[0].Controls.Add(ddlMode);
}
}
protected void gridLedgeDetails_DataBound(object sender, EventArgs e) { }
protected void ddlMode_Indexchanged(object sender, EventArgs e)
{
string uid = this.Page.Request.Params.Get("__EVENTTARGET");
if (uid != null && uid.Contains("ddlMode_"))
{
string[] values = uid.Split('$');
string row = values[1].Replace("ctl", "");
Control ctrl = Page.FindControl(uid);
DropDownList ddl = (DropDownList)ctrl;
if (ddl.SelectedIndex == 1)
{
}
}
}
}
enter image description here
For this you need to take and bind the dropdownlist again in Page_PreInit page method.
For Example....
protected void Page_PreInit(object sender, EventArgs e)
{
// here you need to build the gridview again.
// then the state will retain same......
}
I fill the textboxes on page_load event.Then I edit textboxes data and tried to update data. Variables are assigned with old values. How can I do to get the new values.
Here is my code behind
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Configuration;
using System.Data;
using System.Data.SqlClient;
public partial class mymembertype : System.Web.UI.Page
{
public static int mem_typeid;
protected void Page_Load(object sender, EventArgs e)
{
if (Session["valueid"] != null)
{
mem_typeid = (int)(Session["valueid"]);
string memtype_name = Convert.ToString(Session["valueName"]);
string rate = Convert.ToString(Session["rate"]);
txtmembtype.Text = Convert.ToString(memtype_name);
txtdscrate.Text = rate;
Insert_membertype.Text = "Update";
}
}
protected void Insert_membertype_Click(object sender, EventArgs e)
{
funtions fun = new funtions();
if (txtmembtype.Text != "" && txtdscrate.Text != "" )
{
if (Insert_membertype.Text == "Save")
{
string membetype = txtmembtype.Text;
int dscrate = Convert.ToInt32(txtdscrate.Text);
bool chk = fun.Insert_membertype(membetype, dscrate);
if (chk)
lblInfo.Text = " saving membertype successful";
else
lblInfo.Text = "Error saving membertype";
}
else
{
string membetype = txtmembtype.Text;
int dscrate = Convert.ToInt32(txtdscrate.Text);
bool chk = fun.Update_memberType(mem_typeid, membetype, dscrate);
if (chk)
lblInfo.Text = " Updating membertype successful";
else
lblInfo.Text = "Error Updating membertype";
}
}
}
}
As you see second condition block is for updating data. But it have only values in page load.Now new data is assigned. Please ....
Your problem is that Page Load is called before the event handler for your controls. If you want to understand more about the order things happen in ASP.net just Google "ASP.Net Page Lifecycle" - if you're going to be doing a lot of development I recommend at least getting a basic understanding of what ASP.Net is doing.
You'll want to change your page load to check if the current request is a postback.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack && Session["valueid"] != null)
{
// Doing stuff
}
}
The "IsPostBack" variable is false the first time the page is loaded, and then is true for every subsequent load.
Try if(!IsPostBack) method in Pageload event
You need to write the code inside page_load under IsPostback condition like below to get the updated values
if (!IsPostBack)
{
// Bind your code here
}
write a code GetData Method.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GetData();
}
}
public void GetData()
{
}
I need to know how can i store RadioButtonList items into my SQL db, already have this for TextBoxes:
public class Botones
{
public void SaveCVInfo2(string varOne,string varTwo, string varThree)
{
using (ConexionGeneralDataContext db = new ConexionGeneralDataContext())
{
Usuario_Web columna = new Usuario_Web();
//Add new values to each fields
columna.Nombre = varOne;
columna.Apellido = varTwo;
columna.Em_solicitado = varThree;
//and the rest where the textboxes would have been
//Insert the new Customer object
db.Usuario_Web.InsertOnSubmit(columna);
//Sumbit changes to the database
db.SubmitChanges();
}
}
}
Then reference them like this:
protected void Button1_Click(object sender, EventArgs e)
{
Botones botones = new Botones();
botones.SaveCVInfo2(nombre.Text, Apellidos.Text, EmpleoS.Text);
}
I need to know a way to add to the columna data from Radio Buttons in asp.net, while keeping this format of communication with the db.
BTW i have varchar tables for RadioButtonList data in my db.
I searched for some tutorials, but only found some old fashioned ADO connections examples, and i'm using Linq to SQL here.
This is a RadioButtonList I'm using in my aspx page:
<asp:RadioButtonList ID="RadioButtonList6" RepeatColumns = "2" RepeatDirection="Vertical" RepeatLayout="Table" runat="server">
<asp:ListItem ValidationGroup="Curriculum" style="margin-right:12px; margin-top:-10px" >Si</asp:ListItem>
<asp:ListItem ValidationGroup="Curriculum" >No</asp:ListItem>
</asp:RadioButtonList>
Alright, i solved it like this.
In my class:
using System.Xml.Linq;
using System.IO;
using System.Text.RegularExpressions;
using System.Net.Mail;
namespace Grupo_Zulcon
{
public partial class EnvianosTuCurriculum : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
Botones botones = new Botones();
botones.SaveCVInfo2(nombre.Text, Apellidos.Text, EmpleoS.Text, DireccionPersonal.Text, RadioButtonList6.SelectedItem.Text);
}
}
}
And in aspx.cs codebehind file:
protected void Button1_Click(object sender, EventArgs e)
{
Botones botones = new Botones();
botones.SaveCVInfo2(nombre.Text, Apellidos.Text, EmpleoS.Text, DireccionPersonal.Text, RadioButtonList6.SelectedItem.Text);
}
For anyone who might need it, thx.
AHHHHH ok this is driving me nuts.
Why when does my decimal point in the wrong place e.g.
if i have the string 567 in the textbox and click the decimal button i would expect (or i want) the textbox to change to 567. but instead i get .567
It only goes into the correct place when i add another number e.g. if i had the number 4 then straight after doing the above I'd get 567.4
Edit:
Heres my whole code:
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;
using System.IO;
namespace Calculator
{
public partial class frmCurrencyCalc : Form
{
public frmCurrencyCalc()
{
InitializeComponent();
}
private void cmdZero_Click(object sender, EventArgs e)
{
if (txtScreen.Text == "0")
{
txtScreen.Text = "0";
}
else
{
txtScreen.AppendText("0");
}
}
private void cmd1_Click(object sender, EventArgs e)
{
if (txtScreen.Text == "0")
{
txtScreen.Text = "1";
}
else
{
txtScreen.AppendText("1");
}
}
private void cmdTwo_Click(object sender, EventArgs e)
{
if (txtScreen.Text == "0")
{
txtScreen.Text = "2";
}
else
{
txtScreen.AppendText("2");
}
}
private void cmdThree_Click(object sender, EventArgs e)
{
if (txtScreen.Text == "0")
{
txtScreen.Text = "3";
}
else
{
txtScreen.AppendText("3");
}
}
private void cmdFour_Click(object sender, EventArgs e)
{
if (txtScreen.Text == "0")
{
txtScreen.Text = "4";
}
else
{
txtScreen.AppendText("4");
}
}
private void cmdFive_Click(object sender, EventArgs e)
{
if (txtScreen.Text == "0")
{
txtScreen.Text = "5";
}
else
{
txtScreen.AppendText("5");
}
}
private void cmdSix_Click(object sender, EventArgs e)
{
if (txtScreen.Text == "0")
{
txtScreen.Text = "6";
}
else
{
txtScreen.AppendText("6");
}
}
private void cmdSeven_Click(object sender, EventArgs e)
{
if (txtScreen.Text == "0")
{
txtScreen.Text = "7";
}
else
{
txtScreen.AppendText("7");
}
}
private void cmdEight_Click(object sender, EventArgs e)
{
if (txtScreen.Text == "0")
{
txtScreen.Text = "8";
}
else
{
txtScreen.AppendText("8");
}
}
private void cmdNine_Click(object sender, EventArgs e)
{
if (txtScreen.Text == "0")
{
txtScreen.Text = "9";
}
else
{
txtScreen.AppendText("9");
}
}
private void cmdDecimal_Click(object sender, EventArgs e)
{
txtScreen.AppendText(".");
cmdDecimal.Enabled = false;
}
private void cmdCancel_Click(object sender, EventArgs e)
{
txtScreen.Text = "0";
cmdDecimal.Enabled = true;
}
}
}
The RightToLeft looks to be your problem.
As described in MSDN,
The RightToLeft property is used for
international applications where the
language is written from right to
left, such as Hebrew or Arabic. When
this property is set to
RightToLeft..::.Yes, control elements
that include text are displayed from
right to left.
As one ofthe previous answers suggested, this should be set to false, but with TextAlign set to Right to mimic the appearance of a real calculator.
My advice is -- define a business layer. In your case -- a double variable. Upon button clicks, update the variable first. Then format the value.
My advice is to set TextAlign to Right, but leave RightToLeft set to No.
Edit: Having said that, this issue may be unrelated to these settings.
I remember a friend having this a bug similar to this back in early 2009 in Visual Studio 2008 on Windows Vista. Strangely enough, the same problem did not occur on the same version of Visual Studio on Windows XP.
If you haven't updated Visual Studio / .NET 3.5 to Service Pack 1, I suggest doing that and seeing if it fixes the problem.
Perhaps try a different method:
private void AddDecimal()
{
txtScreen.SelectionLength = txtScreen.TextLength;
txtScreen.SelectedText += ".";
}
(Also is your text box, text aligment, right aligned... if not that may contribute to your problem.)
I think you have a few things here.
By the looks of it, you've set:
txtScreen.right to left = true;
If you append just the decimal point, you get the result you describe. Try using something like:
txtScreen.AppendText(".00");
This will give you the result you are describing.
You could be opening a can of worms. When you start formatting the textbox, you are changing it from holding a value to presentation. Eg:
decimal value = 567;
txtScreen.Text = value.ToString("0.00");
Then you will have to start writing crazy validation rules to avoid values like 567.00.1 etc.
Just to let you all know who are interested.
I managed to fix it somehow. All I did was delete the right to left thing in the design code and then realigned it (using the GUI) to right and its worked...odd as I did nothing different to last time...
Oh well
Thank you all for your help
Much aprreciated
x
You can try this one, it works for me:
private void btndot_Click(object sender, EventArgs e)
{
if (txtbox.Text == "0" && txtbox.Text != null)
{
txtbox.Text = ".";
}
else
{
txtbox.Text = txtbox.Text + ".";
}
}