Livio
Livio

Reputation: 75

How to access controls inside DataRepeater?

I'm trying to get a value from a DataRepeater Control in C# The old program (written in VB) did this with this instruction:

If CBool(e.DataRepeaterItem.Controls(Temp_S.Name).Text) = True Then
  e.DataRepeaterItem.Controls(T_S.Name).Text = "S"
  e.DataRepeaterItem.Controls(T_S.Name).BackColor = Color.Yellow
  e.DataRepeaterItem.Controls(T_S.Name).ForeColor = Color.Black
Else
  e.DataRepeaterItem.Controls(T_S.Name).BackColor = Color.White
  e.DataRepeaterItem.Controls(T_S.Name).ForeColor = Color.White
End If

I try with the same instruction in C# but without result. The error that appear is:

"Non-invocable member 'Control.controls' cannot be used like a method."

Edit: I found a solution but it still doesn't work even if it doesn't produce errors and, very strange, if I put a stop point inside the sub for inspect it the point is never reached! It appears like this sub is never reached!

private void DataRepeater_Buoni_DrawItem(object sender, Microsoft.VisualBasic.PowerPacks.DataRepeaterItemEventArgs e)
{
  // Accende S
  if (Convert.ToBoolean(e.DataRepeaterItem.Controls["t_Buono.Name"].Text = "10268"))
  {
    e.DataRepeaterItem.Controls["t_S.Name"].Text = "S";
    e.DataRepeaterItem.Controls["t_S.Name"].BackColor = Color.Yellow;
    e.DataRepeaterItem.Controls["t_S.Name"].ForeColor = Color.Black;
  }
  else
  {
    e.DataRepeaterItem.Controls[t_S.Name].BackColor = Color.White;
    e.DataRepeaterItem.Controls[t_S.Name].ForeColor = Color.White;
  }
}

Upvotes: 0

Views: 75

Answers (2)

Livio
Livio

Reputation: 75

FINALLY IT WORKS!!!!!

if (e.DataRepeaterItem.Controls["temp_S"].Text == "True")

The problem was the .Name joined with the textbox name. Thanks to everybody for the support!

Upvotes: 0

Amit Mohanty
Amit Mohanty

Reputation: 1880

Try this: (e.DataRepeaterItem.Controls[Temp_S.Name] as Control).Text

Modified Code:

// Change TextBox to the actual type of your control
var control = e.DataRepeaterItem.Controls[Temp_S.Name] as TextBox; 
if (control != null)
{
    string textValue = control.Text;
}

Upvotes: 1

Related Questions