?
Solved

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

Posted on 2011-03-01
5
Medium Priority
?
358 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 2000 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

764 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