Setting private field on user control prevents changing 'Visible' from working - c#

In a page, I have an event handler that sets 'Visible' to false on one control and true on another. Stepping through debug, I see that these values get set properly, and the control marked visible goes through OnPreRender while the control I have set to invisible does not. So all of that seems to be working as expected. However, when the request completes, the visibility has not changed at all on the page. I've tried setting the directly parent UpdatePanel to 'always' and have tried manually calling 'Update()' on it with no effect. Any clue as to what is going on here?
UPDATE:
I have found that it is only setting a private property on my user control that causes this whole thing to not work. I have included an example of that control and all of the places it references the private field.
Example:
protected override void OnLoad(EventArgs e)
{
if (this.IsPostback)
{
return;
}
this.Control1.Visible = true;
this.Control2.Visible = false;
}
protected void OnButtonClicked (object sender, EventArgs e)
{
this.Control1.Visible = false;
this.Control2.Visible = true;
// this has desired results when it fires
}
protected void OnUserControlEventThatFiresAfterRowCommand (object sender, EventArgs e)
{
this.Control2.SomeProp = this.GetSomeObject();
this.Control1.Visible = false;
this.Control2.Visible = true;
// this does not have desired results, even though it does fire
}
And then in Control2:
private SomeClass privatefield;
public SomeClass SomeProp
{
get
{
return this.privatefield;
}
set
{
this.PopulateFields(value);
this.privatefield = value;
// If I comment out this line it works!
}
}
protected override void LoadViewState(object savedState)
{
object[] state = savedState as object[];
base.LoadViewState(state[0]);
this.Enabled = state[1] as bool? ?? true;
this.SomeProp = state[2] as SomeClass;
this.Visible = state[3] as bool? ?? true;
}
protected override object SaveViewState()
{
return new object[]
{
base.SaveViewState(),
this.Enabled,
this.SomeProp,
this.Visible
};
}

I finally found out why by looking at the actual response body.
156|error|500|Error serializing value 'withheld class name' of type 'withheld class name'|
Why this error was not being thrown in debug is beyond me, but for anyone else reading this question looking for answers, look at your response bodies! It is because I was trying to put my instance of a class into Viewstate but that class was not marked with the Serializable attribute.

Related

Combobox value not saving for 1 specific client

Not sure if anyone can be of help here, but I have a comboxbox bound to a viewmodel property, on a form who's value is set by an event. It is working in house, but there is one client where the event fires, the value is set (I know because I added some logging), but their screen is not updated. I have a copy of the database, and I mirror the steps and it works. Any ideas why that could be happening? I included the code below, but it is pretty basic.
private void ViewModel_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == "TriggerId")
{
Method();
}
}
private void Method()
{
ComboBoxSelectedProperty = null;
if (TriggerId != null)
{
var object = Work.ObjectStore.GetById((int)TriggerId);
if (object != null)
{
ComboBoxSelectedProperty = Work.AssociatedObjectStore.GetByObjenct(object);
}
NotifyPropertyChanged("ComboboxSourceSource");
}
}
}

Dropdownlist changing the selected value when control is out of focus

