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

Insert with ObjectDataSource and FormTemplate

8 Answers 213 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Andrey
Top achievements
Rank 1
Andrey asked on 15 Apr 2008, 11:44 AM
I have RadGrid with FormTemplate, InsertButton (CommandName = "PerformInsert") in  FormTemplate and ObjectDataSource.

When I try to insert record I get exception "ObjectDataSource 'odsUsers' has no values to insert. Check that the 'values' dictionary contains values.".
Why?

Update works, but insert works only with AutoGenerated form.

8 Answers, 1 is accepted

Sort by
0
Andrey
Top achievements
Rank 1
answered on 15 Apr 2008, 12:15 PM

This exception occurs only when I use asp:Table in FormTemplate.
And Bind works as Eval.


<EditFormSettings EditFormType="Template">

<FormTemplate>

<asp:Table ID="tblUSerEdit" runat="server">

<asp:TableRow>

<asp:TableCell>

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

<asp:LinkButton ID="btnInsert" runat="server" Text="Insert" CommandName="PerformInsert" Visible='<%#((int)Eval("Id")) == 0 %>' />

<asp:LinkButton ID="btnUpdate" runat="server" Text="Update" CommandName="Update" Visible='<%#((int)Eval("Id")) != 0 %>' />

</asp:TableCell>

</asp:TableRow>

</asp:Table>

</FormTemplate>

</EditFormSettings>

 

0
Andrey
Top achievements
Rank 1
answered on 15 Apr 2008, 01:12 PM
Bind works as Eval in any runat="server" controls.

<

FormTemplate>

<table>

<tr runat="server" visible='<%#((int)Eval("Id")) == 0 %>' >

<td>

Password:

</td>

<td>

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

<asp:RequiredFieldValidator ID="rfvPassword" runat="server" Text="*" ControlToValidate="txtPassword" />

</td>

</tr>

</table>

</FormTemplate>


Password is null in ObjectDataSource Insert Method.
If I remove runat="server" from
<tr runat="server" visible='<%#((int)Eval("Id")) == 0 %>' >
password will be valid, but I need to hide it in EditMode
0
Vlad
Telerik team
answered on 18 Apr 2008, 08:07 AM
Hello Andrey,

I tried to reproduce this however everything worked fine on my end - you can check attached example.

Sincerely yours,
Vlad
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center
0
Andrey
Top achievements
Rank 1
answered on 20 Apr 2008, 02:41 PM
Hello, Vlad.
Thanks for your response.

Please, replace your FormTable with

<FormTemplate>

<table>

<tr runat="server">

<td>

<asp:Panel ID="Panel1" runat="server">

<asp:TextBox ID="TextBox1" Text='<%# Bind("ID") %>' ReadOnly="<%# !(Container is GridEditFormInsertItem)%>" runat="server" />

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

</asp:Panel>

<asp:LinkButton ID="LinkButton1" runat="server" Text='<%# (Container is GridEditFormInsertItem) ? "Insert" : "Update" %>'

CommandName='<%# (Container is GridEditFormInsertItem) ? "PerformInsert" : "Update" %>' />

<asp:LinkButton ID="LinkButton2" runat="server" Text="Cancel" CommandName="Cancel" />

</td>

</tr>

</table>

</FormTemplate>


tr must be runat="server". You will see that 'id' will be 0 and 'name' will be null in MyUpdateMethod.

I use Telerik.Web.UI 2008.1.415.35.



0
Vlad
Telerik team
answered on 21 Apr 2008, 01:53 PM
Hello Andrey,

I checked this with standard FormView control and it seems that your are right - the situation is the same as with RadGrid:
<asp:FormView ID="FormView1" DataSourceID="ObjectDataSource2" DefaultMode="Insert" runat="server"
            <InsertItemTemplate> 
                <table> 
                    <tr id="Tr1" runat="server"
                        <td> 
                            <asp:Panel ID="Panel1" runat="server"
                                <asp:TextBox ID="TextBox1" Text='<%# Bind("ID") %>' runat="server" /> 
                                <asp:TextBox ID="TextBox2" Text='<%# Bind("Name") %>' runat="server" /> 
                            </asp:Panel> 
                            <asp:LinkButton ID="LinkButton1" runat="server" Text="Insert" CommandName="Insert" /> 
                            <asp:LinkButton ID="LinkButton2" runat="server" Text="Cancel" CommandName="Cancel" /> 
                        </td> 
                    </tr> 
                </table> 
            </InsertItemTemplate> 
            <EditItemTemplate> 
                <table> 
                    <tr id="Tr1" runat="server"
                        <td> 
                            <asp:Panel ID="Panel1" runat="server"
                                <asp:TextBox ID="TextBox1" Text='<%# Bind("ID") %>' runat="server" /> 
                                <asp:TextBox ID="TextBox2" Text='<%# Bind("Name") %>' runat="server" /> 
                            </asp:Panel> 
                            <asp:LinkButton ID="LinkButton1" runat="server" Text="Update" CommandName="Update" /> 
                            <asp:LinkButton ID="LinkButton2" runat="server" Text="Cancel" CommandName="Cancel" /> 
                        </td> 
                    </tr> 
                </table> 
            </EditItemTemplate> 
        </asp:FormView> 


