Would it be possible to create a control called say 'AbsolutePanel' that sets the position to 'absolute' for any controls placed on the panel ?
That will also work in VS2010 design time environment ??
Cristian Libardo posted some code which allows you to walk through all controls on a panel:
IEnumerable<Control> EnumerateControlsRecursive(Control parent)
{
foreach (Control child in parent.Controls)
{
yield return child;
foreach (Control descendant in EnumerateControlsRecursive(child))
yield return descendant;
}
}
You can use it like this:
foreach (Control c in EnumerateControlsRecursive(Page))
{
if(c is TextBox)
{
// do something useful
}
}
Could this be used to set the design time & runtime position attribute/property of all controls on the 'AbsolutePanel' ?
Just a thought.
It would make 'Application Form' style development similar to WPF/Winform for fixed size/dialogs/forms.
Any info appreciated.
Thanks
Related
I have tried searching around for this, but what I found was mainly for disabling a single input type.
What I want to do is disable every input type on a single page. Everything. Textboxes, checkboxes the whole lot.
I couldnt figure out how to modify the loops I found, which is why I am asking here, beacause it's likely one of you has a piece of code laying around that can do it.
Thank you in advance.
try below if you like to do it in javascript/jquery
$(document).ready(function(){
$('input').attr('disabled','disabled');
});
OR try below if you want in asp.net
ach control has child controls, so you'd need to use recursion to reach them all:
protected void DisableControls(Control parent, bool State) {
foreach(Control c in parent.Controls) {
if (c is DropDownList) {
((DropDownList)(c)).Enabled = State;
}
DisableControls(c, State);
}
}
Then call it like so:
protected void Event_Name(...) {
DisableControls(Page,false); // use whatever top-most control has all the dropdowns or just the page control
} // divs, tables etc. can be called through adding runat="server" property
I'm simply trying to change all my RadTextBox borders to black. i'm sure the structure of my foreach loop is right, however i'm not able to find any RadTextBoxes.
I have a masterpage called master.Page and a childpage inheriting that page called child.aspx.
There is 10 radtextboxes on child.aspx, but i'm unable to find any, i've tried.....
foreach (var control in this.Controls.OfType<RadTextBox>())
{
control.BorderColor = System.Drawing.Color.Black;
}
foreach (var control in this.Page.Controls.OfType<RadTextBox>())
{
control.BorderColor = System.Drawing.Color.Black;
}
Since you've mentioned that you're using MasterPages. You''ll find the controls which are on top of your child aspx pages from master not via this.Controls but via contentPlaceHolder1.Controls since that is the NamingContainer.
If you would have other textboxes in child controls like FormView or GridView you woudn't even find them this way because Enumerable.OfType does not search recursively into child controls of a given control. You can try this recursive extension with OfType:
public static IEnumerable<T> Traverse<T>(this IEnumerable<T> source, Func<T, IEnumerable<T>> fnRecurse)
{
foreach (T item in source)
{
yield return item;
IEnumerable<T> seqRecurse = fnRecurse(item);
if (seqRecurse != null)
{
foreach (T itemRecurse in Traverse(seqRecurse, fnRecurse))
{
yield return itemRecurse;
}
}
}
}
Use it in following way:
var allRadTextBoxes = this.Controls.Cast<Control>()
.Traverse(c => c.Controls.OfType<RadTextBox>());
foreach(var radTextBox in allRadTextBoxes)
{
radTextBox.BorderColor = System.Drawing.Color.Black;
}
The problem is because your control is not directly on the page. Your control is in a Form that is on the page.
foreach (var control in this.Page.Form.Controls.OfType<RadTextBox>())
{
control.BorderColor = System.Drawing.Color.Black;
}
You can either look for the Controls inside the form, or search recursively using Tim Schmelter approach
hope you help me with I think a simple TreeView Expand problem.
I have a TreeView control in my MasterPage and my default depth is 2 and I see that when I click on the deeper node it keeps expanded.. But when I redirected into another page, the node collapsed.
I have a problem with my code which suppose to keep the node expanded.
TreeNode thisNode = tvCategories.FindNode(Session["SelectedCIDValPath"].ToString());
if (thisNode != null)
{
thisNode.Selected = true;
thisNode.Expand();
thisNode.Select();
thisNode.Expanded = true;
lbl.Text = "valupath: " + Session["SelectedCIDValPath"].ToString();
}
as you can see, I tried all the possible properties and methods to keep the deeper node expanded.. but it doesn't work.
Please help me? Thank you so much
It happens to be the case (and I find it just a bit frustrating) that expanding a node does not also cause parent nodes to expand. In order to ensure a node expands, it is necessary to also ensure that the parent nodes expand. I keep an extension method handy for this purpose:
public static void EnsureExpanded(this TreeNode node)
{
if (node != null)
{
EnsureExpanded(node.Parent);
node.Expand();
}
}
You can employ the extension like so:
TreeNode thisNode = tvCategories.FindNode(Session["SelectedCIDValPath"].ToString());
thisNode.EnsureExpanded();
What is the trick to get the controls inside the FormView. I was getting them with FindControl() but, now i cant get access on them. Example: i have some ImageButton on the FooterTemplate, great i can get those smoothly, when it comes to the controls inside the FormView!!! null every control. Do you think i should name them differently in every template?
This gets me thinking about the table causing this noise!
I'm using the DataBound Event and checking for specific Mode! Any ideas? Thank you.
[UPDATED]
This is working
if (this.kataSistimataFormView.CurrentMode == FormViewMode.Edit)
{
ImageButton update = (ImageButton)this.kataSistimataFormView.FindControl("btnUpdate");
update.Visible = true;
But this for some reason no
CheckBox chkBoxPaidoi = kataSistimataFormView.FindControl("chkBoxPaidoi") as CheckBox;
FindControl is not recursive. What I mean is that it will only find controls that are within the child controls of the control you are searching - it will not search any child controls of the child controls
If you have placed the control you previously were looking for within another control then you will have to either search within that new control or, if you still want to use kataSistimataFormView as the parent control, you may have to use a recursive search.
Google for "findcontrol recursive" there are some good examples that you can probably just cut-and-paste.
As it seems this was caused because of the same naming ID's on various templates, Insert,Edit,Item. Even this is supported by the compiler, has problem when you are going for them programmaticaly later.
Thank you all.
Did you ever get this figured out? If you know the ID you can use this recursive function:
private Control FindControlRecursive(Control root, string id)
{
if (root.ID == id)
{
return root;
}
foreach (Control c in root.Controls)
{
Control t = FindControlRecursive(c, id);
if (t != null)
{
return t;
}
}
return null;
}
Found here:
http://www.codinghorror.com/blog/2005/06/recursive-pagefindcontrol.html
I am having a number of panels in my page in which I am collecting user information and saving the page details. The page panel has textbox, dropdown list, listbox.
When I need to come to this page. I need to show the Page if these controls have any values. How to do this?
It boils down to enumerating all the controls in the control hierarchy:
IEnumerable<Control> EnumerateControlsRecursive(Control parent)
{
foreach (Control child in parent.Controls)
{
yield return child;
foreach (Control descendant in EnumerateControlsRecursive(child))
yield return descendant;
}
}
You can use it like this:
foreach (Control c in EnumerateControlsRecursive(Page))
{
if(c is TextBox)
{
// do something useful
}
}
You can loop thru the panels controls
foreach (Control c in MyPanel.Controls)
{
if (c is Textbox) {
// do something with textbox
} else if (c is Checkbox) {
/// do something with checkbox
}
}
If you have them nested inside, then you'll need a function that does this recursively.
I know this is an old post, and I really liked christian libardo's solution. However, I do not like the fact that in order to yield an entire set of elements to the outer scope I would have to iterate over those elements yet again only to yield those to myself from an inner scope to the current scope. I prefer:
IEnumerable<Control> getCtls(Control par)
{
List<Control> ret = new List<Control>();
foreach (Control c in par.Controls)
{
ret.Add(c);
ret.AddRange(getCtls(c));
}
return (IEnumerable<Control>)ret;
}
Which allows me to use it like so:
foreach (Button but in getCtls(Page).OfType<Button>())
{
//disable the button
but.Enabled = false;
}
Depeding on which UI library or language you are using, container controls such as panels maintain a list of child controls. To test if a form/page has any data you need to recursively search each panel for data entry controls such as text boxes. Then test if any of the data entry controls contain values other than default value.
A simpler solutions would be to implement an observer class that attaches to the changed events of your data controls. If the observer is triggered then your page has changes. You will need to take into consideration actions such as changing and then reverting data.
Very similar solution to Cristian's here, which uses recursion and generics to find any control in the page (you can specify the control at which to start searching).
http://intrepidnoodle.com/articles/24.aspx