Viewstate set Property value - c#

I use viewstate in application where I have found that encapsulating viewstate in property is much more convenient.
Here is the code what I do in my code.
public List<TransactionEntity> TransactionData
{
get
{
if(ViewState["TransactionData"] == null)
return new List<TransactionEntity>();
return _TransactionData as List<TransactionEntity>();
}
set
{
How to set value to gridview from here ??
}
}
Now i have a gridview in user control which I need to bind everytime I assign value to this property.
Anyone knows how to do it.
Thanks.

Now i have a gridview in user control which I need to bind everytime I
assign value to this property.
Anyone knows how to do it
set
{
ViewState["TransactionData"] = value;
gridview.DataSource = value as List<TransactionEntity>; //check if null etc
gridview.DataBind();// ...
}

Related

asp.net DropDownList set SelectedValue NOT working

I have a user-control contains a DropDownList, and this user-control is used inside another one. I am passing the selected value to the drop-down from the outer user control to the inner one, and I am tracking the value passed (during debugging) and the value is passed correctly, but at least the value is NOT set correctly!
Here is the code of the outer user-control
public int SelectedPhoneNumberCountryCode
{
get { return ucRecoveryPhoneNumber.SelectedPhoneNumberCountryCode; }
set { ucRecoveryPhoneNumber.SelectedPhoneNumberCountryCode = value; }
}
And this is the code of the inner user-control (ucRecoveryPhoneNumber)
public int SelectedPhoneNumberCountryCode
{
get { return int.Parse(DdlCountryPhoneCodes.SelectedValue); }
set { DdlCountryPhoneCodes.SelectedValue = value.ToString(); }
}
At the Page_Load, the value is passed correctly until the last step, but it is not set correctly! and the drop-down is always selected as the first value (default one).
Q What is wrong with this code? or What should be done to make it work?

Field property is required. All field web controls require the field name to be set

I am assigning the field name of Sitecore image control dynamically from code behind file like below:
.ascx
<sc:Image runat="server" ID="scImgRelatedArticle"></sc:Image>
.ascx.cs
if(currentItem != null)
{
Sitecore.Web.UI.WebControls.Date scDateArticleDate = e.Item.FindControl("scDateArticleDate") as Sitecore.Web.UI.WebControls.Date;
if (scDateArticleDate != null)
{
if (DisplayDates)
{
scDateArticleDate.Field = StartDateFieldName;
scDateArticleDate.Item = currentItem;
}
}
}
Sometimes current Item is null i don't want to assign any field value. I dont want to display the item. But i am ending up with an error message "Field property is required. All field web controls require the field name to be set."
Is there a way in sitecore to do this automatically if i didn't specify the scDateArticleDate.Item property.
You should always set the Field property
scDateArticleDate.Field = StartDateFieldName // where is a string right!
Then you control the visibility of the item depending on if you have or not the item.
Also notice you post a image in your ascx and a date field in the .cs
the complete code would be
scDateArticleDate.Field = StartDateFieldName; //always set the field
if(currentItem != null)
{
Sitecore.Web.UI.WebControls.Date scDateArticleDate = e.Item.FindControl("scDateArticleDate") as Sitecore.Web.UI.WebControls.Date;
if (scDateArticleDate != null)
{
if (DisplayDates)
{
scDateArticleDate.Item = currentItem;
scDateArticleDate.Visible = true;
}
else
{
scDateArticleDate.Visible = false;
}
}
}
cheers
You are not assigning the Sitecore field to the sc:image web control,it should work as:
Sitecore.Data.Fields.Date scDateArticleDate=(Sitecore.Data.Fields.Date)e.Item.FindControl("scDateArticleDate");

How to block assign value in setter in WPF

