Using datakeyfield of first listbox in insert of second

4 posts, 0 answers
  1. Trevor
    Trevor avatar
    6 posts
    Member since:
    Dec 2011

    Posted 22 Dec 2011 Link to this post

    I am looking at the the reference online that has this example:
    <asp:SqlDataSource ID="SqlDataSource1" runat="server"
       ConnectionString="<$ ConnectionStrings>"
       SelectCommand="SELECT [ID] as [DataKeyID], [ID] as [InsertID], [Name], [Order] FROM [Cities] ORDER BY [Order]"
       DeleteCommand="DELETE FROM [Cities] WHERE [ID] = @ID">
    <DeleteParameters>
       <asp:Parameter Name="ID" Type="Int32" />
    <//DeleteParameters>
    </asp:SqlDataSource>
    <telerik:RadListBox ID="RadListBox1"
       runat="server"
       AllowAutomaticUpdates="true"   
       AllowTransfer="true"
       TransferToID="RadListBox2"
       AutoPostBackOnTransfer="true"
       DataKeyField="DataKeyID"             
       DataTextField="Name"  
       DataValueField="ID"                
       DataSourceID="SqlDataSource1" >
    </telerik:RadListBox>

    If you want to use the field, bound to the DataKeyField of the first RadListBox in the InsertCommands of the second ListBox, select it twice in the SelectCommand and give them different names. Then use the first one for the DataKeyField of the first RadListBox, and the second one for the InsertCommand of the second RadListBox.

    Here is an example:

    2. In the second RadListBox:

    • Set the AllowAutomaticUpdates="True" (so the DataSource is updated automatically)
    • Make sure that the DataSource has its InsertCommand set.

    Here is an example:

         
    
    <asp:SqlDataSource ID="SqlDataSource2" runat="server"
       ConnectionString="<$ ConnectionStrings>"
       SelectCommand="SELECT [ID], [Name], [Order] FROM [Cities] ORDER BY [Order]"
       InsertCommand="INSERT INTO [Cities] ([Name], [Order]) VALUES (@Name, @Order)">
    <InsertParameters>
       <asp:Parameter Name="Name" Type="String" />
       <asp:Parameter Name="Order" Type="Int32" />
    </InsertParameters>
    </asp:SqlDataSource>
    <telerik:RadListBox ID="RadListBox2"
       runat="server"
       AllowAutomaticUpdates="true"  
       DataKeyField="ID"            
       DataTextField="Name"  
       DataValueField="ID"                
       DataSourceID="SqlDataSource1" >
    </telerik:RadListBox>


    However I am failing to see where the [InsertID] column value is actually used in the second data source.  Could you clarify this?  This is exactly what I need to do for the project I am working on.

    Thanks
  2. Bozhidar
    Admin
    Bozhidar avatar
    1102 posts

    Posted 23 Dec 2011 Link to this post

    Hello,

    Thank you for pointing this out to us. We will fix this help article to prevent further confusion. 

    You can use the InsertID column in the second DataSource's Insert statement, just like the other fields. Like so:

    <asp:SqlDataSource ID="SqlDataSource2" runat="server"
       ConnectionString="<$ ConnectionStrings>"
       SelectCommand="SELECT [ID], [Name], [Order] FROM [Cities] ORDER BY [Order]"
       InsertCommand="INSERT INTO [Cities] ([InsertID], [Name], [Order]) VALUES (@InsertID, @Name, @Order)">
    <InsertParameters>
       <asp:Parameter Name="InsertID" Type="Int32" />
       <asp:Parameter Name="Name" Type="String" />
       <asp:Parameter Name="Order" Type="Int32" />
    </InsertParameters>
    </asp:SqlDataSource>
    Kind regards,
    Bozhidar
    the Telerik team
    If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Trevor
    Trevor avatar
    6 posts
    Member since:
    Dec 2011

    Posted 23 Dec 2011 Link to this post

    Thank you, it now works.  I have one more question.  I have one list box that is going to be pulling from a table that will always be the same however whenever I set the ListBox's TransferMode to "Move" it deletes the record that it just transferred.  I read somewhere on the forum that if you set "Allow Delete" to false then it would not delete that record however it is still deleting it.  The left box will have a list of employees that can be assigned to a job.  The right box will be the employees that are assigned to a certain job.  I don't want to delete the employee from the database, I just don't want it in the left box if it is in the right.  Is this possible with transfer?
  5. Bozhidar
    Admin
    Bozhidar avatar
    1102 posts

    Posted 26 Dec 2011 Link to this post

    Hello,

    You can set the transfer mode to Copy, and then manually remove the item from the listbox on the Tranferred server event. Here's how the handler should look like:

    protected void RadListBox1_Transferred(object sender, Telerik.Web.UI.RadListBoxTransferredEventArgs e)
    {
        foreach (RadListBoxItem item in e.Items)
        {
            RadListBox1.Items.Remove(item);
        }
    }
    Kind regards,
    Bozhidar
    the Telerik team
    If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now
Back to Top