2

I ran into an issue trying to find a textbox control on the same page. It keeps giving me a null when I debug it.

Here is my code:

public void UpdateTimeLog(string input)
    {
        string timeNumber = "txtTime" + input;
        TextBox myTextbox = (TextBox)FindControl(timeNumber);

        sqlConnection.Open();
        using (var command = new SqlCommand("UPDATE [JobSheet] SET [Time" + input + "]=@Time" + input + " WHERE [JobShtId]=@JobShtId", sqlConnection))
        {
            command.Parameters.AddWithValue("@JobShtId", jobSheetId);
            command.Parameters.AddWithValue("@Time" + input + "", myTextbox.Text);
            command.ExecuteNonQuery();
        }
        sqlConnection.Close();
    }

Problem Solved: I didn't specify that the textbox control was in a container(e.g. ContentPlaceHolder1).

Here is the corrected code:

public void UpdateTimeLog(string input)
    {
        var container = Master.FindControl("ContentPlaceHolder1");
        string timeNumber = "txtTime" + input;
        TextBox myTextbox = (TextBox)container.FindControl(timeNumber);

        sqlConnection.Open();
        using (var command = new SqlCommand("UPDATE [JobSheet] SET [Time" + input + "]=@Time" + input + " WHERE [JobShtId]=@JobShtId", sqlConnection))
        {
            command.Parameters.AddWithValue("@JobShtId", jobSheetId);
            command.Parameters.AddWithValue("@Time" + input + "", myTextbox.Text);
            command.ExecuteNonQuery();
        }
        sqlConnection.Close();
    }
Ron T
  • 397
  • 1
  • 4
  • 22
  • Instead of find control.. Please try Page.FindControl & this.Page.FindControl and let me know if it helps you – maulik sakhare Apr 07 '16 at 04:45
  • Where is the textbox defined? is it in some other container control like gridview? – Adil Apr 07 '16 at 04:47
  • I did that but it didn't work. I found out why it kept returning null, and the reason was because I didn't specify that the control was in a container. For example, the ContentPlaceHolder1 – Ron T Apr 07 '16 at 04:47
  • Please try it should work.... var container = Master.FindControl("ContentPlaceHolder1"); var control = container.FindControl("txtNaam1"); – maulik sakhare Apr 07 '16 at 04:48
  • Thank you @maulik sakhare, I just realized that a few minutes ago! – Ron T Apr 07 '16 at 04:51

3 Answers3

1

you should just send your textbox as the sender object like this:

UpdateTimeLog("2",(TextBox)sender);

of course you'll need to modify your method too to accept the TextBox object.

and this only makes sense when you're using the same event handler for more than one text box.

Gubr
  • 324
  • 1
  • 7
  • 15
1

it should be Work try it,

 TextBox myTextBox = (TextBox)(this.Controls[("txtTime" + input)]).Text);
Prabhat Sinha
  • 1,500
  • 20
  • 32
1

It is difficult to find control by ID.so try this code

public static Control[] FlattenHierachy(Control root)
        {
            List<Control> list = new List<Control>();
            list.Add(root);
            if (root.HasControls())
            {
                foreach (Control control in root.Controls)
                {
                    list.AddRange(FlattenHierachy(control));
                }
            }
            return list.ToArray();
        }

and

  public void UpdateTimeLog(string input)
        {
            string timeNumber = "txtTime" + input;
           // TextBox myTextbox = (TextBox)FindControl(timeNumber);
            Control[] allControls = FlattenHierachy(Page);
            foreach (Control control in allControls)
            {
                TextBox textBox = control as TextBox;
                if (textBox != null && textBox.ID == timeNumber)
                {
                    textBox.Text = "Hello";//Do your other stuff
                }
            }

          //Rest is ommited
        }
Syed Mhamudul Hasan
  • 1,341
  • 2
  • 17
  • 45