I have for example property like this:
private string foobar;
public string Foobar
{
get
{
return this.foobar;
}
set
{
if (value != this.foobar)
{
// here I want to check if value is correct
if(value != something)
{
this.foobar = value;
this.NotifyPropertyChanged("Foobar");
}
else
{
value = null;
this.foobar = null;
this.NotifyPropertyChanged("Foobar");
}
}
}
}
Property is binded (MVVM) to Listview :
SelectedItem="{Binding Foobar, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}
.
And when user changes value in list, selecteditem is changed and value is set in setter. I code is ok, when user selected incorrect value, to value and foobar null is assigned. But in WPF still selected value is displayed. When I set breakpoint in getter I can see that it return null too. How to refresh WPF to clear selected value in listview ? It shoud be empty like at the begin.
Thanks
The problem with your code is that you want to override value assigned by binding in your setter method. This will not work because control will not update on next property change for simple reason that it has invoked it by setting property. To implement validation on your values try this build in mechanism.
i would prefer using IDataErrorInfo and dont use property setter logic. the main advantage is that your property value in your viewmodel and your view are always the same and your viewmodel has the information wether the value is ok or not.

Storing an List<int> in viewstate

I have an aspx page which has the following:
A repeater with a linkbutton in each
The link button has a commandargument of an integer value
A user control
The idea is that when the user clicks on the linkbutton the value of the commandarguement is stored in a List. No problem you may think, however I need the value to be stored in an List in the usercontrol, not in the ASPX page. The List needs to be persisted across postbacks, so it also needs to be stored in the viewstate.
So I created a public property in the user control like so:
public List<int> ImageString {
get {
if (this.ViewState["ImageString"] != null) {
return (List<int>)(this.ViewState["ImageString"]);
}
return new List<int>();
}
set { this.ViewState["ImageString"] = value; }
}
And then I was hoping that from my aspx page I could add a line of code to add a value to the list like so:
this.LightBoxControl.ImageString.Add(value);
The problem I'm getting is that the the value is actually never added to the list. The count is always zero.
I'm sure its just that I've set the property up wrong, but I'm not sure how to right it..
Any help would be greatly appreciated.
Thanks
Al
Your getter is wrong. This is the correct variant:
get {
if (this.ViewState["ImageString"] == null) {
this.ViewState["ImageString"] = new List<int>();
}
return (List<int>)(this.ViewState["ImageString"]);
}
Here you first check whether there is something you need in ViewState already, and if there is no, you add it there. Then you return the item from ViewState - it is guaranteed to be there.
Your solution was bad because it did not place new List<int>() into the ViewState

determine a textbox's previous value in its lost focused event? WPF

I have a textbox and have an onlostfocus event on it.
Inside the lostfocus method, is there a way I can determine if the user has actually changed the value in it?
i.e how do i get hold of any previous value in it?
Thanks
As with just about everything else in WPF, this is easier if you use data binding.
Bind the text box to a class property. By default, bindings update the source when the bound control loses focus, so you don't have to muck around with the LostFocus event. You then have access to both the new value and the value that the user entered in the property setter.
In the XAML it looks like this:
<TextBox Text="{Binding MyProperty, Mode=TwoWay}"/>
In the class it looks like this:
private string _MyProperty;
public string MyProperty
{
get { return _MyProperty; }
set
{
// at this point, value contains what the user just typed, and
// _MyProperty contains the property's previous value.
if (value != _MyProperty)
{
_MyProperty = value;
// assuming you've implemented INotifyPropertyChanged in the usual way...
OnPropertyChanged("MyProperty");
}
}
What comes to mind for me is a two stage approach. Handle the TextChanged event on the textbox and flag it. Then when the textbox OnLostFocus occurs you can simply check your flag to see if the text has been changed.
Here is a code snippet on how you could handle the tracking.
public class MyView
{
private bool _textChanged = false;
private String _oldValue = String.Empty;
TextChanged( ... )
{
// The user modifed the text, set our flag
_textChanged = true;
}
OnLostFocus( ... )
{
// Has the text changed?
if( _textChanged )
{
// Do work with _oldValue and the
// current value of the textbox
// Finished work save the new value as old
_oldValue = myTextBox.Text;
// Reset changed flag
_textChanged = false;
}
}
}
Store the original value somewhere. You could write a common component to store the value when it gets focus and compare the value when it loses focus. I've done this in ASP.NET and it works quite well.
Another way to solve this by databinding:
Bind the TextBox.Text to the property, that holds the inital value, but use a binding with
UpdateSourceTrigger=Explicit
Then, when the textbox loses focus, you can check the binding if source and target values differ, using this code snippet and evaluating the resulting BindingExpression:
BindingExpression be = tb.GetBindingExpression(TextBox.TextProperty);
Some more code can be found here:
http://bea.stollnitz.com/blog/?p=41

Categories