Solved

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

Posted on 2011-03-01
5
350 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

707 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now