daveredfern
daveredfern

Reputation: 1255

Get text value of input from FindControl

I know now normally you can get the value of a text input using the following:

txtName.Text

But because my input is inside of a LoginView I am using FindControl like this:

LoginView1.FindControl("txtComment")

This successfully find the text input but returns its type rather than the value. Adding the Text function at the end does not work.

Upvotes: 5

Views: 32689

Answers (2)

Jeremy B.
Jeremy B.

Reputation: 9216

It has been a while since I used the controls, but i believe it is:

string text = ((TextBox)LoginView1.FindControl("txtComment")).Text;

Upvotes: 3

hunter
hunter

Reputation: 63512

Try casting that Control to TextBox. FindControl returns a Control which doesn't have the Text property

TextBox txtName = LoginView1.FindControl("txtComment") as TextBox;
if (txtName != null)
{
    return txtName.Value;
}

Upvotes: 7

Related Questions