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

[Solved] Cannot get values from FormTemplate in Grid

9 Answers 346 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Elena Lavrinenko
Top achievements
Rank 1
Elena Lavrinenko asked on 12 Sep 2009, 05:29 PM
I try to use FormTemplate in Grid:
 
<telerik:radgrid ID="RadGrid1" runat="server" GridLines="None" ShowGroupPanel="True" 
            Width="99%" AllowSorting="True" AllowMultiRowSelection="false" allowpaging="True" 
            pagesize="6" skin="Outlook" OnNeedDataSource="RadGrid1_NeedDataSource"   
            HorizontalAlign="NotSet" onitemcommand="ChangeAccount" 
              
        >                       
            <MasterTableView AutoGenerateColumns="false" DataKeyNames="Username"   
                CommandItemDisplay="Bottom">  
                <PagerStyle Mode="NextPrevAndNumeric" /> 
                <Columns> 
                    <telerik:GridEditCommandColumn ButtonType="ImageButton" /> 
                    <telerik:gridboundcolumn headertext="Last Name" datafield="lastname" /> 
                    <telerik:gridboundcolumn headertext="First Name" datafield="firstname" /> 
                    <telerik:gridboundcolumn headertext="Phone #" datafield="Telephone"/>  
                    <telerik:gridboundcolumn headertext="Email" datafield="email" /> 
                    <telerik:GridButtonColumn headertext="Lock" CommandName="Lock" ButtonType="ImageButton"   
...etc.                  
</Columns> 
 
                <EditFormSettings EditFormType="Template">  
                        <FormTemplate> 
                            <table cellspacing="7" cellpadding="1" width="100%">  
                                <tr><td colspan="2"><b>Username:&nbsp;<span id="spanUname" runat="server"><%# Eval("username") %></span></b> 
                                    <asp:TextBox Width="200px" ID="tbUname" runat="server"/></td></tr>  
                                <tr><td><table class="update"><tr> 
...etc 
...etc.

Form works perfectly.

When I try to get values on update as shown in the example http://www.telerik.com/help/aspnet-ajax/grdupdatingvaluesusercontrolformtemplate.html:
 

 

 

 

 

 

