Insert with ObjectDataSource and FormTemplate

9 posts, 0 answers
  1. Andrey
    Andrey avatar
    30 posts
    Member since:
    Apr 2008

    Posted 15 Apr 2008 Link to this post

    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.
  2. Andrey
    Andrey avatar
    30 posts
    Member since:
    Apr 2008

    Posted 15 Apr 2008 Link to this post

    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>

     

  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Andrey
    Andrey avatar
    30 posts
    Member since:
    Apr 2008

    Posted 15 Apr 2008 Link to this post

    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
  5. Vlad
    Admin
    Vlad avatar
    11100 posts

    Posted 18 Apr 2008 Link to this post

    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
  6. Andrey
    Andrey avatar
    30 posts
    Member since:
    Apr 2008

    Posted 20 Apr 2008 Link to this post

    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.



  7. Vlad
    Admin
    Vlad avatar
    11100 posts

    Posted 21 Apr 2008 Link to this post

    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
  8. Josh Winkler
    Josh Winkler avatar
    31 posts
    Member since:
    Nov 2007

    Posted 05 May 2008 Link to this post

    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.
  9. Prangadj
    Prangadj avatar
    174 posts
    Member since:
    Jan 2008

    Posted 05 May 2008 Link to this post

    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


  10. Josh Winkler
    Josh Winkler avatar
    31 posts
    Member since:
    Nov 2007

    Posted 05 May 2008 Link to this post

    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.
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017