populate text box with blank value

IHi could someone help me with this bit of code please, I want is to if the dropdown list value is 0 the textbox is populated with the number 0.

Similarly if the drop down list value = 1, the text box is populated with the number 1



private void PopulateSingleNumbers()
    {
        if (ddlNoAnswers.SelectedValue == 0)
        {
            tbText = "0";
        }
 if (ddlNoAnswers.SelectedValue == 1)
        {
            tbText = "1";
        }









getting errors
Operator '==' cannot be applied to operands of type 'string' and 'int'








    }

bailey1200Asked:
Who is Participating?
 
Roshan DavisConnect With a Mentor Commented:
or something like this?
private void PopulateSingleNumbers() 
{ 
	TextBox tbAnswer;

	foreach (RepeaterItem item in gvPossibleAnswers.Items)
	{
		tbAnswer = item.FindControl("tbAnswer") as TextBox;
		if(tbAnswer != null)
			break;
	}

	if(tbAnswer == null)
	{
		MessageBox.Show("Error");
		return;
	}
	if (Int32.Parse(ddlNoAnswers.SelectedValue) == 0) 
	{ 
		tbAnswer.Text = "0"; 
	} 
	if (Int32.Parse(ddlNoAnswers.SelectedValue) == 1) 
	{ 
		tbAnswer.Text = "1"; 

Open in new window

0
 
VenkatgviCommented:
Instead of using a function use the SelectedValueChanged event of the dropdownlist

sample code below

private void ddlNoAnswers_SelectedValueChanged(object sender, EventArgs e)
        {
            tbText .Text = comboBox1.Text;
        }
0
 
Roshan DavisConnect With a Mentor Commented:
you can try either of the following
private void PopulateSingleNumbers()
{
if (ddlNoAnswers.SelectedValue.CompareTo("0") == 0)
{
	tbText = "0";
}
if (ddlNoAnswers.SelectedValue.CompareTo("0") == 0)
{
	tbText = "1";
}



 ======== OR ===========


private void PopulateSingleNumbers()
{
if (Int32.Parse(ddlNoAnswers.SelectedValue) == 0)
{
	tbText = "0";
}
if (Int32.Parse(ddlNoAnswers.SelectedValue) == 1)
{
	tbText = "1";
}

Open in new window

0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
Roshan DavisCommented:
oops, some typo. Here is the corrected one
private void PopulateSingleNumbers()
{
	if (ddlNoAnswers.SelectedValue.CompareTo("0") == 0)
	{
		tbText = "0";
	}
	if (ddlNoAnswers.SelectedValue.CompareTo("1") == 0)
	{
		tbText = "1";
	}



OR


private void PopulateSingleNumbers()
{
	if (Int32.Parse(ddlNoAnswers.SelectedValue) == 0)
	{
		tbText = "0";
	}
	if (Int32.Parse(ddlNoAnswers.SelectedValue) == 1)
	{
		tbText = "1";
	}

Open in new window

0
 
Roshan DavisCommented:
or you can do this also
private void PopulateSingleNumbers()
{
     tbText = ddlNoAnswers.SelectedValue;

Open in new window

0
 
VenkatgviCommented:
Use the below code, wherever u need to update the value of dropdown to the textbox

   tbText .Text = ddlNoAnswers.Text;
0
 
bailey1200Author Commented:
hi thank, for that I forgor that the tb is in a repeater...
how can I work that in to the code so is exist in the current context??

Thanks

I have something like.....
 foreach (RepeaterItem item in gvPossibleAnswers.Items)
        {
            TextBox tb = (TextBox)item.FindControl("tbAnswer")    
        }
0
 
Roshan DavisCommented:
something like this?
foreach (RepeaterItem item in gvPossibleAnswers.Items)
        {
            TextBox tb = (TextBox)item.FindControl("tbAnswer");
            if(tb != null)
            {            
               tb.Text = ddlNoAnswers.SelectedValue; //or  ddlNoAnswers.Text
            }
        }

Open in new window

0
 
bailey1200Author Commented:
yeah something like that but I need to work that in to this...


private void PopulateSingleNumbers()
{
if (Int32.Parse(ddlNoAnswers.SelectedValue) == 0)
{
       tbAnswer= "0";
}
if (Int32.Parse(ddlNoAnswers.SelectedValue) == 1)
{
       tbAnswer= "1";
0
 
Roshan DavisCommented:
something like this?
private void PopulateSingleNumbers() 
{ 
	TextBox tbAnswer;

	foreach (RepeaterItem item in gvPossibleAnswers.Items)
	{
		tbAnswer = item.FindControl("tbAnswer") as TextBox;
	}

	if(tbAnswer == null)
	{
		MessageBox.Show("Error");
		return;
	}
	if (Int32.Parse(ddlNoAnswers.SelectedValue) == 0) 
	{ 
		tbAnswer.Text = "0"; 
	} 
	if (Int32.Parse(ddlNoAnswers.SelectedValue) == 1) 
	{ 
		tbAnswer.Text = "1"; 

Open in new window

0
All Courses

From novice to tech pro — start learning today.