Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Desperately trying to implement Web User Controls
Message
From
19/10/2004 20:12:02
 
 
To
14/10/2004 17:16:50
Natalie Sebastian
Great Lakes Behavioral Research Institut
Cranberry Township, Pennsylvania, United States
General information
Forum:
ASP.NET
Category:
The Mere Mortals .NET Framework
Miscellaneous
Thread ID:
00951564
Message ID:
00952854
Views:
8
Hello,

Been there done that!

Note: Keep in mind that the BusinessObjects are registered on the form, data is retreived and populated into the respective Session[""].

Note: Make sure you assign the commands to the grids events.

I created a custom user control so that I could add my own user controls to the form myself. This also gives me flexibility when programming around the UserControls. In this case I even derived the mmBusinessWebPage

---------------- The UserControl definition : Begin ------------------------
using System;
using System.Web;
using System.Web.UI;

// Containers are mmBusinessWebPage and UserControls
using Mine.Main.Web.UI.Containers;

namespace Mine.Main.Web.UI.Containers
{
	/// <summary>
	/// Summary description for dcgUserControl.
	/// </summary>
	public class MineUserControl : UserControl
	{
		/// <summary>
		/// A link to the mmBusinessWebPage
		/// </summary>
		protected MineBusinessWebPage _BusinessWebPage = null;
		protected int _TabID;

		public MineUserControl()
		{
			//
			// TODO: Add constructor logic here
			//
		}

		public MineBusinessWebPage BusinessWebPage
		{
			get
			{
				return this._BusinessWebPage;
			}

			set
			{
				this._BusinessWebPage = value;
			}
		}

		public void SetBusinessWebPage()
		{
			if (this.BusinessWebPage == null)
			{
				this.BusinessWebPage = (MineBusinessWebPage) this.Page;
			}
		}
	}
}
---------------- The UserControl definition : End ------------------------


---------------- Using the custom MineUserControl : Begin ------------------------

This will give you access to the functionality of the BusinessWebPage, but probably will not give you access to the exact properties of the page itself. I found I had to be creative when I do this stuff.


In the Page_Load of the UserControl (with the datagrid)
private void Page_Load(object sender, System.EventArgs e)
{
	this.SetBusinessWebPage();
			
	if (!Page.IsPostBack)
	{
	}
}
</pre


A business object can be addressed like this, but I think Kevin could think of a better way to handle this.  Everytime you use this.oMyBusinessObject it will refresh the connection/properties.

<pre>
public virtual MyBusinessObject oMyBusinessObject
{
	get
	{
		// I was having problems assinging the value to the object
		// Here we cheat to ensure the system gets what it needs
		DataSet dsMyBusinessObject = (DataSet)Session["dsMyBusinessObject"];

		this._MyBusinessObject = (MyBusinessObject) this.BusinessWebPage.GetBizObj("MyBusinessObject");
		this._MyBusinessObject.SetCurrentDataSet(dsMyBusinessObject, "MyBusinessObject");

		return this._MyBusinessObject;
	}

	set
	{
		this._MyBusinessObject = value;
	}
}
private MyBusinessObject _MyBusinessObject = null;
An example of the delete command.
private void grdMyGrid_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
	// Notice I use the e.Item.DataSetIndex instead of the e.Item.ItemIndex
	int RowToDelete = e.Item.DataSetIndex;
	DataSet dsMyBusinessObject= (DataSet)Session["dsMyBusinessObject"];
	this.oMyBusinessObject.Delete(dsMyBusinessObject, RowToDelete);

	if (e.Item.ItemIndex < 1 && this.grdMyGrid.CurrentPageIndex > 0)
		this.grdMyGrid.CurrentPageIndex = this.grdMyGrid.CurrentPageIndex - 1;

	this.BusinessWebPage.BindControl(this.grdMyGrid);
}
An example of the edit command.
private void grdMyGrid_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
	// hide the Delete column
	this.grdMyGrid.Columns[5].Visible = false;

	// Set the current edit item to the selected item and rebind the DataGrid
	// e.Item.ItemIndex points to the specific row on any page of the grid
	// e.g. e.Item.ItemIndex = 4 points to the 5th row on ALL pages of the grid
	this.grdMyGrid.EditItemIndex = e.Item.ItemIndex; 
	this.BusinessWebPage.BindControl(this.grdMyGrid);
}
Do you use Paging???
private void grdMyGrid_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
	this.grdMyGrid.CurrentPageIndex = e.NewPageIndex;
	this.BusinessWebPage.BindControl(this.grdMyGrid);
}
An example of the update command
private void grdMyGrid_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
	// Get the item to be updated
	this.grdMyGrid.EditItemIndex = e.Item.ItemIndex;
	// Retrieve the previous Categories DataSet
	DataSet dsMyBusinessObject= (DataSet)Session["dsMyBusinessObject"];

	// ERROR FLAG!!!
	// The data is not saved correctly here if paging is used.  An email has been sent to Kevin with the specific code
	// that is causing the problem.  I am sure a fix is quick.
	// I will be making a post here with the specifics of the error.

	// if you use e.Item.ItemIndex - you will save over the values on the first page
	// if you use e.Item.DataSetIndex - you will crash out
	if (this.oMyBusinessObject.SaveDataSet(this.oMyBusinessObject.TableName, e.Item.ItemIndex) == mmSaveDataResult.RulesPassed)
	{
		// make the Delete column visible again
		this.grdMyGrid.Columns[5].Visible = true;
		this.grdMyGrid.EditItemIndex = -1;
	}
	else
	{
		this.grdMyGrid.EditItemIndex = e.Item.ItemIndex;
	}
	this.BusinessWebPage.BindControl(this.grdMyGrid);
}
An example of the Cancel command
private void grdMyGrid_CancelCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
	// Make the Delete column visible
	this.grdMyGrid.Columns[5].Visible = true;

	// Reset the edit index and rebind the DataGrid
	this.grdMyGrid.EditItemIndex = -1;
	this.BusinessWebPage.BindControl(this.grdMyGrid);
}
---------------- Using the custom MineUserControl : End ------------------------


Enjoy!!! This is basically cut and pasted out of my UserControl. I edited the naming only.




>Hello,
>
>I've have been trying to create Web User Controls using MM. I found some code from a previous post to register the business object by casting:
>
> private void Page_Load(object sender, System.EventArgs e)
> {
> page = (mmBusinessWebPage)this.Page;
> this.oDocument = (Document)page.RegisterBizObj(new Document());
> DataSet dsDocs = this.oDocument.GetAllDocuments();
> Session["dsDocs"] = dsDocs;
> }
>
>Seemed well and good... no compile errors or run errors. BUT, when I tried to do updating of data via mmdatagrid (using the Developers guide examples), nothing happens. I click on the grid's row Edit and Delete buttons but nothing happens.
>One note, i had to use my instance of Page i created to do the binding to my mmdatagrid:
>
> page.BindControl(this.dgDocuments);
>
>Instead of :
>
> this.BindControl(this.dgDocuments);
>
>
>Conversely, when i created the same code but on a webform the database is updated. So, this leads me to believe that my user control is totally missing something or it's impossible to do.
>
>Anybody successfully used MM and ASP.NET web user controls? I've been toiling for days, so any advice will be much welcomed.
>
>Thanks.
>Nat
Gordon de Rouyan
DC&G Consulting
Edmonton, Alberta
Email: derouyag@shaw.ca
Previous
Reply
Map
View

Click here to load this message in the networking platform