I'm trying to use FindControl() to SET the values of some DropDownLists on a page.
for some reason, it appears that FindControl() is returning a copy of the control object as opposed to a reference, I was under the impression that this would not be the case due to the lack of a copy constructor for Controls?
<EDIT>
Ok, it apparears that it is not in fact returning a copy, it's simply not letting me set the visibilty of a control, the other properties I'm setting work fine.
Does anyone have any insight as to why this might be the case?
I've tried setting it in quick-watch mode then looking at the value straight away, and that isn't actually changing the value either!
</EDIT>
<EDIT> (two)
Ok, I'm doing this in Page_Load, and it's not in a gridview (I like how you guys assumed that one cus I was using FindControl()).
I'm doing this as there is a set of operations I have to perform on a dynamic number of similarly named lists, and it's MUCH better to do it in a loop than to hard code it.
</EDIT>
Can anyone help?
Cheers, Ed
Edit:
Whoa, wait... are you setting the Visible property to true rather than false?
In your comment, you mention it changes back instantly. The Visible property will evaluate to false if it's parent is set to be invisible, no matter how often you tell it the value should be true.
Where are you calling FindControl() and setting the Visible property?
For info, FindControl() gets a reference to a control in the current naming container, based on a string id and does not copy the control. In order to work with the control, you should cast it to the type of control that you expect -
DropDownList ddl = (DropDownList)e.Row.FindControl("myDropDownList");
this example would allow you to work with a DropDownList control in the OnRowDataBound event of a GridView.
Related
I'm working a WPF application which has a number of groupboxes, each containing 3-4 controls a piece. Each of these groupboxes contains a reset button.
Instead of explicitly writing a line for each item the groupbox reset buton should reset is there a way to have the reset button only reset all items for the groupbox it's contained upon?
When researching this issue I found the following post which seems relevant but I'm unsure how to extend Nathan's answer to do this: What is the best way to clear all controls on a form C#?
You can use this question as an source of methods you need.
First of all you need to find groupbox that contains button. To do that use sender argument form click event it is reference to button that was clicked and call FindVisualParent method from one of answers in that question. This will give you groupbox.
Then Find all ui elements by FindVisualChildren<DependencyObject> method. This will give you super set of controls that are to be cleared.
Then it is just a loop over this as in question that you referenced. Note that that question was intended for winforms not wpf so you will have to revise the controldefaults dictionary which handles grouping controls ~(Panel GroupBox) which is not needed here (and would not work in wpf).
You might add a TAG to each control telling which property is beeing edited in the control, and then on reset press, you travel up the button to find containing GroupBox, then down to find all controls contained in this GroupBox, and then you find, with the TAGs, the name of the properties that needs reset. You then reset the properties using Reflexion.
Maybe an issue is that the default value might not be obvious. But you could store the default values for all your bindings in a static object, and copy the value of the property for this reset object into the current status object.
Rq : you could also use reflexion to get the bindings for all control of current GroupBox, and then get the bounded properties from the binding, so no need for a TAG. BUT if a control has several bindings, you won't know which one to reset. In my case, controls within GroupBoxes have also a color binding to indicate wether they have default value (green) or non-default (red), hence the need for a TAG. (BUT the color indicator are read-only so reflexion could, in fact, also be used with a little more use of Reflexion... was just too lazy to change a working logic when i saw that :-) )
I don't know about your bindings so obviously i cannot tell what's best, i hope this thoughs can help.
In WinForm, I have a combobox with DropDownStyle set to DropDownList (so can't enter a Text). In the properties window, there is the Items property which is a string collection. I enter all my values.
But now, I would like to set one of these value by default (instead to have the empty entry at run-time). I know how to do this via coding, but I am pretty sure (damn memory) that it was possible to set one of the value in the string collection as default by adding a special symbole in front of the line.
Anybody know that symbole? Or my memory is playing me trick and it is not possible to do it via the designer?
Doesn't look like it can be done when using a DropDownList. From here it is suggested that you can set the text property to the default value you want, but this will only works in a DropDown rather than DropDownList style.
I'm sorry but that is not possible within the Designer only, since the Text property is used for this feature and that property is ignored/cleared when the using a DropDownList.
If you don't mind having your data values outside of the Designer, you could probably use DataBinding to accomplish this since the DisplayMember and ValueMember properties of ComboBox can be used in the Designer and would set the display value. I don't normally use DataBinding so unfortunately I cannot provide code examples - perhaps another user can chime in?
I wonder if someone can help.
I have a LookupEdit within a Devexpress Xtragrid which is populated from a list of filenames, as well as an empty option, and a "new" option.
If the user selects the "new" option, it goes off to an upload form (which is fine), uploads to the new directory and returns to the form.
The problem is that I want to set the form to the new value, but I can't get it to work.
In the EditValueChanging for the lookupedit, I repopulate the lookup and then use GetKeyValueByDisplayText, but the value returned is null for some reason. Yet I've defined an object for uploadfile with a property of Filename and set both the displaymember and valuemember to Filename.
If anyone can help, as I've exhausted my options.
The problem might appear because the new list hasn't been loaded by the editor yet. Please try to call the
(gridView.ActiveEditor as LookupEdit).Properties.ForceInitialize();
method. Does this work for you?
I just wanted to confirm couple of things.
I) Code snippet:
cmb1.Datasource= dt;
cmb1.Valuemember = "value";
Does the control rendering happens 2 time for the control, 1 more time extra because of the value member getting changed after data source assigned. Is this so?
II) How can I trace these re-populations in C#? I just wanted to debug and see and confirm? Example please?
Thanks
Karthik
I) It depends. If this is for an asp.net site, you have to also call the DataBind() method before anything actually happens. But otherwise, yes - databinding will likely happen twice.
II) You can trace it by building a method that returns a datatable, and binding to that method as the data source. Then you can set a breakpoint inside the method and watch for when the breakpoint is hit.
This depends on the actual implementation of the data bound control. I'd expect a "good" control to only retrieve the data when it needs it, and therefore changing these properties would not do anything but clear out existing bindings (if there were any) and create the new ones without actually retrieving data.
You can use a profiler to trace such things, or bind to a class of yours and set a breakpoint or increase a counter.
I'm trying to use the selected value of a ListBox control to populate a TextBox with its Text property, and a HiddenField with its value property. It sounds simple enough, and I went with this:
currentGroupTextBox.Text = currentSiteGroupList.SelectedItem.Text;
currentGroupHiddenField.Value = currentSiteGroupList.SelectedValue;
But upon execution, ASP.NET returns an error:
Object reference not set to an instance of an object.
And highlights the first line. currentGroupTextBox and currentGroupHiddenField are two controls which are enabled in the ASPX file so I'm not too sure why ASP.NET would complain about instancing them.
I'm going to try and pull together answers to all of your questions, including those in the comments.
Even if SelectionMode="Single", the listbox starts out without anything being selected, unless you specify which item should be selected in your code.
To test if the SelectedItem is null, use the following code:
if (currentGroupSiteList.SelectedItem != null) {
currentGroupTextBox.Text = currentSiteGroupList.SelectedItem.Text;
currentGroupHiddenField.Value = currentSiteGroupList.SelectedValue;
}
What does your Page_Load code that loads the listbox look like? Is it wrapped with a if (!Page.IsPostBack) check? If not, pressing the button and initiating a postback will reload the listbox, thus losing the SelectedItem that the user selected.
I'd be willing to bet that your first line is choking on referencing currentSiteGroupList.SelectedItem, as that seems the most likely candidate for being a null reference. Make sure your code is executing in the right place within the ASP.NET page lifecycle, such that the SelectedItem is set properly behind the scenes.
SelectedItem from your currentSiteGroupList.SelectedItem is likely to be null (what represents no selection). You need to test it before assigning it to currentGroupTextBox.Text
Is it currentGroupTextBox or currentGroupTextBox that is null?
In debug if stop on that line..is it one or both that are not existing?
A common issue I find is that controls are placed on an asp formview or similar and so the reference to that control is not actually its id/name, but more likely you need to do;
TextBox myTextBoxReference = (TextBox) formName.FindControl("currentGroupTextBox")
string theValue = myTextBoxReference.Text
Another often issue is the page life cycle. So if your object is not on a form but maybe you are referencing it before it exists in the postback.
hth