K
Kelsey Hart
Guest
Im fairly new to all this (never done any web-scripting before), so please go easy on me.
Ive been working on a project for a company which involves listing database records in GUI form using ASP and C#. The person who had my position before me started the project and decided to use a GridView to display the data. Im not exactly sure why he chose that, but Im picking up where he left off regardless. The point that Im at is updating a record. I want to freeze all controls, while the user is in edit mode to prevent them from altering a different record when a window pops up asking them to confirm (which takes place in the update event). So in the edit event I call a custom built function to disable all controls but the single web control record that the user wishes to edit. These records are created on load or post-back events (dynamic SQL data source).
Now I understand a parent container must be enabled in order for the child to be enabled (if the parent is disabled, it doesnt matter if the child is enabled or disabled, the child will inherit the disabled value from its parent). My question is, can a child web control be disabled while the greater parent container is enabled? This would effectively accomplish what Im looking for as it prevents the other records from being edited or deleted while completing the update.
MORE INFO: MSDN Article - WebControl.Enabled Property
Relevant Code:
protected void MemberGrid_RowEditing(object sender, GridViewEditEventArgs e)
{
MemberGrid.EditIndex = e.NewEditIndex;
EnableControls(Page.Controls, false);
chkOrder();
}
protected void EnableControls(System.Web.UI.ControlCollection controlCollection, bool isEnable)
{
foreach (System.Web.UI.Control control in controlCollection)
{
bool foundChild = containsChild(control.Controls, MemberGrid.Rows[MemberGrid.EditIndex].Cells[2]);
if (control is WebControl && control.HasControls() == false && !foundChild)
{
((WebControl)control).Enabled = isEnable;
}
if (control.HasControls() == true)
{
if (!foundChild && control is WebControl)
{
((WebControl)control).Enabled = isEnable;
EnableControls(control.Controls, isEnable);
}
else if (foundChild && control is WebControl)
{
((WebControl)control).Enabled = !isEnable;
EnableControls(control.Controls, isEnable);
}
else
{
EnableControls(control.Controls, isEnable);
}
}
}
}
protected bool containsChild(System.Web.UI.ControlCollection parent, System.Web.UI.Control child)
{
bool foundChild = false;
foreach (System.Web.UI.Control control in parent)
{
if (control.HasControls() == true)
{
foundChild = containsChild(control.Controls, child);
}
else
{
if (control == child)
{
((WebControl)control).Enabled = foundChild;
return true;
}
}
if (foundChild && control is WebControl)
{
((WebControl)control).Enabled = foundChild;
return true;
}
}
return false;
}
Excuse the rough shape of the code please. Like I said, Im really new to all this. The chkOrder() function just requires the database on postback to apply any changes made through other controls. It is a generic call I use for almost all postback events.
Continue reading...
Ive been working on a project for a company which involves listing database records in GUI form using ASP and C#. The person who had my position before me started the project and decided to use a GridView to display the data. Im not exactly sure why he chose that, but Im picking up where he left off regardless. The point that Im at is updating a record. I want to freeze all controls, while the user is in edit mode to prevent them from altering a different record when a window pops up asking them to confirm (which takes place in the update event). So in the edit event I call a custom built function to disable all controls but the single web control record that the user wishes to edit. These records are created on load or post-back events (dynamic SQL data source).
Now I understand a parent container must be enabled in order for the child to be enabled (if the parent is disabled, it doesnt matter if the child is enabled or disabled, the child will inherit the disabled value from its parent). My question is, can a child web control be disabled while the greater parent container is enabled? This would effectively accomplish what Im looking for as it prevents the other records from being edited or deleted while completing the update.
MORE INFO: MSDN Article - WebControl.Enabled Property
Relevant Code:
protected void MemberGrid_RowEditing(object sender, GridViewEditEventArgs e)
{
MemberGrid.EditIndex = e.NewEditIndex;
EnableControls(Page.Controls, false);
chkOrder();
}
protected void EnableControls(System.Web.UI.ControlCollection controlCollection, bool isEnable)
{
foreach (System.Web.UI.Control control in controlCollection)
{
bool foundChild = containsChild(control.Controls, MemberGrid.Rows[MemberGrid.EditIndex].Cells[2]);
if (control is WebControl && control.HasControls() == false && !foundChild)
{
((WebControl)control).Enabled = isEnable;
}
if (control.HasControls() == true)
{
if (!foundChild && control is WebControl)
{
((WebControl)control).Enabled = isEnable;
EnableControls(control.Controls, isEnable);
}
else if (foundChild && control is WebControl)
{
((WebControl)control).Enabled = !isEnable;
EnableControls(control.Controls, isEnable);
}
else
{
EnableControls(control.Controls, isEnable);
}
}
}
}
protected bool containsChild(System.Web.UI.ControlCollection parent, System.Web.UI.Control child)
{
bool foundChild = false;
foreach (System.Web.UI.Control control in parent)
{
if (control.HasControls() == true)
{
foundChild = containsChild(control.Controls, child);
}
else
{
if (control == child)
{
((WebControl)control).Enabled = foundChild;
return true;
}
}
if (foundChild && control is WebControl)
{
((WebControl)control).Enabled = foundChild;
return true;
}
}
return false;
}
Excuse the rough shape of the code please. Like I said, Im really new to all this. The chkOrder() function just requires the database on postback to apply any changes made through other controls. It is a generic call I use for almost all postback events.
Continue reading...