I have several comboboxes with the same properties.
Dropdownstyle : Dropdownlist
AutoCompleteMode: SuggestAppend
AutoCompleteSource: ListItems
For example, I have a dropdownlist cboxStates that has 50 states of United States of America in Items Collection entered manually. When I type in WI, it is highlighted among WA,WV,WI,WY but if I tab over/press enter/mouse click on another control, WA is selected instead of WI which is highlighted. This is total random and it happens to comboboxes that are binded dynamically. And also, they do not have any events.
This seems to be an issue that has been submitted to Connect. There's a workaround there which extends the default ComboBox control and fixes the issue. The extended ComboBox code is horribly formatted on the Connect site, so here's the nicer version :)
public class BetterComboBox : ComboBox
{
private int _windows7CorrectedSelectedIndex = -1;
private int? _selectedIndexWhenDroppedDown = null;
protected override void OnDropDown(EventArgs e)
{
_selectedIndexWhenDroppedDown = SelectedIndex;
base.OnDropDown(e);
}
private bool _onDropDownClosedProcessing = false;
protected override void OnDropDownClosed(EventArgs e)
{
if (_selectedIndexWhenDroppedDown != null && _selectedIndexWhenDroppedDown != SelectedIndex)
{
try
{
_onDropDownClosedProcessing = true;
OnSelectionChangeCommitted(e);
}
finally
{
_onDropDownClosedProcessing = false;
}
}
base.OnDropDownClosed(e);
if (SelectedIndex != _windows7CorrectedSelectedIndex)
{
SelectedIndex = _windows7CorrectedSelectedIndex;
OnSelectionChangeCommitted(e);
}
}
protected override void OnSelectionChangeCommitted(EventArgs e)
{
if (!_onDropDownClosedProcessing)
_windows7CorrectedSelectedIndex = SelectedIndex;
_selectedIndexWhenDroppedDown = null;
base.OnSelectionChangeCommitted(e);
}
protected override void OnSelectedIndexChanged(EventArgs e)
{
bool alreadyMatched = true;
if (_windows7CorrectedSelectedIndex != SelectedIndex)
{
_windows7CorrectedSelectedIndex = SelectedIndex;
alreadyMatched = false;
}
base.OnSelectedIndexChanged(e);
//when not dropped down, the SelectionChangeCommitted event does not fire upon non-arrow keystrokes due (I suppose) to AutoComplete behavior
//this is not acceptable for my needs, and so I have come up with the best way to determine when to raise the event, without causing duplication of the event (alreadyMatched)
//and without causing the event to fire when programmatic changes cause SelectedIndexChanged to be raised (_processingKeyEventArgs implies user-caused)
if (!DroppedDown && !alreadyMatched && _processingKeyEventArgs)
OnSelectionChangeCommitted(e);
}
private bool _processingKeyEventArgs = false;
protected override bool ProcessKeyEventArgs(ref Message m)
{
try
{
_processingKeyEventArgs = true;
return base.ProcessKeyEventArgs(ref m);
}
finally
{
_processingKeyEventArgs = false;
}
}
}

HiddenField.Value is an empty string upon postback inside my custom control

I have a custom control that has a hidden field. Upon postback I want to obtain the value stored in it, but it's always an empty string. any thoughts?
I am performing client-side manipulation of the hidden field values and have verified in firebug that the fields are correct before issue a post back
Here is the setup:
public class DualListPanel : SWebControl, INamingContainer
{
protected IList<DlpItem> UnassignedList { get; set; }
protected IList<DlpItem> AssignedList { get; set; }
private HiddenField assignedItemsField, unassignedItemsField;
public DualListPanel()
{
CssClass = "DualListPanel";
EnableViewState = true;
}
#region ViewState
protected override void LoadViewState(object savedState)
{
var state = savedState as object[];
UnassignedList = state[0] as List<DlpItem>;
AssignedList = state[1] as List<DlpItem>;
base.LoadViewState(state[2]);
}
protected override object SaveViewState()
{
object[] state = new object[3];
state[0] = UnassignedList;
state[1] = AssignedList;
state[2] = base.SaveViewState();
return state;
}
#endregion
#region WebControl Overrides
protected override void OnInit(EventArgs e)
{
EnsureChildControls();
GetUnassignedList(); //omitted method
GetAssignedList(); //omitted method
base.OnInit(e);
}
protected override void CreateChildControls()
{
assignedItemsField = new HiddenField();
assignedItemsField.ID = "HiddenAssignedItems";
assignedItemsField.EnableViewState = true;
unassignedItemsField = new HiddenField();
unassignedItemsField.ID = "HiddenUnassignedItems";
unassignedItemsField.EnableViewState = true;
Controls.Add(assignedItemsField);
Controls.Add(unassignedItemsField);
base.CreateChildControls();
}
#endregion
#region Item Lists Retrieval
public string GetCommaDelimUnassignedItems()
{
return unassignedItemsField.Value;
}
public string GetCommaDelimAssignedItems()
{
return assignedItemsField.Value;
}
#endregion
}
I think hidden field's value does not lost during postback,
Put your code in Ispostback, whenever you initialize hidden fields.
protected override void CreateChildControls()
{
if(!ispostback){
assignedItemsField = new HiddenField();
assignedItemsField.ID = "HiddenAssignedItems";
assignedItemsField.EnableViewState = true;
unassignedItemsField = new HiddenField();
unassignedItemsField.ID = "HiddenUnassignedItems";
unassignedItemsField.EnableViewState = true;
Controls.Add(assignedItemsField);
Controls.Add(unassignedItemsField);
base.CreateChildControls();
}
}
Ugggh I omitted information that would have been useful. I was primarily testing if I could access the values during the page cycle process. Not necessarily from a page calling
GetCommaDelimUnassignedItems();
I realized I had implemented OnInit() and made calls to check the value (I omitted it thinking it was not useful to the issue). Completely forgot that the ViewState will not be loaded during OnInit(). I changed it to OnPreRender() and it's working fine now
It looks like you simply need to mark your 2 hidden fields protected instead of private.

