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

[Solved] linq - user control examples

5 Answers 262 Views
Grid
This is a migrated thread and some comments may be shown as answers.
kyle
Top achievements
Rank 1
kyle asked on 03 Mar 2008, 05:47 PM
Are there any examples around using linq instead of a data table/row when using a custom user control for the form edits?

5 Answers, 1 is accepted

Sort by
0
Vlad
Telerik team
answered on 04 Mar 2008, 12:56 PM
Hello kyle,

Here is an example:
<%@ Page Language="C#" %> 
 
<%@ Register TagPrefix="telerik" Namespace="Telerik.Web.UI" Assembly="Telerik.Web.UI" %> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
 
<script runat="server"
 
    protected void RadGrid1_UpdateCommand(object source, GridCommandEventArgs e) 
    { 
        UserControl userControl = (UserControl)e.Item.FindControl(GridEditFormItem.EditFormUserControlID); 
 
        ListDictionary keyValues = new ListDictionary(); 
        ListDictionary newnewValues = new ListDictionary(); 
        ListDictionary oldValues = new ListDictionary(); 
 
        keyValues.Add("ProductID", ((GridEditFormItem)e.Item).ParentItem.GetDataKeyValue("ProductID")); 
 
        oldValues.Add("ProductName", ((GridEditFormItem)e.Item).ParentItem["ProductName"].Text); 
 
        newValues.Add("ProductName", ((TextBox)userControl.FindControl("TextBox1")).Text); 
 
        LinqDataSource1.Update(keyValues, newValues, oldValues); 
    } 
</script> 
 
<html xmlns="http://www.w3.org/1999/xhtml"
<head runat="server"
    <title>Untitled Page</title> 
</head> 
<body> 
    <form id="form1" runat="server"
    <div> 
        <asp:ScriptManager ID="ScriptManager1" runat="server" /> 
        <telerik:RadGrid ID="RadGrid1" DataSourceID="LinqDataSource1" AutoGenerateEditColumn="true" 
            runat="server" OnUpdateCommand="RadGrid1_UpdateCommand"
            <MasterTableView DataKeyNames="ProductID" AllowAutomaticUpdates="true"
                <EditFormSettings EditFormType="WebUserControl" UserControlName="WebUserControl.ascx"
                </EditFormSettings> 
            </MasterTableView> 
        </telerik:RadGrid> 
    </div> 
    <asp:LinqDataSource ID="LinqDataSource1" runat="server" ContextTypeName="DataClassesDataContext" 
        EnableUpdate="True" TableName="Products"
    </asp:LinqDataSource> 
    </form> 
</body> 
</html> 
 

and the UserControl code:
<%@ Control Language="C#" ClassName="WebUserControl" %> 
ProductName: <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("ProductName") %>'></asp:TextBox> 
<asp:LinkButton ID="LinkButton1" CommandName="Update" runat="server">Update</asp:LinkButton> 
<asp:LinkButton ID="LinkButton2" CommandName="Cancel" runat="server">Cancel</asp:LinkButton> 


Kind regards,
Vlad
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center
0
kyle
Top achievements
Rank 1
answered on 04 Mar 2008, 02:15 PM
Thanks - I am surprised we are not able to use or cast our of the grid  one of the link objects for that row and just use obj.prodname = newvalue etc...

so do we have to always give the oldvalues?


One last question,  what method is used to populate the userControl when using the linq data source?
0
kyle
Top achievements
Rank 1
answered on 05 Mar 2008, 02:55 AM
one other note here....  The example above produced this error:

Cannot add an entity with a key that is already in use.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Data.Linq.DuplicateKeyException: Cannot add an entity with a key that is already in use.
0
kyle
Top achievements
Rank 1
answered on 06 Mar 2008, 01:42 AM
Can you tell me why this would be happening with your example?
0
Vlad
Telerik team
answered on 07 Mar 2008, 03:51 PM
Hi kyle,

Please check this example for more info:
http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.linqdatasource.update.aspx

Here is also a bit more info about this exception:
http://www.west-wind.com/WebLog/posts/134095.aspx

Kind regards,
Vlad
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center
Tags
Grid
Asked by
kyle
Top achievements
Rank 1
Answers by
Vlad
Telerik team
kyle
Top achievements
Rank 1
Share this question
or