Solved

How do I implement following in ASP.NET, C#?

Posted on 2011-03-01
5
355 Views
Last Modified: 2012-05-11
Hi,

In my ASP.NET form I have 5 checkBoxes and 2 textBoxes... Depending on how many checkBoxes are checked, value is automatically entered into the textBoxes.

If 1 chkBox is checked, textBox1=1 and textBox2=0
If 2 chkBoxes are checked, textBox1=2 and textBox2=1
If 3 chkBoxes are checked, textBox1=3 and textBox2=2
......
And if no checkBox is checked, both textBoxes will either be empty or "0".

I know how to implement it using basic if.. else statement on CheckedChanged event of each checkBox...

protected void chkBox1_CheckedChanged(object sender, EventArgs e)
{
if (chkBox1.checked==true && chkBox2.checked==true )
textBox1.text="1";
textBox2.text="2";
}

And so on....

I have five checkBoxes and I have to write a lot of code for each CheckedChanged event.

Is there any other way I can implemet this?


Thank you.



0
Comment
Question by:pawar_deepak
5 Comments
 
LVL 9

Expert Comment

by:gdupadhyay
ID: 35011861
You can use java scrpits to fill the text box. But if you want to fill some data from data base you have to write event.

let me know how you want?
0
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 500 total points
ID: 35011901
Well for starters you can have each checkbox use the same event handler, you don't need one for each checkbox. Just set the CheckedChanged event for each checkbox to be the same value.

Secondly, you can probably get away with simply incrementing/decrementing the value on checked changed rather than having to check each checkbox individually.
Something like:
protected void CheckBox_CheckedChanged(object sender, EventArgs e)
{
    CheckBox current = (CheckBox)sender;
    int counter = 0;

    if (int.TryParse(textBox1.Text, out counter))
    {
         if (current.Checked)
               counter = counter + 1;
         else
               counter = counter - 1;

         // sanity check
         if (counter < 0) counter = 0;
    }
 
     textBox1.Text = counter.ToString();
     textBox2.Text = (counter + 1).ToString();
     
}

Open in new window

0
 

Author Comment

by:pawar_deepak
ID: 35012031
Hello gdupadhyay,

The values from textBoxes are stored in database after submitting the form. If user opens the form next time, previous values will be displayed... User can update those textBox values by checking/unchecking checkboxes....
0
 
LVL 9

Expert Comment

by:gdupadhyay
ID: 35012265
You can use below as example. Let me know if you are looking for different.
protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
    {
        if (CheckBox1.Checked)
        {
            TextBox1.Text = "1"; // assigned the data base fields
        }
        else
        {
            TextBox1.Text = "";
        }
    }

    


    protected void Button1_Click(object sender, EventArgs e)
    {

        if (CheckBox1.Checked)
        {
            // save the value in local variable
        }

        if (CheckBox2.Checked)
        {
            // save the value in local variable
        }

        if (CheckBox3.Checked)
        {
            // save the value in local variable
        }

        // Call daba base layer object for update

    }

Open in new window

0
 
LVL 33

Expert Comment

by:Todd Gerbert
ID: 35012370
I would expand slightly on carl_tawn's suggestion, and say that I would prefer to have each checkbox have a separate event handler - put the code that fills the text boxes into it's own method, and call that method from each of the event handlers.  This way it's a little more obvious to someone else (or yourself) reading your code what the intention is, plus gives you the flexibility to do other things differently in the event handlers.

Also borrowing from carl_tawn's suggestion...you can keep a class-level counter that's incremented everytime a checkbox is checked, and decremented each time a checkbox is un-checked- then you can just write checkedCount.ToString() to TextBox1 and (checkedCount - 1).ToString() to TextBox 2.

public partial class _Default : System.Web.UI.Page
{
	private static int checkedCount = 0;

	private void FillTextBoxes(bool Checked)
	{
		if (Checked)
			checkedCount++;
		else
			checkedCount--;

		TextBox1.Text = checkedCount.ToString();
		TextBox2.Text = (checkedCount <= 0 ? 0 : checkedCount - 1).ToString();
	}

	protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
	{
		FillTextBoxes(((CheckBox)sender).Checked);
	}
	protected void CheckBox2_CheckedChanged(object sender, EventArgs e)
	{
		FillTextBoxes(((CheckBox)sender).Checked);
	}
	protected void CheckBox3_CheckedChanged(object sender, EventArgs e)
	{
		FillTextBoxes(((CheckBox)sender).Checked);
	}
}

Open in new window

0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

827 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question