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

ObjectDataSource Automatic operations Insert Query string parm as default value

2 Answers 227 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Michael
Top achievements
Rank 1
Michael asked on 05 Mar 2014, 08:34 PM
All Im trying to do is pass a default value to the bound EDICustomerID feild so that when a new code is created via the radgrids 'add new record' it gets the value from the querystring Which holds the EDICustomerID that all codes belong to. 
What is the cleanest / best practice way to get the default value to be a value from the querystring?

I tried the following but while it didn't throw an error, it didn't work, either:
ASPX:
<asp:ObjectDataSource ID="ods_EDICustomerCodes" runat="server"
  SelectMethod="SelectEDICustomerCode"
  InsertMethod="InsertEDICustomerCode"
  UpdateMethod="UpdateEDICustomerCode"
  DeleteMethod="DeleteEDICustomerCode"
  TypeName="App.BLLEDIIntegration.DS.EDICustomerCodeDS"
  DataObjectTypeName="App.BLLEDIIntegration.Models.EDICustomerCodeObj">
   <SelectParameters>
     <asp:QueryStringParameter DefaultValue="0" Name="EDICustomerID"
       QueryStringField="EDICustomerID" Type="Int32" />
     <asp:Parameter DefaultValue="1" Name="active" Type="Int32" />
   </SelectParameters>
</asp:ObjectDataSource>
 
<telerik:RadGrid ID="RadGrid1" AllowAutomaticDeletes="True" AllowAutomaticInserts="True"
  AllowAutomaticUpdates="True" OnItemInserted="ItemInserted" OnItemUpdated="ItemUpdated"
  OnItemDeleted="ItemDeleted" OnPreRender="PreRender" OnBatchEditCommand="BatchEdit"
  DataSourceID="ods_EDICustomerCodes" runat="server" AutoGenerateColumns="False"
  Skin="Windows7" GridLines="Both" AutoGenerateDeleteColumn="True" >
 
  <MasterTableView CommandItemDisplay="TopAndBottom" EditMode="Batch" DataKeyNames="EDICustomerCodeID"
    OverrideDataSourceControlSorting="true" DataSourceID="ods_EDICustomerCodes">
    <BatchEditingSettings EditType="Row" />
 
     <Columns>
       <telerik:GridBoundColumn DataField="EDICustomerCodeID"
         HeaderText="EDICustomerCodeID"
         UniqueName="EDICustomerCodeID" DataType="System.Int32" Display="false"
         ReadOnly="true">
       </telerik:GridBoundColumn>
       <telerik:GridBoundColumn DataField="EDICustomerID" DataType="System.Int32"
         HeaderText="EDICustomerID"
         UniqueName="EDICustomerID" Display="False" DefaultInsertValue='<%= Request["EDICustomerID"] %>'>
       </telerik:GridBoundColumn>
       <telerik:GridBoundColumn DataField="EDICustomerCode" 
         HeaderText="EDICustomerCode"
         UniqueName="EDICustomerCode">
       </telerik:GridBoundColumn>
       <telerik:GridBoundColumn DataField="Priority" DataType="System.Int32"
         HeaderText="Priority"
         UniqueName="Priority">
       </telerik:GridBoundColumn>
       <telerik:GridBoundColumn DataField="Active" DataType="System.Int32"
         Display="False" HeaderText="Active"
         UniqueName="Active">
       </telerik:GridBoundColumn>
    </Columns>
  </MasterTableView>
</telerik:RadGrid>

