views:

369

answers:

2

Having:

<asp:FormView ID="frmEmployee" runat="server">
    <EditTemplate>
        <asp:TextBox ID="txtFirstName" runat="server" />
    </EditTemplate>
</asp:FormView>

one uses FindControl to reference the txtFirstName textbox in the code-behind file:

VB.Net
Dim txtFirstName As TextBox = CType(Page.FindControl("txtFirstName"), TextBox)
txtFirstName.Text = "George"

C#
TextBox txtFirstName = (TextBox)Page.FindControl("txtFirstName");
txtFirstName.Text = "George";

is there a way to reference this control statically, without having to use FindControl?

VB.Net
txtFirstName.Text = "George"

C#
txtFirstName.Text = "George";
A: 

Not that I know of. Are you trying to reference statically so the compiler will validate you are referencing valid controls at compile-time, or trying to reference it statically so it is easier to work with?

If it is the latter I ended up just creating a function that gets and sets values for me in a FormView. Here is the basic layout:

public string GetValue(string id, ref FormView fv)
{
    Control ctrl = fv.FindControl(id);
    string value = "";

    if(ctrl is TextBox)
    {
       TextBox tb = (TextBox)ctrl);
       value = tb.Text;
    }
    else if(ctrl is DropDownList)
    {
        DropDownList ddl = (DropDownList)ctrl;
        value = ddl.SelectedValue;
    }
    else if( ...
    ...
    ...

    return(value);
}

public void SetValue(string id, string value, ref FormView fv)
{
    Control ctrl = fv.FindControl(id);

    if(ctrl is TextBox)
    {
       TextBox tb = (TextBox)ctrl);
       tb.Text = value;
    }
    else if(ctrl is DropDownList)
    {
        DropDownList ddl = (DropDownList)ctrl;
        ddl.SelectedValue = value;
    }
    else if( ...
    ...
    ...
}

Hope that helps.

Austin
A: 

yes, you can, put your EditTemplate in AJAX updatepanel, In this way you will get all the controls directly, without using findcontrol method...........

Muhammad Akhtar