Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

value of variable

Status
Not open for further replies.

neomax

Programmer
Jul 1, 2006
29
BG
public class ButtonTest : Page
{
protected RadioButton a1;
protected RadioButton a2;
protected RadioButton a3;
protected Label message;
protected Button Button4;
int X1;
int X2;
int X3;

public void Page_Init(Object sender, EventArgs e)
{
EventHandler selectionHandler =
new EventHandler(RadioChangeQuestion1);
EventHandler selectHandler =
new EventHandler(RadioChangeQuestion2);
EventHandler selHandler =
new EventHandler(RadioChangeQuestion3);
EventHandler sHandler =
new EventHandler(Collback);

a1.CheckedChanged += selectionHandler;
a2.CheckedChanged += selectionHandler;
a3.CheckedChanged += selectionHandle;
Button4.Click += sHandler;
}

public void RadioChangeQuestion1(Object sender, EventArgs e)
{
RadioButton checkedButton = null;
if (a1.Checked)
{
checkedButton= a1;
}
else if (a2.Checked)
{
checkedButton = a2;
}
else if (a3.Checked)
{
checkedButton = a3;
}
if (checkedButton != null)
{
string y = checkedButton.Text;
X1 = Convert.ToInt32(y);

// here everything is allright, X1=5
}

// Button click event
public void Collback (Object sender, EventArgs e)
{
message.Text = "Sum" +(X1).ToString();
}
}


That is only part of code. Please, Help me ! why when I call method Collback the result is "Sum 0", but in method RadioChangeQuestion1, the variable X1 has value ( for a example X1=5)?????


 
your X1 is not persisting across postback maybe and cannot then be shared across subs...

maybe try turning off autopostback of your radio buttons and just using your Collback event using a string function to return the value.

Code:
    public  void Collback (Object sender, EventArgs e)
        {
            object cbVal = checkVal();
            if (cbVal != null){
                 message.Text = "Sum" + cbVal;
                 ifIntValueNeededHere = Convert.ToInt32(cbVal);
            }
            else
            {
                message.Text = "No value selected";
            }
        }
    } 

    string checkVal() 
    { 
          RadioButton checkedButton = null;
            if (a1.Checked)
            {
                checkedButton= a1;
            }
            else if (a2.Checked)
            {
                checkedButton = a2;
            }
            else if (a3.Checked)
            {
                checkedButton = a3;
            }
            if (checkedButton != null)
            {
                checkVal = checkedButton.Text;
            }
            else
            {
                checkVal = System.DBNull.Value;
            }
     return checkVal; 
    }
 
oops, cant do

string checkVal() {

needed to do

object checkVal() {
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top