Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 364
  • Last Modified:

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

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
pawar_deepak
Asked:
pawar_deepak
1 Solution
 
gdupadhyayCommented:
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
 
Carl TawnSystems and Integration DeveloperCommented:
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
 
pawar_deepakAuthor Commented:
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
 
gdupadhyayCommented:
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
 
Todd GerbertIT ConsultantCommented:
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now