This is a migrated thread and some comments may be shown as answers.

Pre-populate a textbox on insert new

4 Answers 152 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Kati
Top achievements
Rank 1
Kati asked on 05 Oct 2012, 11:19 AM

I need to add a default value to one of the textboxes when adding a new item in the grid

<telerik:GridTemplateColumn HeaderText="Activity ID" SortExpression="activityID" UniqueName="activityID"  >

                    <ItemTemplate>

                        <%# Eval("activityID")%>

                    </ItemTemplate>

                    <EditItemTemplate>

                        <asp:TextBox runat="server" ID="txtActivity" Text='<%# Bind("activityID") %>' />

                        <asp:RequiredFieldValidator runat="server" ID="rfvActivity" ControlToValidate="txtActivity" Display="Dynamic" ErrorMessage="*" />

                    </EditItemTemplate>

                </telerik:GridTemplateColumn>

 

 

When clicking “Add new recored” I need to pre-populate the txtActivity textbox. How can I access this from the code behind?

 

I tried this with no success

protected void RadGrid1_ItemCommand(object source, GridCommandEventArgs e)

        {

            if (e.CommandName == RadGrid.InitInsertCommandName)

            {

                GridEditableItem editedItem = e.Item as GridEditableItem;

                TextBox t = (TextBox)(editedItem.FindControl("txtActivity"));

            }

        }

 

And this:

 

protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)

        {

            if (e.Item is GridCommandItem && e.Item.OwnerTableView.IsItemInserted)

            {               

                TextBox txtActivity = (TextBox)e.Item.FindControl("txtActivity");

                txtActivity.Text = "300";

            }

}

I am running out of ideas.

Please help. Thanks

4 Answers, 1 is accepted

Sort by
0
Jayesh Goyani
Top achievements
Rank 2
answered on 05 Oct 2012, 11:29 AM
Hello,

Try with below code snippet.

protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
   {
       if (e.Item is GridDataItem)
       {
           GridDataItem item = e.Item as GridDataItem;
 
       }
 
       if (e.Item is GridEditableItem && e.Item.IsInEditMode)
       {
           if (e.Item is GridEditFormInsertItem)
              //  if (e.Item is GridDataInsertItem) // EditMode="inplace"
           {
               GridEditableItem editedItem = e.Item as GridEditableItem;
               TextBox txtActivity = (TextBox)editedItem.FindControl("txtActivity");
 
               txtActivity.Text = "300";
           }
 
       }
   }


Thanks,
Jayesh Goyani
0
Shinu
Top achievements
Rank 2
answered on 05 Oct 2012, 12:57 PM
Hi,

Please take a look into the following code snippet.

C#:
protected void RadGrid1_ItemCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
{
    if (e.CommandName == RadGrid.InitInsertCommandName)
    {
        e.Canceled = true;
        e.Item.OwnerTableView.InsertItem();
        GridEditableItem insertedItem = e.Item.OwnerTableView.GetInsertItem();
        GridEditFormItem editFormItem = insertedItem as GridEditFormItem;
        TextBox t = (TextBox)(editFormItem.FindControl("txtActivity"));
        t.Text = "hello";
    }
}

Thanks,
Shinu.
0
Kati
Top achievements
Rank 1
answered on 08 Oct 2012, 04:53 PM
sorry, but none of the solutions you posted work :(

Object reference not set to an instance of an object.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:

Line 54:         GridEditableItem insertedItem = e.Item.OwnerTableView.GetInsertItem();
Line 55:         GridEditFormItem editFormItem = insertedItem as GridEditFormItem;
Line 56: TextBox t = (TextBox)(editFormItem.FindControl("txtActivity"));Line 57:         t.Text = "hello";
Line 58:     }
0
Kati
Top achievements
Rank 1
answered on 08 Oct 2012, 05:01 PM
I found the problem

GridEditableItem insertedItem = e.Item.OwnerTableView.GetInsertItem();
   GridEditFormItem editFormItem = insertedItem as GridEditFormItem;
        TextBox t = (TextBox)(editFormItem.FindControl("txtActivity"));



on line 3 editFormItem  should be insertedItem


Thanks :)
Tags
Grid
Asked by
Kati
Top achievements
Rank 1
Answers by
Jayesh Goyani
Top achievements
Rank 2
Shinu
Top achievements
Rank 2
Kati
Top achievements
Rank 1
Share this question
or