Solved

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

Posted on 2011-03-01
5
357 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

726 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