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

Find TextBox in inline edit mode

11 Answers 164 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Mattias
Top achievements
Rank 1
Mattias asked on 28 Aug 2008, 06:52 PM
Hi,
I have problem to find my textbox, how do I find it?
The code works when inserting but not when updating.
I'm getting: object reference not set to an instance of an object.
<Columns> 
         <telerik:GridEditCommandColumn ButtonType="ImageButton" UniqueName="EditCommandColumn"
                        <ItemStyle CssClass="MyImageButton" /> 
                    </telerik:GridEditCommandColumn> 
        <telerik:GridTemplateColumn HeaderText="Namn" UniqueName="Name"
        <ItemTemplate><asp:Label runat="server" ID="Name" Text='<%# Eval("Name") %>'></asp:Label></ItemTemplate
        <EditItemTemplate> 
            <asp:TextBox ID="Name" runat="server" Text='<%# Bind("Name") %>'></asp:TextBox> 
         <asp:RequiredFieldValidator ID="RequiredFieldValidator1" ControlToValidate="Name" 
                ErrorMessage="obligatorisk" runat="server" Text="*" Display="Dynamic" /> 
        </EditItemTemplate> 
        </telerik:GridTemplateColumn> 
        <telerik:GridCheckBoxColumn UniqueName="IsEnabled" HeaderText="Aktiverad" DataField="IsEnabled" /> 
    </Columns> 


protected void RadGrid1_UpdateCommand(object source, GridCommandEventArgs e) 
        { 
            GridEditableItem editedItem = e.Item as GridEditableItem; 
 
            Client c = (Client)e.Item.DataItem; 
            c.Name = ((TextBox)editedItem.FindControl("Name")).Text.Trim(); 
            c.IsEnabled = ((GridBoolColumnEditor)editedItem.EditManager.GetColumnEditor("IsEnabled")).Value; 
 
            try 
            { 
                ClientBL.SaveOrUpdate(c); 
            } 
            catch 
            { 
                Msg.Show("Fel uppstod."); 
            } 
        } 

11 Answers, 1 is accepted

Sort by
0
Mattias
Top achievements
Rank 1
answered on 28 Aug 2008, 07:20 PM
Hmm, I have 5 rows in the grid and when I print this code in the update method:
Response.Write(editedItem["IsEnabled"].Controls.Count.ToString()); 

I get 1. And that is correct.
But when I print the "Name" control, I get 5. Why?
0
Mattias
Top achievements
Rank 1
answered on 28 Aug 2008, 07:46 PM
Many questions! :)
This seems to work:
string name = (editedItem["Name"].FindControl("Name"as TextBox).Text; 

Now the problem is that I can't get the rows datasource:
Client c = (Client)e.Item.DataItem;  
I have also tried with:
Client c = (Client)editedItem.DataItem;
But it won't work!


0
Vlad
Telerik team
answered on 29 Aug 2008, 08:23 AM
Hello Mattias,

Generally e.Item.DataItem is available only during data-binding (ItemDataBound). On post back you can use DataKeyNames/Values:

....
<MasterTableView DataKeyNames="YourFieldName" ....
......
and in UpdateCommand:

object value = null;
if(e.Item is GridDataItem)
{
   value = (e.Item as GridDataItem).GetDataKeyValue("YourFieldName");
}
else if(e.Item is GridEditFormItem)
{
    value = (e.Item as GridEditFormItem).ParentItem.GetDataKeyValue("YourFieldName");
}


Kind regards,
Vlad
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Mattias
Top achievements
Rank 1
answered on 29 Aug 2008, 08:48 AM
Hmm, ok!
So, what choices do I have in the update method?
1. Collect every property that the 'Client' domain object contains like in your example.
2. Get the datakey of the row and get an extra roundtripp to the database and fill my Client.
3. ???

I don't like either no 1 or 2.
If Client contains say 50 properties the update method would be really ugly!
And an extra roundtripp is not "clean" solution. But compare no 1 and 2, I guess I go for no 2.

Isn't there a way to get the rows dataitem like in MS grid?
0
Vlad
Telerik team
answered on 29 Aug 2008, 10:40 AM
Hi Mattias,

The situation in MS grid is exactly the same.

Greetings,
Vlad
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Mattias
Top achievements
Rank 1
answered on 29 Aug 2008, 01:05 PM
Ohh, ok!
I was almost certain it was possible with MS. My fault!

One more question:
Is there some default way to get the controls from the edited row?
Why I'm asking is that I'm getting reference error with this code:
c.IsEnabled = (editedItem["IsEnabled"].FindControl("IsEnabled"as CheckBox).Checked; 

But with the textbox it works fine.
And vice versa, when typing ...Controls[0]... the checkbox works but not the textbox.

Is it just when using template fields Controls[0] is not working?
0
Missing User
answered on 29 Aug 2008, 02:54 PM
Hello Mattias,

You can review the following help article: Accessing cells and rows

All the best,
Plamen
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Mattias
Top achievements
Rank 1
answered on 29 Aug 2008, 03:28 PM
Thank you Plamen! :)
I have read the page but there is still one thing I don't understand.
I'm getting object reference error (again).
I want to fill a GridBoundColumn with text from a dropdownlist when clicking on "add new row".
Isn't the GridBoundColumn a Label maybe?

<telerik:GridBoundColumn UniqueName="ClientName" DataField="Client.Name" HeaderText="Kund" ReadOnly="true"></telerik:GridBoundColumn> 


        protected void RadGrid1_ItemCommand(object source, GridCommandEventArgs e) 
        { 
            if (e.CommandName == RadGrid.InitInsertCommandName) //"Add new" button clicked 
            { 
                GridDataItem gdi = e.Item as GridDataItem; 
                (gdi["ClientName"].Controls[0] as Label).Text = DDL_Clients.SelectedItem.Text; 
 
            } 
        } 

0
Missing User
answered on 29 Aug 2008, 03:41 PM
Hello Mattias,


Please find attached a sample web application that demonstrates the needed approach.

Best wishes,
Plamen
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Mattias
Top achievements
Rank 1
answered on 29 Aug 2008, 04:33 PM
Almost there! :)
I had to rewrite a little:
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e) 
        { 
            if (e.Item.IsInEditMode && (e.Item is GridDataInsertItem)) 
            { 
                GridDataInsertItem gridEditFormItem = (GridDataInsertItem)e.Item; 
                TextBox clientName = (TextBox)gridEditFormItem["ClientName"].Controls[0]; 
                clientName.Text = "Test"
                //clientName.ReadOnly = true; 
            } 
        } 

This works. But:
I can't set the textbox to readonly in design mode, then the text doesn't show.
And trying to set readonly in codebehind in the code above seems to have no effect.
Any ideas?
0
Shinu
Top achievements
Rank 2
answered on 01 Sep 2008, 08:03 AM
Hi Mattias,

Try the following code snippet to set the ReadOnly property for the TextBox in insert mode.

CS:
 protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e) 
    { 
        if ((e.Item is GridEditFormInsertItem) && (e.Item.OwnerTableView.IsItemInserted)) 
        { 
            GridEditFormInsertItem gridEditFormItem = (GridEditFormInsertItem)e.Item; 
            TextBox clientName = (TextBox)gridEditFormItem["ColumnUniqueName"].Controls[0]; 
            clientName.Text = "Test"
            clientName.ReadOnly = true;  
        } 
    } 


Thanks
Shinu.
Tags
Grid
Asked by
Mattias
Top achievements
Rank 1
Answers by
Mattias
Top achievements
Rank 1
Vlad
Telerik team
Missing User
Shinu
Top achievements
Rank 2
Share this question
or