how to access i statement in usercontrol

How can I go about accessing the result of an if statement in a user control?
UserControl code:
public bool SendBack(bool huh)
{
if(huh)
huh = true;
else huh = false;
return huh;
}
And in a separate project i am trying to access it like this:
private void button1_Click(object sender, EventArgs e)
{
MyControl.TextControl t = (MyControl.TextCOntrol)sender;
if(t.SendBack(true))
{
// Do something.
}
}
In this case I thing the sender will be the button1, so it will not be castable to your usercontrol...
You will need a reference form the container (form/panel/...) that contains your usercontrol.
Also, I know this might be for simplicity but you can change
public bool SendBack(bool huh)
{
if(huh)
huh = true;
else huh = false;
return huh;
}
to
public bool SendBack(bool huh)
{
return huh;
}
You might also want to take a look at Control.ControlCollection.Find Method
Searches for controls by their Name
property and builds an array of all
the controls that match.

Differentiating between events raised by user interaction and my own code

The SelectedIndexChanged event gets fired in my application from a combo box when:
the user chooses a different
item in the combo box, or when:
my own code updates the combo
box's SelectedItem to reflect that
the combo box is now displaying
properties for a different object.
I am interested in the SelectedIndexChanged event for case 1, so that I can update the current object's properties. But in case 2, I do not want the event to fire, because the object's properties have not changed.
An example may help. Let's consider that I have a list box containing a list of people and I have a combo box representing the nationality of the currently selected person in the list. Case 1 could happen if Fred is currently selected in the list, and I use the combo box to change his nationality from English to Welsh. Case 2 could happen if I then select Bob, who is Scottish, in the list. Here, my list update event-handler code sees that Bob is now selected, and updates the combo box so that Scottish is now the selected item. This causes the combo box's SelectedIndexChanged event to be fired to set Bob's nationality to Scottish, even though it already is Scottish.
How can I update my combo box's SelectedItem property without causing the SelectedIndexChanged event to fire? One way would be to unregister the event handler, set SelectedItem, then re-register the event handler, but this seems tedious and error prone. There must be a better way.
I created a class I called SuspendLatch. Offers on a better name are welcome, but it does what you need and you would use it like this:
void Method()
{
using (suspendLatch.GetToken())
{
// Update selected index etc
}
}
void listbox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (suspendLatch.HasOutstandingTokens)
{
return;
}
// Do some work
}
It's not pretty, but it does work, and unlike unregistering events or boolean flags, it supports nested operations a bit like TransactionScope. You keep taking tokens from the latch and it's only when the last token is disposed that the HasOutstandingTokens returns false. Nice and safe. Not threadsafe, though...
Here's the code for SuspendLatch:
public class SuspendLatch
{
private IDictionary<Guid, SuspendLatchToken> tokens = new Dictionary<Guid, SuspendLatchToken>();
public SuspendLatchToken GetToken()
{
SuspendLatchToken token = new SuspendLatchToken(this);
tokens.Add(token.Key, token);
return token;
}
public bool HasOutstandingTokens
{
get { return tokens.Count > 0; }
}
public void CancelToken(SuspendLatchToken token)
{
tokens.Remove(token.Key);
}
public class SuspendLatchToken : IDisposable
{
private bool disposed = false;
private Guid key = Guid.NewGuid();
private SuspendLatch parent;
internal SuspendLatchToken(SuspendLatch parent)
{
this.parent = parent;
}
public Guid Key
{
get { return this.key; }
}
public override bool Equals(object obj)
{
SuspendLatchToken other = obj as SuspendLatchToken;
if (other != null)
{
return Key.Equals(other.Key);
}
else
{
return false;
}
}
public override int GetHashCode()
{
return Key.GetHashCode();
}
public override string ToString()
{
return Key.ToString();
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (!disposed)
{
if (disposing)
{
// Dispose managed resources.
parent.CancelToken(this);
}
// There are no unmanaged resources to release, but
// if we add them, they need to be released here.
}
disposed = true;
// If it is available, make the call to the
// base class's Dispose(Boolean) method
//base.Dispose(disposing);
}
}
}
I think the best way would be to use a flag variable:
bool updatingCheckbox = false;
void updateCheckBox()
{
updatingCheckBox = true;
checkbox.Checked = true;
updatingCheckBox = false;
}
void checkbox_CheckedChanged( object sender, EventArgs e )
{
if (!updatingCheckBox)
PerformActions()
}
[Edit: Posting only the code is not really clear]
In this case, the event handler wouldn't perform its normal operations when the checkbox is changed through updateCheckBox().
I have always used a boolean flag variable to protect against unwanted event handlers. The TaskVision sample application taught me how to do this.
Your event handler code for all of your events will look like this:
private bool lockEvents;
protected void MyEventHandler(object sender, EventArgs e)
{
if (this.lockEvents)
{
return;
}
this.lockEvents = true;
//Handle your event...
this.lockEvents = false;
}
I let the event fire. But, I set a flag before changing the index and flip it back after. In the event handler, I check if the flag is set and exit the handler if it is.
I think your focus should be on the object and not on the event that's occuring.
Say for example you have the event
void combobox_Changed( object sender, EventArgs e )
{
PerformActions()
}
and PerformActions did something to the effect of
void PerformActions()
{
(listBox.SelectedItem as IPerson).Nationality =
(comboBox.SelectedItem as INationality)
}
then inside the Person you would expect to see something to the effect of
class Person: IPerson
{
INationality Nationality
{
get { return m_nationality; }
set
{
if (m_nationality <> value)
{
m_nationality = value;
this.IsDirty = true;
}
}
}
}
the point here is that you let the object keep track of what is happening to itself, not the UI. This also lets you keep track of dirty flag tracking on your objects, which could be useful for persistence later on.
This also keeps your UI clean and keeps it from getting odd event registration code that will most likely be error prone.
I have finally found a solution to avoid the uncessary event from being fired too many time.
I use a counter and I only hook/unhook the events I want to mask once when it is not needed, and when it is needed again.
The example below shows how I hide the CellValueChanged event of a datagrid.
EventMask valueChangedEventMask;
// In the class constructor
valueChangedEventMask = new EventMask(
() => { dgv.CellValueChanged += new DataGridViewCellEventHandler(dgv_CellValueChanged); },
() => { dgv.CellValueChanged -= new DataGridViewCellEventHandler(dgv_CellValueChanged); }
);
// Use push to hide the event and pop to make it available again. The operation can be nested or be used in the event itself.
void changeCellOperation()
{
valueChangedEventMask.Push();
...
cell.Value = myNewCellValue
...
valueChangedEventMask.Pop();
}
// The class
public class EventMask
{
Action hook;
Action unHook;
int count = 0;
public EventMask(Action hook, Action unHook)
{
this.hook = hook;
this.unHook = unHook;
}
public void Push()
{
count++;
if (count == 1)
unHook();
}
public void Pop()
{
count--;
if (count == 0)
hook();
}
}

Categories

Resources