ASP:RadioButtonList Databind Custom Object - asp.net

Hey guys, I'm having a bit of trouble with my ASP:RadioButtonList, searched Google and SO, no luck, can you help me out?
I'm having trouble databinding. I've got a custom class that looks like this:
public class myClass{
public myInnerClass{
public int myID;
public String myTextField;
/* other fields*/
}
public List<myInnerClass> myList;
}
And I'm trying to bind a Generic List of it's inner class to a radiolist:
protected void Page_Load(object sender, EventArgs e){
myClass data = anotherClass.getData();
uxRadioList1.DataSource = data.myList;
uxRadioList1.DataTextField = "myTextField";
uxRadioList1.DataValueField = "myID";
uxRadioList1.DataBind();
}
But it just won't go. When I don't specify the DataTextField and DataValueField field it binds, but it displays 'myClass+myInnerClass' . How do I do this properly?

I think you can only bind to public properties, but not to fields. Try changing the fields of myInnerClass to properties:
public class myClass{
public myInnerClass{
public int myID { get; set; }
public String myTextField { get; set; }
/* other fields*/
}
public List<myInnerClass> myList;
}

Related

How to safe a ObservableCollection within a ObservableCollection in xamarin.forms with Sqlite.net

Last 3 days i tried to save my collection in my database.
I tried it in all sorts of ways... without success.
The last one i tried is to serialize my collection and save it to a byte[] everytime the collection changes.
And on appstart i tried to derserialize it in my collection.
I'm just trying to show the most important thing's.
I can save and reload this class without problems from my database.
[Serializable]
public class MainElement : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public static int count = 0;
[PrimaryKey]
public int Id { get; set; }
public string Name { get; set; }
public byte[] _elements;
private ObservableCollection<SecondElement> elements;
[OneToMany]
public ObservableCollection<SecondElement> Elements
{
get
{
return elements;
}
set
{
elements = value;
this.SerializeElementsFromMainElement();
//_elements = ExtensionAndDb.SerializeElementsFromMainElement(elements);
OnPropertyChanged();
}
}
public MainElement()
{
Id = count++;
Elements = new ObservableCollection<SecondElement>();
this.DeSerializeElementsFromMainElement();
//Elements = ExtensionAndDb.DeSerializeElementsFromMainElement(_elements);
}
public void OnPropertyChanged([CallerMemberName] string propertyname = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyname));
}
public void DeSerializeElementsFromMainElement()
{
BinaryFormatter bf = new BinaryFormatter();
MemoryStream stream = new MemoryStream(_elements);
using (stream)
{
Elements = bf.Deserialize(stream) as ObservableCollection<SecondElement>;
}
}
public void SerializeElementsFromMainElement()
{
BinaryFormatter bf = new BinaryFormatter();
MemoryStream stream = new MemoryStream();
using (stream)
{
bf.Serialize(stream, Elements);
_elements = stream.ToArray();
}
}
}
Every MainElement is stored in the MainList (ObservableCollection).
Everything is fine.
But i cant save or reload the public byte[] _elements; from/to the collection.
I know my code is not good, but if anyone has an idea or can show me how to save the public ObservableCollection<SecondElement> Elements with my class together in Sqlite, i would be really grateful.
I've been trying for 3 days
if you need more code, let me know.
Thank's in advance.
According to your description, you want to serialize and deserialie ObservableCollection, I suggest you ca use Newtonsoft.Json to do this.
Installing Newtonsoft.Json by Nuget package firstly.
For example, I do one esample:
public class person
{
public string username { get; set; }
public int age { get; set; }
}
public ObservableCollection<person> persons { get; set; }
private string list;
public Page13()
{
InitializeComponent();
persons = new ObservableCollection<person>()
{
new person(){username="cherry",age=12},
new person(){username="barry",age=14}
};
}
Btn1.clicked is to serialize data, Btn2.clicked is to deserialize data.
private void Btn1_Clicked(object sender, EventArgs e)
{
list = Newtonsoft.Json.JsonConvert.SerializeObject(persons);
}
private void Btn2_Clicked(object sender, EventArgs e)
{
var myValue = Newtonsoft.Json.JsonConvert.DeserializeObject<ObservableCollection<person>>(list) ;
}
If the Observablecollection changed, you can query this data by Id or other, then update this data.
Here is the article about Update, insert and save data in sqlite, you can take a look:
https://dzone.com/articles/register-and-login-using-sqlite-in-xamarinforms

The type 'xxxx' already contains a definion for 'xxx'

I am working in Asp.net 2010 C#
Inside my web page,
public partial class NewStore : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
public class Item()
{
public int ItemID;
public string ItemName;
}
}
The Problem is that When I rebuild the Project then an error occurs 'The type 'NewStore' already contains a definion for 'Item'.
I didnt find any other class Item inside the Page... Will u please help me..
Remove parenthesis after class name Item.
public class Item
{
public int ItemID;
public string ItemName;
}

pass variable to the user control