All the best,
Vlad
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center
0
Josh Winkler
Top achievements
Rank 1
answered on 05 May 2008, 02:23 PM
So what about doing the insert from the code behind using a layout like this? I keep getting an error on the insert. Here is the code:

<

radG:RadGrid runat="server" ID="rgCERReasons" AutoGenerateColumns="false">

<MasterTableView CommandItemDisplay="Top">

<CommandItemTemplate>

<asp:LinkButton runat="Server" ID="lb1" CommandName="InitInsert" Text="Add Reason" />

</CommandItemTemplate>

<Columns>

<radG:GridBoundColumn DataField="CERReasonID" ReadOnly="true" Visible="false" UniqueName="CERReasonID"/>

<radG:GridDropDownColumn HeaderText="Summary of Reasons" DataField="reasonID" ListTextField="Reason" ListValueField="ReasonID" UniqueName="ReasonList"/>

<radG:GridTemplateColumn HeaderText="Amount" UniqueName="ReasonAmount">

<EditItemTemplate>

<asp:TextBox id="ReasonAmount" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "ReasonAmount") %>'></asp:TextBox>

<asp:RequiredFieldValidator id="RequiredFieldValidator1" runat="server" ErrorMessage="*"

ControlToValidate= "ReasonAmount"></asp:RequiredFieldValidator>

</EditItemTemplate>

<ItemTemplate>

<asp:Label id="Label1" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "ReasonAmount") %>'></asp:Label>

</ItemTemplate>

</radG:GridTemplateColumn>

<%

--<radG:GridBoundColumn HeaderText="Amount" DataField="ReasonAmount" UniqueName="ReasonAmount" />--%>

<radG:GridDropDownColumn HeaderText="Asset Category" DataField="categoryID" ListTextField="AssetCategory" ListValueField="CategoryID" UniqueName="CategoryList" />

<radG:GridTemplateColumn HeaderText="Amount" UniqueName="CategoryAmount">

<EditItemTemplate>

<asp:TextBox id="CategoryAmount" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "CategoryAmount") %>'></asp:TextBox>

<asp:RequiredFieldValidator id="RequiredFieldValidator2" runat="server" ErrorMessage="*"

ControlToValidate= "CategoryAmount"></asp:RequiredFieldValidator>

</EditItemTemplate>

<ItemTemplate>

<asp:Label id="Label2" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "CategoryAmount") %>'></asp:Label>

</ItemTemplate>

</radG:GridTemplateColumn>

<%

--<radG:GridBoundColumn HeaderText="Amount" DataField="CategoryAmount" UniqueName="CategoryAmount" />--%>

<radG:GridEditCommandColumn ButtonType="linkButton" />

<radG:GridButtonColumn ButtonType="LinkButton" Text="Remove Reason" ConfirmText="Are You Sure You Want To Remove This Reason?" CommandName="Delete" />

</Columns>

<NoRecordsTemplate>There Are Currently No Records</NoRecordsTemplate>

</MasterTableView>

</radG:RadGrid>


CODE BEHIND:

void

rgCERReasons_InsertCommand(object source, GridCommandEventArgs e)

{

if (e.Item is GridEditableItem)

{

DropDownList Reason = (DropDownList)((GridEditableItem)e.Item)["ReasonList"].Controls[0];

DropDownList Category = (DropDownList)((GridEditableItem)e.Item)["CategoryList"].Controls[0];

TextBox ReasonAmount = (TextBox)((GridEditableItem)e.Item)["ReasonAmount"].Controls[0];

TextBox CategoryAmount = (TextBox)((GridEditableItem)e.Item)["CategoryAmount"].Controls[0];

}

}


And the error is...

Unable to cast object of type 'System.Web.UI.LiteralControl' to type 'System.Web.UI.WebControls.TextBox'.



Thanks much. Im a newbie to telerik controls.
0
Prangadj
Top achievements
Rank 1
answered on 05 May 2008, 03:12 PM
Sir,when you have controls in edit templates you can reference them directly using the FindControl(id) method where id is the actual server id of the editor, namely:

TextBox CategoryAmount = (TextBox)((GridEditableItem)e.Item).FindControl("CategoryAmount");

Mark this thread as an answer if this helps, pls.

Prangadj


0
Josh Winkler
Top achievements
Rank 1
answered on 05 May 2008, 03:26 PM
You are the man! Thanks so much. I dont have an option to mark this as answer since I didnt originally post the question or I would for you. Thanks again sir.
Tags
Grid
Asked by
Andrey
Top achievements
Rank 1
Answers by
Andrey
Top achievements
Rank 1
Vlad
Telerik team
Josh Winkler
Top achievements
Rank 1
Prangadj
Top achievements
Rank 1
Share this question
or