Anuj
Anuj

Reputation: 1526

find controls from emptydatatemplate in gridview

i am using EmptyDataTemplate for entering new data in grid while there is no existing data,but i am not able to find my controls in the EmptyDateTemplate

protected void gvNavigationDtls_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName.Equals("EInsert"))
        {
            GridViewRow emptyrow = gvNavigationDtls.Controls[0].Controls[0] as GridViewRow;
            if (((TextBox)emptyrow.FindControl("txtCode")).Text == "")

in page load also i checked by writing following code

gvNavigationDtls.DataBind();
            Control c = gvNavigationDtls.Controls[0].FindControl("txtCode");
            if (c != null)
            {
            }

but c is null,that means i am not able to find control to use it, Please help,Thanks in Advance

Upvotes: 1

Views: 7539

Answers (3)

user15759356
user15759356

Reputation:

For me, the above code does not work. I have to access the empty template and find the control like this:

TextBox tb1 = (TextBox)((System.Web.UI.Control)e.CommandSource).BindingContainer.Controls[0].FindControl("TextBox5");

The bindingcontainer of the "e.commandsource" is a gridviewrow (then, via the quickwatch you should find the container on which apply findcontrol by going deeper in the "controls" members)

Upvotes: 1

Adam
Adam

Reputation: 411

Actually, just had this problem and it's really easy to find access the data but it's in the RowDataBound event.

    Protected Sub grdView(sender As Object, e As GridViewRowEventArgs) Handles grdView.RowDataBound
        If e.Row.RowType = DataControlRowType.EmptyDataRow Then
            Dim txt As TextBox = e.Row.FindControl("txtBox")
            txt.Text = "Hey, this works!"
        End If
    End Sub

Upvotes: 1

Avinash Jha
Avinash Jha

Reputation: 127

protected void gvNavigationDtls_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName.Equals("EInsert"))
        {
          TextBox txtCode=(TextBox)gvNavigationDtls.Controls[0].Controls[0].FindControl("txtCode");
          txtCode.Text="Your value";
        }
     }

Upvotes: 3

Related Questions