objectDataSource(c#):
namespace App.BLLEDIIntegration.DS {
  public class EDICustomerCodeDS {
 
    // Select
    public List<EDICustomerCodeObj> SelectEDICustomerCode( int EDICustomerID, int active ) {
      List<EDICustomerCodeObj> customerCodes = BLLData.RetrieveList<EDICustomerCodeObj>(                                   true"EDIIntegration!GetEDICustomerCodes",
        new BLLParameterList( "@EDICustomerID", EDICustomerID, "@Active", active ) );
      return customerCodes;
    }
 
    // Insert
    public void InsertEDICustomerCode(EDICustomerCodeObj cc){
      BLLData.SaveData( true, "EDIIntegration!AddCodeToEDICustomer",
        new BLLParameterList( "@EDICustomerID", cc.EDICustomerID,
              
"@EDICustomerCode", cc.EDICustomerCode,
              "@Priority", cc.Priority,
              "@Active", 1
          ) );
    }
 
    // Update
    public void UpdateEDICustomerCode(EDICustomerCodeObj cc) {
      BLLData.SaveData(true,"EDIIntegration!UpdateEDICustomerCode",
        new BLLParameterList("@EDICustomerCodeID", cc.EDICustomerCodeID,
          "@EDICustomerID", cc.EDICustomerID,
          "@EDICustomerCode", cc.EDICustomerCode,
          "@Priority", cc.Priority,
          "@Active", cc.Active
        ) );
    }
 
    // Delete
    public void DeleteEDICustomerCode(EDICustomerCodeObj cc) {
      BLLData.SaveData( true, "EDIIntegration!DeactivateEDICustomerCode",
        new BLLParameterList( "@EDICustomerCodeID", cc.EDICustomerCodeID ) );
    }
  }
}

And finally the object its self:
namespace App.BLLEDIIntegration.Models {
  [Serializable]
  public partial class EDICustomerCodeObj {
    public int EDICustomerCodeID { get; set; }
    public int EDICustomerID { get; set; }
    public string EDICustomerCode { get; set; }
    public int Priority { get; set; }
    public int Active { get; set; }
  }
}

2 Answers, 1 is accepted

Sort by
0
Konstantin Dikov
Telerik team
answered on 10 Mar 2014, 01:54 PM
Hello Michael,

I have examined the provided code snippet and the requirement that you have and I could suggest two approaches for achieving the desired result.

The first approach would be to set QueryStringParameter in the InsertParameters of your ObjectDataSource control (in the same manner as you are setting it for the SelectParameters).

The second approach would be to handle the client-side OnBatchEditOpened event of the grid and retrieve the query string parameter on the client and assign it to the hidden EDICustomerID column:
RadGrid client-setting:
<ClientSettings>
    <ClientEvents OnBatchEditOpened="batchEditOpened"/>
</ClientSettings>

And the JavaScript:

<telerik:RadCodeBlock runat="server">
    <script type="text/javascript">
        function getParameterByName(name) {
            name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
            var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
                results = regex.exec(location.search);
            return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
        }
 
        function batchEditOpened(sender, args) {
            if (args.get_columnUniqueName() == "EDICustomerID") {
                setTimeout(function () {
                    sender.get_batchEditingManager().changeCellValue(args.get_cell(), getParameterByName("EDICustomerID"));
                })
            }
        }
    </script>
</telerik:RadCodeBlock>
 
Please give this a try and see if it meets your requirements.


Regards,
Konstantin Dikov
Telerik

DevCraft Q1'14 is here! Watch the online conference to see how this release solves your top-5 .NET challenges. Watch on demand now.

0
Michael
Top achievements
Rank 1
answered on 10 Mar 2014, 07:54 PM
Konstantin, 

Thank you for your reply.

While this does seem to work. We ended up modifying the data source so that when the data source creates a new object it passes in the query string defaults to the objects constructor.

like so: 
01.<asp:ObjectDataSource ID="ods_EDICustomerCodes" runat="server"
02.  SelectMethod="getEDICustomerCodeObjs"
03.  InsertMethod="InsertEDICustomerCode" OnObjectCreating="ds_ObjectCreaing"
04.  UpdateMethod="UpdateEDICustomerCode"
05.  DeleteMethod="DeleteEDICustomerCode"
06.  TypeName="App.BLLEDIIntegration.DS.EDICustomerCodeDS"
07.  DataObjectTypeName="App.BLLEDIIntegration.Models.EDICustomerCodeObj">
08.   <SelectParameters>
09.     <asp:QueryStringParameter DefaultValue="0" Name="EDICustomerID"
10.       QueryStringField="EDICustomerID" Type="Int32" />
11.     <asp:Parameter DefaultValue="1" Name="active" Type="Int32" />
12.   </SelectParameters>
13.</asp:ObjectDataSource>
14. 
15.<asp:label runat="server"  Text='EDICustomerID: <%= Request.QueryString %>'/>
16. 
17.<telerik:RadGrid ID="RadGrid1" AllowAutomaticDeletes="True" AllowAutomaticInserts="true"
18.  AllowAutomaticUpdates="True" OnItemInserted="ItemInserted" DataSourceID="ods_EDICustomerCodes"
19.  runat="server" AutoGenerateColumns="False" Skin="Windows7" GridLines="Both" AutoGenerateDeleteColumn="True" >
20. 
21.  <MasterTableView CommandItemDisplay="TopAndBottom" EditMode="Batch" DataKeyNames="EDICustomerCodeID"
22.    OverrideDataSourceControlSorting="true" DataSourceID="ods_EDICustomerCodes">
23.    <BatchEditingSettings EditType="Row" />
24. 
25.     <Columns>
26.       <telerik:GridBoundColumn DataField="EDICustomerCodeID"
27.         HeaderText="EDICustomerCodeID"
28.         UniqueName="EDICustomerCodeID" DataType="System.Int32" Display="false"
29.         ReadOnly="true">
30.       </telerik:GridBoundColumn>
31.       <telerik:GridBoundColumn DataField="EDICustomerID" DataType="System.Int32"
32.         HeaderText="EDICustomerID"
33.         UniqueName="EDICustomerID" Display="False" >
34.       </telerik:GridBoundColumn>
35.       <telerik:GridBoundColumn DataField="EDICustomerCode" 
36.         HeaderText="EDICustomerCode"
37.         UniqueName="EDICustomerCode">
38.       </telerik:GridBoundColumn>
39.       <telerik:GridBoundColumn DataField="Priority" DataType="System.Int32"
40.         HeaderText="Priority"
41.         UniqueName="Priority">
42.       </telerik:GridBoundColumn>
43.       <telerik:GridBoundColumn DataField="Active" DataType="System.Int32"
44.         Display="False" HeaderText="Active"
45.         UniqueName="Active">
46.       </telerik:GridBoundColumn>
47.    </Columns>
48.  </MasterTableView>
49.</telerik:RadGrid>

On line three give the data source a onObjectCreating handler, then call the Data Sources constructor passing the defaults from the code behind page, like so:

1.protected void ds_ObjectCreaing( object sender, ObjectDataSourceEventArgs e ) {
2.      int _EDICustomerID;
3.      EDICustomerCodeDS ret;
4.      int.TryParse( Request["EDICustomerID"], out _EDICustomerID );
5.      ret = new EDICustomerCodeDS( this.DevMode, this.CompanyID, this.UserID, _EDICustomerID);
6.      e.ObjectInstance = ret;
7.    }

And finally in the Data Source object have a constructor that takes the defaults. When the 'crud' method is called and you need the default then just use this.<paramName> and it inserts the the correct value for that particular value, like this:
01.public EDICustomerCodeDS() {
02.      throw new ArgumentException( "EDICustomerCodeDS Default constructor called! Constructor should always be called with arguments" );
03.    }
04. 
05.    public EDICustomerCodeDS( bool devMode, int companyID, int userID )
06.      : this( devMode, companyID, userID, 0 ) {
07.    }
08. 
09.    public EDICustomerCodeDS( bool devMode, int companyID, int userID, int EDICustomerID ) {
10.      this.DevMode = devMode;
11.      this.CompanyID = companyID;
12.      this.UserID = userID;
13.      this.EDICustomerID = EDICustomerID;
14.    }
15. 
16....
17. 
18.  // Insert
19.    public void InsertEDICustomerCode(EDICustomerCodeObj cc){
20.      BLLData.SaveData( true, "EDIIntegration!AddCodeToEDICustomer",
21.        new BLLParameterList( "@EDICustomerID", this.EDICustomerID, "@EDICustomerCode", cc.EDICustomerCode, "@Priority", cc.Priority, "@Active", 1 ) );
22.    }

This seems to work well for us keeping the data and interface fairly separate and we haven't found any issues with this yet so until we do we may stick with this approach. 

I really do appreciate the time and effort you put into this answer, and I will bring this up with our team and see if this may be a better solution. 

This is definitely just a wip solution and we may change this in the future.
Tags
Grid
Asked by
Michael
Top achievements
Rank 1
Answers by
Konstantin Dikov
Telerik team
Michael
Top achievements
Rank 1
Share this question
or