|
code
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
DataSourceControl and declarative SelectParametersI am trying to implement a DataSourceControl and I need to simulate the same functionality as the SqlDataSourceControl : being able to set declaratively the select parameters to use when binding a DetailView to the Selected Row in a GridView, but I have difficulties with the sample in MSDN concerning DataSourceControl, Visual Studio give me an error on the <Parameter><asp:ControlParameter...../> lines: My control doesn't accept child controls ??? What am I missing to allow this declarative style ? Should I simply name my properties SelectParameters, InsertParameters, DeleteParemeters, etc. to allow DetailView to send these parameters in the method protected override IEnumerable ExecuteSelect(DataSourceSelectArguments selectArgs) Thanks for help CS PS: I originaly posted on asp.net but this forum seems to better suite my request Hi CS,
Thank you for posting. From your description, I understand you're developing a custom DataSourceControl. And currently you're encountering some problem on making the custom datasource control utilize custom properties that act as parameters in aspx template, correct? Based on my experience, for such parameters in DataSource control, they should be declared as a public property on the DataSource control, also we need to apply some design-time attributes (like the PersistenceModeAttribute....) .e.g: ====================== [Editor("System.Web.UI.Design.WebControls.ParameterCollectionEditor, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof(UITypeEditor)), MergableProperty(false), PersistenceMode(PersistenceMode.InnerProperty), DefaultValue((string)null)] public ParameterCollection SelectParameters { get { if (_selectparameters == null) { _selectparameters = new ParameterCollection(); } return _selectparameters; } } ============================= # the above code snippet is extracted from the built-in sqlDatasource control in ASP.NET 2.0. Hope this helps. Regards, Steven Cheng Microsoft Online Community Support ================================================== When responding to posts, please "Reply to Group" via your newsreader so that others may learn and benefit from your issue. ================================================== This posting is provided "AS IS" with no warranties, and confers no rights. Get Secure! www.microsoft.com/security (This posting is provided "AS IS", with no warranties, and confers no rights.) I have found similar code on Nikhil K. blog, but I am unable to restitute
the full behavior: GridView doesn't call my DataSource control with filled SelectArguments. Seems that something is missing for GridView to act with my control the same way it does with SqlDataSource control. CS. "Steven Cheng[MSFT]" <stch***@online.microsoft.com> a écrit dans le message de news: Mxj6TU1ZGHA.***@TK2MSFTNGXA01.phx.gbl...Show quoteHide quote > Hi CS, > > Thank you for posting. > > From your description, I understand you're developing a custom > DataSourceControl. And currently you're encountering some problem on > making > the custom datasource control utilize custom properties that act as > parameters in aspx template, correct? > > Based on my experience, for such parameters in DataSource control, they > should be declared as a public property on the DataSource control, also we > need to apply some design-time attributes (like the > PersistenceModeAttribute....) .e.g: > > ====================== > > > [Editor("System.Web.UI.Design.WebControls.ParameterCollectionEditor, > System.Design, Version=2.0.0.0, Culture=neutral, > PublicKeyToken=b03f5f7f11d50a3a", > typeof(UITypeEditor)), > MergableProperty(false), > PersistenceMode(PersistenceMode.InnerProperty), > DefaultValue((string)null)] > public ParameterCollection SelectParameters > { > get > { > if (_selectparameters == null) > { > _selectparameters = new ParameterCollection(); > } > return _selectparameters; > } > } > ============================= > > # the above code snippet is extracted from the built-in sqlDatasource > control in ASP.NET 2.0. > > Hope this helps. > > Regards, > > Steven Cheng > Microsoft Online Community Support > > > ================================================== > > When responding to posts, please "Reply to Group" via your newsreader so > that others may learn and benefit from your issue. > > ================================================== > > > This posting is provided "AS IS" with no warranties, and confers no > rights. > > > > Get Secure! www.microsoft.com/security > (This posting is provided "AS IS", with no warranties, and confers no > rights.) > Thanks for your response,
Yes, actually the most important is how does us implement our custom datasource control's DataSourceView, the concrete work are done in DataSourceView derived class, its select,update... methods do the underlying work and will requie those paramters. Currently how are you implementing the custom class? Regards, Steven Cheng Microsoft Online Community Support ================================================== When responding to posts, please "Reply to Group" via your newsreader so that others may learn and benefit from your issue. ================================================== This posting is provided "AS IS" with no warranties, and confers no rights. Get Secure! www.microsoft.com/security (This posting is provided "AS IS", with no warranties, and confers no rights.) Here is my code, if you can fugure out what is missing to have GridView send
correct parameters ? namespace WTCRM.Settings { /// <summary> /// AddressDataSource is used to manage address datas from a cached dataset /// </summary> [AspNetHostingPermission(SecurityAction.Demand, Level = AspNetHostingPermissionLevel.Minimal), DefaultProperty("SelectParameters"), Designer(typeof(WTCRM.Design.AddressDSDesigner), typeof(IDesigner)), ParseChildren(true), PersistChildren(false) ] public class AddressDataSource : DataSourceControl { private AddressManagerDB AdMan = new AddressManagerDB(); private Dictionary<string, AddressDataSourceView> dicView = new Dictionary<string, AddressDataSourceView>(); private DataSet _Ds = null; private ParameterCollection _SelectParameters = null; [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden) ] public DataSet dsAddress { get { if (_Ds != null) return _Ds; try { _Ds = AdMan.GetDsAllAddresses(); } catch (Exception ex) { System.Diagnostics.Trace.WriteLineIf(PageTraceSwitch.Sw.TraceError, string.Format("AddressManagerEdit dsAddress : ex : {0}", ex)); throw; } return _Ds; } } [ DefaultValue(null), Editor(typeof(ParameterCollectionEditor), typeof(UITypeEditor)), MergableProperty(false), PersistenceMode(PersistenceMode.InnerProperty), Category("Data") ] public ParameterCollection SelectParameters { get { if (_SelectParameters == null) { _SelectParameters = new ParameterCollection(); _SelectParameters.ParametersChanged += new EventHandler(this.OnSelectParametersChanged); if (IsTrackingViewState) { ((IStateManager)_SelectParameters).TrackViewState(); } } return _SelectParameters; } } protected override void LoadViewState(object state) { object baseState = null; if (state != null) { Pair p = (Pair)state; baseState = p.First; if (p.Second != null) { ((IStateManager)SelectParameters).LoadViewState(p.Second); } } base.LoadViewState(baseState); } protected override void OnInit(EventArgs e) { Page.LoadComplete += new EventHandler(this.OnPageLoadComplete); } private void OnPageLoadComplete(object sender, EventArgs e) { if (_SelectParameters != null) { _SelectParameters.UpdateValues(Context, this); } } private void OnSelectParametersChanged(object sender, EventArgs e) { foreach( KeyValuePair<string, AddressDataSourceView> kvp in dicView) kvp.Value.RaiseChangedEvent(); } protected override object SaveViewState() { object baseState = base.SaveViewState(); object SelectParameterstate = null; if (_SelectParameters != null) { SelectParameterstate = ((IStateManager)_SelectParameters).SaveViewState(); } if ((baseState != null) || (SelectParameterstate != null)) { return new Pair(baseState, SelectParameterstate); } return null; } protected override void TrackViewState() { base.TrackViewState(); if (_SelectParameters != null) { ((IStateManager)_SelectParameters).TrackViewState(); } } public AddressDataSource() : base() {} public bool HasView(string viewName) { return dicView.ContainsKey(viewName); } // Return a strongly typed view for the current data source control. protected override DataSourceView GetView(string viewName) { AddressDataSourceView view; if (!dicView.ContainsKey(viewName)) { view = new AddressDataSourceView(this, viewName); dicView.Add(viewName, view); } else view = dicView[viewName]; return view; } // calls the DataSourceControl.GetViewNames method. public string[] GetArViewNames() { DataSet ds = dsAddress; string[] list = new string[ds.Tables.Count]; int i=0; foreach( DataTable Tb in ds.Tables) list[i++] = Tb.TableName; return list; } // calls the DataSourceControl.GetViewNames method. protected override ICollection GetViewNames() { DataSet ds = dsAddress; return ds.Tables as ICollection; } #region insert operations /// <summary> /// Insert a new city, rebind the page and select the inserted row in display /// </summary> /// <param name="name"></param> /// <returns></returns> public int InsertCity(IDictionary values) { string name = (string)values["wt_CityName"]; int ret = AdMan.InsertCity(name); GridView ctl = (GridView)Page.FindControl("GVCities"); Page.DataBind(); if (ctl != null) { string strval = ret.ToString(); for (int i = 0; i < ctl.Rows.Count; i++) { GridViewRow gr = ctl.Rows[i]; if (gr.Cells.Count > 1 && gr.Cells[1].Text == strval) { ctl.SelectedIndex = i; break; } } } return ret; } /// <summary> /// Insert a street name for selected city /// get cityId form the listbox becaused user can select another city than the display selected one /// rebind the page /// </summary> /// <param name="cityId"></param> /// <param name="name"></param> /// <returns></returns> public int InsertStreet(IDictionary values) { DetailsView ctl = (DetailsView)Page.FindControl("DVStreet"); DropDownList ddl = (DropDownList)ctl.FindControl("DDLCity1"); int ret = 0; if (ddl == null) return ret; int cityId = int.Parse(ddl.SelectedValue); string name = (string)values["wt_StreetName"]; ret = AdMan.InsertStreet(cityId, name); ctl.Page.DataBind(); GridView ctl2 = (GridView)Page.FindControl("GVStreets"); if (ctl2 != null) { string strval = ret.ToString(); for (int i = 0; i < ctl2.Rows.Count; i++) { GridViewRow gr = ctl2.Rows[i]; if (gr.Cells.Count > 1 && gr.Cells[1].Text == strval) { ctl2.SelectedIndex = i; break; } } } return ret; } public int InsertStreetPart(IDictionary values) { DropDownList ddlS = (DropDownList)Page.FindControl("LBStreets"); DetailsView ctl = (DetailsView)Page.FindControl("DVStreetPart"); int ret = 0; if (ctl == null || ddlS == null) return ret; int streetId = int.Parse(ddlS.SelectedValue); DropDownList ddl = (DropDownList)ctl.FindControl("DDLSide"); int sideId = int.Parse(ddl.SelectedValue); ddl = (DropDownList)ctl.FindControl("DDLSuffix1"); int numInf = int.Parse((string)values["wt_StreetPartNumInf"]); int suffixInf = int.Parse(ddl.SelectedValue); int numSup = int.Parse((string)values["wt_StreetPartNumSup"]); ddl = (DropDownList)ctl.FindControl("DDLSuffix2"); int suffixSup = int.Parse(ddl.SelectedValue); ddl = (DropDownList)ctl.FindControl("DDLMicroSector"); int microSectorId = int.Parse(ddl.SelectedValue); string name = (string)values["wt_StreetPartName"]; ret = AdMan.InsertStreetPart(streetId, sideId, numInf, suffixInf, numSup, suffixSup, microSectorId, name); // to avoid loosing city selection during rebind DropDownList ddlC = (DropDownList)Page.FindControl("LBStreetCities"); Page.DataBind(); GridView ctl2 = (GridView)Page.FindControl("GVStreetParts"); if (ctl2 != null) { string strval = ret.ToString(); for (int i = 0; i < ctl2.Rows.Count; i++) { GridViewRow gr = ctl2.Rows[i]; if (gr.Cells.Count > 1 && gr.Cells[1].Text == strval) { ctl2.SelectedIndex = i; break; } } } return ret; } public int InsertMicroSector(IDictionary values) { DetailsView ctl = (DetailsView)Page.FindControl("DVMicroSecteur"); int ret = 0; if (ctl == null) return ret; DropDownList ddl = (DropDownList)ctl.FindControl("DDLSector2"); int sectorId = int.Parse(ddl.SelectedValue); string name = (string)values["wt_MicroSectorName"]; ret = AdMan.InsertMicroSector(sectorId, name); this.Page.DataBind(); GridView ctl2 = (GridView)Page.FindControl("GVMicroSector"); if (ctl != null) { string strval = ret.ToString(); for (int i = 0; i < ctl2.Rows.Count; i++) { GridViewRow gr = ctl2.Rows[i]; if (gr.Cells.Count > 1 && gr.Cells[1].Text == strval) { ctl2.SelectedIndex = i; break; } } } return ret; } #endregion insert #region update operations public void UpdateCity(IDictionary keys, IDictionary values, IDictionary oldValues) { if (values != null && values.Count > 0) { string nm = (string)values["wt_CityName"]; GridView gv = (GridView)Page.FindControl("GVCities"); if (gv.EditIndex != -1) { int Id = int.Parse(gv.Rows[gv.EditIndex].Cells[1].Text); AdMan.UpdateCity(Id, nm); } } } public void UpdateStreet(IDictionary keys, IDictionary values, IDictionary oldValues) { if (values != null && values.Count > 0) { string nm = (string)values["wt_StreetName"]; GridView gv = (GridView)Page.FindControl("GVStreets"); if (gv.EditIndex != -1) { int Id = int.Parse(gv.Rows[gv.EditIndex].Cells[1].Text); AdMan.UpdateStreet(Id, nm); } } } public void UpdateStreetPart(IDictionary keys, IDictionary values, IDictionary oldValues) { if (values != null && values.Count > 0) { int streetId = (int)values["wt_StreetId"]; int sideId = (int)values["wt_SideId"]; int numInf = (int)values["wt_StreetPartNumInf"]; int suffixInf = (int)values["wt_SuffixInf"]; int numSup = (int)values["wt_StreetPartNumSup"]; int suffixSup = (int)values["wt_SuffixSup"]; int microSectorId = (int)values["wt_MicroSectorId"]; string nm = (string)values["wt_StreetName"]; GridView gv = (GridView)Page.FindControl("GVStreetParts"); if (gv.EditIndex != -1) { int Id = int.Parse(gv.Rows[gv.EditIndex].Cells[1].Text); AdMan.UpdateStreetPart(Id, streetId, sideId, numInf, suffixInf, numSup, suffixSup, microSectorId, nm); } } } public void UpdateSector(IDictionary keys, IDictionary values, IDictionary oldValues) { if (values != null && values.Count > 0) { string nm = (string)values["wt_SectorName"]; GridView gv = (GridView)Page.FindControl("DLSectors"); if (gv.EditIndex != -1) { int Id = int.Parse(gv.Rows[gv.EditIndex].Cells[1].Text); AdMan.UpdateSector(Id, nm); } } } public void UpdateMicroSector(IDictionary keys, IDictionary values, IDictionary oldValues) { if (values != null && values.Count > 0) { string nm = (string)values["wt_MicroSectorName"]; GridView gv = (GridView)Page.FindControl("GVMicroSector"); if (gv.EditIndex != -1) { int Id = int.Parse(gv.Rows[gv.EditIndex].Cells[1].Text); AdMan.UpdateMicroSector(Id, nm); } } } #endregion update #region delete operations public void DeleteCity(IDictionary keys, IDictionary values) { if ( keys != null && keys.Count > 0) { int Id = (int)keys["wt_CityId"]; AdMan.DeleteCity(Id); } } public void DeleteStreet(IDictionary keys, IDictionary values) { if (keys != null && keys.Count > 0) { int Id = (int)keys["wt_StreetId"]; AdMan.DeleteStreet(Id); } } public void DeleteStreetPart(IDictionary keys, IDictionary values) { if (keys != null && keys.Count > 0) { int Id = (int)keys["wt_StreetPartId"]; AdMan.DeleteStreetPart(Id); } } public void DeleteSector(IDictionary keys, IDictionary values) { if (keys != null && keys.Count > 0) { int Id = (int)keys["wt_SectorId"]; AdMan.DeleteSector(Id); } } public void DeleteMicroSector(IDictionary keys, IDictionary values) { if (keys != null && keys.Count > 0) { int Id = (int)keys["wt_MicroSectorId"]; AdMan.DeleteMicroSector(Id); } } #endregion delete } // The AddressDataSourceView class encapsulates the // capabilities of the CsvDataSource data source control. public class AddressDataSourceView : DataSourceView { const string cityTbName = "wt_city"; const string streetTbName = "wt_street"; const string streetPartTbName = "wt_streetpart"; const string sectorTbName = "wt_sector"; const string microSectorTbName = "wt_microsector"; AddressDataSource AddressDs = null; public AddressDataSourceView(IDataSource owner, string name) : base(owner, name) {AddressDs = (AddressDataSource)owner; } internal void RaiseChangedEvent() { OnDataSourceViewChanged(EventArgs.Empty); } // Get data from the underlying data source. // Build and return a DataView, regardless of mode. protected override IEnumerable ExecuteSelect(DataSourceSelectArguments selectArgs) { DataSet ds = AddressDs.dsAddress; DataTable data = ds.Tables[Name]; DataView dataView = new DataView(data); if (AddressDs.SelectParameters.Count > 0) { StringBuilder sb = new StringBuilder(); bool first = true; bool existFilter = false; foreach (Parameter p in AddressDs.SelectParameters) { if (!(p is ControlParameter)) continue; ControlParameter p1 = (ControlParameter)p; Control ctl = AddressDs.Page.FindControl(p1.ControlID); if (ctl == null) continue; existFilter = true; if (p1.PropertyName == "SelectedValue") { string val = string.Empty; if (ctl is DropDownList && ((DropDownList)ctl).SelectedValue != null) val = ((DropDownList)ctl).SelectedValue; else if (ctl is GridView ) { GridView gv = (GridView)ctl; if (gv.SelectedValue != null) { if (gv.SelectedValue is IDictionary) val = ((IDictionary)gv.SelectedValue)[p1.ControlID].ToString(); else val = gv.SelectedValue.ToString(); } } if (first) { if (string.IsNullOrEmpty(val)) sb.AppendFormat("{0} is null", p1.Name); else sb.AppendFormat("{0} = '{1}'", p1.Name, val); first = false; } else { if (string.IsNullOrEmpty(val)) sb.AppendFormat(" and {0} is null", p1.Name); else sb.AppendFormat(" and {0} = '{1}'", p1.Name, val); } } } if (existFilter) { try { dataView.RowFilter = sb.ToString(); } catch (Exception ex) { System.Diagnostics.Trace.WriteLineIf(DBTraceSwitch.Sw.TraceError, string.Format("AddressDataSourceView ExecuteSelect ViewName: {0}, ex : {1}", Name, ex)); } } } if (selectArgs.SortExpression != String.Empty) { dataView.Sort = selectArgs.SortExpression; } return dataView; } // The AddressDataSourceView does ue // permit deletion. You can modify or extend // this sample to do so. public override bool CanDelete { get { return true; } } /// <summary> /// Called when a delete button is clicked in a GridView /// Find the corresoponding Table from the viewName and call correspkinding delete method on the Datasource control attached /// </summary> /// <param name="keys"></param> /// <param name="values"></param> /// <returns></returns> protected override int ExecuteDelete(IDictionary keys,IDictionary values) { switch (Name.ToLower()) { case cityTbName: AddressDs.DeleteCity(keys,values); break; case streetTbName: AddressDs.DeleteStreet(keys, values); break; default: return 0; case streetPartTbName: AddressDs.DeleteStreetPart(keys, values); break; break; case sectorTbName: AddressDs.DeleteSector(keys, values); break; case microSectorTbName: AddressDs.DeleteMicroSector(keys, values); break; } return 1; } // The AddressDataSourceView does not currently // permit insertion of a new record. You can // modify or extend this sample to do so. public override bool CanInsert { get { return true; } } protected override int ExecuteInsert(IDictionary values) { switch (Name.ToLower()) { case cityTbName: AddressDs.InsertCity(values); break; case streetTbName: AddressDs.InsertStreet(values); break; default: return 0; case streetPartTbName: AddressDs.InsertStreetPart(values); break; case microSectorTbName: AddressDs.InsertMicroSector(values); break; } return 1; } // The AddressDataSourceView does not currently // permit update operations. You can modify or // extend this sample to do so. public override bool CanUpdate { get { return true; } } protected override int ExecuteUpdate(IDictionary keys, IDictionary values, IDictionary oldValues) { switch (Name.ToLower()) { case cityTbName: AddressDs.UpdateCity(keys,values,oldValues); break; case streetTbName: AddressDs.UpdateStreet(keys, values, oldValues); break; default: return 0; case streetPartTbName: AddressDs.UpdateStreetPart(keys, values, oldValues); break; case sectorTbName: AddressDs.UpdateSector(keys, values, oldValues); break; case microSectorTbName: AddressDs.UpdateMicroSector(keys, values, oldValues); break; } return 1; } } } Thanks for your response and the detailed code snippet you provided.
From the code, in your custom DataSourceView control, you are looping through the SelectParameter collections and union them as a filter string for your internal dataset , yes? If so, in that ExecuteSelect method, have you tried use some trace statement or just debug/step into it to see whether the collection has contained those supplied parameter entries (defined in page) ? Regards, Steven Cheng Microsoft Online Community Support ================================================== When responding to posts, please "Reply to Group" via your newsreader so that others may learn and benefit from your issue. ================================================== This posting is provided "AS IS" with no warranties, and confers no rights. Get Secure! www.microsoft.com/security (This posting is provided "AS IS", with no warranties, and confers no rights.)
Javascript with Webcontrols
MyGridView cause "Columes" lose the intellisense in VS2005 IDE! CompositeControl and Toolbox A socket operation was attempted to an unreachable network Print version use CSS or similar ? Sorting Datagrid Can i Make a Month Column Display May instead of 5 2.0 TreeNode How to get dynamic control's value after postback? Edit, Update, Cancel |
|||||||||||||||||||||||