switch (e.CommandName)  
            {  
                 case RadGrid.InitInsertCommandName:  
                GridEditableItem editedItem = e.Item as GridEditableItem;  
                string s = (editedItem.FindControl("tbUname"as TextBox).Text;  
...etc. 

It gives me NullReferenceException, because editedItem is null. When I try to debug, there are absolutely no Controls for e.Item.
I am confused how I can get values from my Textboxes in FormTemplate.
Elena

 

 

 

 

 

 

 

 

 

9 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 14 Sep 2009, 04:56 AM
Hello Elena,

              The Edit and InitInsert command events are too early inorder to access controls in the FormTemplate since the controls are not rendered then. If you want to access control values after making changes, then you can make use of the Update command or PerformInsert command as shown below:
c#:
switch (e.CommandName)   
            {   
               case RadGrid.PerformInsertCommandName:   
                GridEditableItem editedItem = e.Item as GridEditableItem;   
                string s = (editedItem.FindControl("tbUname"as TextBox).Text; 
               .... 

              Inorder to access the control values on clicking the Edit button or on clicking the AddNewRecord button, you can make use of the ItemDataBound event as shown in the code below:
c#:
 protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e) 
    { 
        if (e.Item is GridEditableItem && e.Item.IsInEditMode)//clicking Edit button 
        { 
            GridEditableItem editedItem = (GridEditableItem)e.Item;             
            string s = (editedItem.FindControl("tbUname"as TextBox).Text; 
        } 
 
        if (e.Item is GridEditableItem && e.Item.OwnerTableView.IsItemInserted)//clicking AddNewRecord button 
        { 
            GridEditableItem editedItem = (GridEditableItem)e.Item; 
            TextBox txtbx = editedItem.FindControl("tbUname"as TextBox;          
        } 
    } 

Hope this helps..
Princy.
0
Elena Lavrinenko
Top achievements
Rank 1
answered on 14 Sep 2009, 08:04 PM
Thanks for your reply, Princy.

However, I need to render the look of the page in FormTemplate. For example, if I have 'Selected' item in dropdownlist, or Check in checkbox, the page gives me an error when I try to execute Add command, because these values are not setup.
What should I do in this case? I try to make controls visible if I need them, and I fail.
I can generate empty string and than add it for Insert Command, but It seems very complicated: Add the empty string to datasource and be able to open it on Add Comand...I don't think it is right approach...
can you advice please?

Elena 
0
Accepted
Princy
Top achievements
Rank 2
answered on 15 Sep 2009, 06:40 AM
Hello Elena,

The error happens to occur since the grid  cannot bind a value for the newly inserted item. You can try out the following code to fix the this error:

For instance, if you have a dropdownlist or checkbox in EditMode bound as shown below:

aspx:

      <asp:DropDownList id="ddList1"  runat="server" DataTextField="Country" DataSource='<%# LoadCountryNames()  %>' 
         SelectedValue='<%# Bind("ColumnDataField1")%>' /> 
 
     <asp:CheckBox ID="chkBoxEdit" runat="server" Checked='<%# Bind("ColumnDataField2") %>' /> 
 

Then you can preset the default value of the controls when binding to a grid item on the RadGrid.InitInsertCommandName command as shown below:

c#:

protected void RadGrid1_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)  
 {  
           if (e.CommandName == RadGrid.InitInsertCommandName)  
           {                
               e.Canceled = true;  
                 
               System.Collections.Specialized.ListDictionary newValues = new  
               System.Collections.Specialized.ListDictionary();  
               newValues[ "ColumnDataField1"] = " ";  
               newValues[ "ColumnDataField2"] = "False"
   
               e.Item.OwnerTableView.InsertItem(newValues);  
           }  
 }   

Hope this helps you out:)
Princy.
0
Elena Lavrinenko
Top achievements
Rank 1
answered on 19 Sep 2009, 05:25 PM
Thanks, Princy, that helped.
0
Princy
Top achievements
Rank 2
answered on 21 Sep 2009, 07:43 AM
Hello Elena,

You would also be able to find more explanation on the above code or error in the following document. Probably this may help you understand things better :)
Known reasons for error messages

Thanks
Princy.
0
Elena Lavrinenko
Top achievements
Rank 1
answered on 21 Sep 2009, 02:18 PM
Yes, Princy, that answered the first part of my question. Now I can make form to work with the empty values in the owner table. However, the second part of the question still remains: I cannot grab values on "PerformInsert" or "Edit" because inserted/edited item is not reachable. RowIndex is -1, e.Item.ItemIndex is -1, and, of cource, no controls inside.
What is wrong with this?
I didn't want to use objectdatasource (of cource, I can try) due to the application architecture. And I need to understand how to manage my data.
0
Elena Lavrinenko
Top achievements
Rank 1
answered on 21 Sep 2009, 03:55 PM
One more thing. I tried to change datasource to objectdatasourse.
<asp:objectdatasource id="gridDS" typename="BusinessLogic.Locations" selectmethod="getEmployees" insertmethod="insertEmployee">  
   <insertparameters> 
      <asp:controlparameter controlid="tblastname" propertyname="text" name="lastName" type="String"/>  
   </insertparameters> 
</asp:objectdatasource> 
It gives me the same error- JScript, cannot find control tblastname...
0
Elena Lavrinenko
Top achievements
Rank 1
answered on 21 Sep 2009, 11:16 PM
I forgot to mention 1 thing:
I tried your approach Posted on Sep 13. For the PerfornInsert it gives me an error - NullReferenceException (see my previous posts),
for the ItemDataBound after I click Insert button, I cannot take textbox values - it never comes there(

e.Item never

is GridEditableItem && e.Item.OwnerTableView.IsItemInserted=false ). Do I miss something? Can the issue be related  to using Master page?

0
Elena Lavrinenko
Top achievements
Rank 1
answered on 22 Sep 2009, 06:32 PM
I accept your previous answer as you gave me direction to make a recearch; however, there is no clear explanation on the difference between commands when they are rendering controls. After some experiments I found that all .net related code should be performed not onitemcommand - at this stage controls still do not exist, and not ondatabound or ondatabinding - at this time it is too late, and there are no edited items anymore. So if I need to use commands that I created myself I should invent some artificial container to pass data. Values from controls I found in specialized commands binded to grid, such as oninsertcommand, onupdatecommand etc. 
It was a little confusing in documentation and examples.

Elena
Tags
Grid
Asked by
Elena Lavrinenko
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Elena Lavrinenko
Top achievements
Rank 1
Share this question
or