I have created an user control for cascading two dropdownlists "province" and "city". But everytime when I run it, the error message said "An unhandled exception of type 'System.StackOverflowException' occurred"
Here is my user control code
public partial class cascadingdropdownlist : System.Web.UI.UserControl
{
public string province_selectedvalue
{
set
{
string province_selectedvalue = value;
}
get
{
return city_selectedvalue;
}
}
public string city_selectedvalue
{
set
{
string city_selectedvalue = value;
}
get
{
return city_selectedvalue;
}
}
protected void Page_Load(object sender, EventArgs e)
{
string a = province_selectedvalue;
............
}
}
In the host page
<uc1:cascadingdropdownlist ID="province_city" runat="server" OnPreLoad="province_city_OnPreLoad"/>
The code behind is
protected void province_city_OnPreLoad(object sender, EventArgs e)
{
province_city.province_selectedvalue = myReader["Province/State"].ToString();
province_city.city_selectedvalue = myReader["City"].ToString();
}
The error happened in calling user control the province_selectedvalue.get method. why I don't understand why? Anyone can help me, thanks very much
The problem is in your property:
public string city_selectedvalue
{
set
{
string city_selectedvalue = value;
}
Calls the setter (or the getter!) on city_selectedvalue over and over again resulting in a stack overflow.
Replace your property with this
public string city_selectedvalue { get; set; }
I also thought you could do the same with province_selectedvalue but its getter refers to city_selectedvalue - is that correct?
You are getting that error because the code is in an infinitive loop. When you call province_selectedvalue.get you are calling city_selectedvalue get. And you have the same name for the property in the variable you think you are calling so it is calling the same over and over again.
Change you code to
public string city_selectedvalue { get; set; }
And
public string province_selectedvalue { get; set; }

How to bind data in gridview in asp.net without using properties

I have this code I want to bind data to grid view without using properties .
public class UserTerritory
{
public string TerrId;
public string TerrName;
public string AccAccessLevel;
public UserTerritory(string _TerrId, string _TerrName, string _AccAccessLevel)
{
this.TerrId = _TerrId;
this.TerrName = _TerrName;
this.AccAccessLevel= _AccAccessLevel;
}
}
protected void Page_Load(object sender, EventArgs e)
{
List<UserTerritory> ut = new List<UserTerritory>();
ut.Add(new UserTerritory("1", "x", "a"));
ut.Add(new UserTerritory("2", "y", "b"));
ut.Add(new UserTerritory("3", "z", "c"));
grdUserTerr.DataSource = ut;
grdUserTerr.DataBind();
}
When I execute the above code I get following Httpexception " The data source for GridView with id 'grdUserTerr' did not have any properties or attributes from which to generate columns. Ensure that your data source has content."
Can Somebody tell me what wrong I am doing ?
Thanks for your reply
The problem is that databinding uses DataBinder.Eval (or just Eval ) behind the scenes and it
looks only for properties. They aren't exactly the same as public members (fields). Properties are closer to methods than fields are.
So why don't you just use properties like this?
public string AccAccessLevel { get; set; }
public string TerrName { get; set; }
public string TerrId { get; set; }
there is problem some where in the property decalration section please change the properties as private memeber it will works for you.
public class UserTerritory
{
private string TerrId;
private string TerrName;
private string AccAccessLevel;
public UserTerritory(string _TerrId, string _TerrName, string _AccAccessLevel)
{
this.TerrId = _TerrId;
this.TerrName = _TerrName;
this.AccAccessLevel= _AccAccessLevel;
}
}

Create a log everytime When methods in an interface class are called

I want to update a log file(txt) everytime when methods in a an interface class are called?
Is there any way to do this other than writing code in every method to create log?
Here's my 30 mins. you'll have to implement the logging code somewhere so you have to create another abstraction for your code. thus an abstract class is needed. i think. this is very quick and dirty.
public interface IService<T>
{
List<T> GetAll();
bool Add(T obj);
}
then you'll need the abstract class where you'll need to implement your logging routine
public abstract class Service<T> : IService<T>
{
private void log()
{
/// TODO : do log routine here
}
public bool Add(T obj)
{
try
{
log();
return AddWithLogging(obj);
}
finally
{
log();
}
}
public List<T> GetAll()
{
try
{
log();
return GetAllWithLog();
}
finally
{
log();
}
}
protected abstract List<T> GetAllWithLog();
protected abstract bool AddWithLogging(T obj);
}
as for your concrete classes
public class EmployeeService : Service<Employee>
{
protected override List<Employee> GetAllWithLog()
{
return new List<Employee>() { new Employee() { Id = 0, Name = "test" } };
}
protected override bool AddWithLogging(Employee obj)
{
/// TODO : do add logic here
return true;
}
}
public class CompanyService : Service<Company>
{
protected override List<Company> GetAllWithLog()
{
return new List<Company>() { new Company() { Id = 0, Name = "test" } };
}
protected override bool AddWithLogging(Company obj)
{
/// TODO : do add logic here
return true;
}
}
public class Employee
{
public int Id {get;set;}
public string Name { get; set;}
}
public class Company
{
public int Id { get; set; }
public string Name { get; set; }
}
then on your implementation you can just..
static void Main(string[] args)
{
IService<Employee> employee = new EmployeeService();
List<Employee> employees = employee.GetAll();
foreach (var item in employees)
{
Console.WriteLine(item.Name);
}
IService<Company> company = new CompanyService();
List<Company> companies = company.GetAll();
foreach (var item in companies)
{
Console.WriteLine(item.Name);
}
Console.ReadLine();
}
hope this helps!
I think you would have to use Aspect Oriented Programming to achieve that. Read http://www.sharpcrafters.com/aop.net
I think you meant class (instead of interface)
Two options I can think of:
Implementing INotifyPropertyChanged which is in lines of writing code in every method
or
to adopt on of the AOP frameworks in the article http://www.codeproject.com/KB/cs/AOP_Frameworks_Rating.aspx if that is not a major leap

Resources