pass variable to the user control - asp.net

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; }

Related

Getting selected value from Picker in Xamarin

I am getting data from a web service and I am loading it in Picker. Now I want to call a new web service to get some data related to selected item. But I am not getting that selected item.
I am using below class model to get data from web service and loading it in Picker.
public class ModelGetEmployeeList
{
public string ServiceStatus { get; set; }
public List<EmployeeList> EmpList { get; set; }
}
public class EmployeeList
{
public string uid { get; set; }
public string fname { get; set; }
public string lname { get; set; }
}
This is how I loaded data in Picker:
if (response.IsSuccessStatusCode)
{
var content = await response.Content.ReadAsStringAsync ();
var Items = JsonConvert.DeserializeObject <ModelGetEmployeeList> (content);
foreach(EmployeeList emp in Items.EmpList)
{
pickerEmployee.Items.Add(emp.uid.ToString()+"-"+emp.fname.ToString()+" "+emp.lname.ToString());
}
}
Now I am implementing SelectedIndexChanged event like this:
public void PickerEmployee_SelectedIndexChanged(object sender, SelectedItemChangedEventArgs e)
{
if (pickerEmployee.SelectedIndex == -1)
{
//Message
}
else
{
var item = sender as EmployeeList;
var selectedItem = item.uid;
DisplayAlert (selectedItem.ToString (), "OK", "OK");
}
}
But its giving me an error that above method has wrong signature.
You can take sellected value with this:
string selectedEmployee = string.Empty;
selectedEmployee = pickerEmployee.Items[pickerEmployee.SelectedIndex];
According to the Xamarin.Forms Picker documentation SelectedIndexChanged event is expecting delegate which matches EventHandler delegate (EventHandler documentation)
So, you have to change signature of your method :
public void PickerEmployee_SelectedIndexChanged(object sender, EventArgs e)
{
...
}
Your signature is wrong.
Also the following code is wrong:
var item = sender as EmployeeList;
var selectedItem = item.uid;
Please find the corrected version below :
public void PickerEmployee_SelectedIndexChanged(object sender, EventArgs e)
{
if (pickerEmployee.SelectedIndex == -1)
{
//Message
}
else
{
var selectedItem = PickerEmployee[SelectedIndex];
DisplayAlert (selectedItem, "OK", "OK");
}
}
The Xamarin Forms picker will get you only the string which was added to the list and not the object.
If you need the object either you can use the selectedIdex on your orginal lsit to get the object as :
var selectedEmp = Items.EmpList[SelectedIndex];
Or you can use a Bindable Picker.
public void PickerEmployee_SelectedIndexChanged(object sender, EventArgs e)
{
if (pickerEmployee.SelectedIndex == -1)
{
//Message
}
else
{
var selectedItem = (EmployeeList)PickerEmployee.SelectedItem;
DisplayAlert (selectedItem.fname, "OK", "OK");
}
}
public void PickerEmployee_SelectedIndexChanged(object sender, SelectedItemChangedEventArgs e)
{
if (pickerEmployee.SelectedIndex == -1)
{
//Message
}
else
{
var item = sender as Picker;
var selectedItem = item.SelectedItem as EmployeeList;
var uid =selectedItem.uid;
DisplayAlert (uid .ToString (), "OK", "OK");
}
}
The Items collection is a list of strings so you can get the currently selected value using SelectedIndex
var selectedValue = picker.Items [picker.SelectedIndex];
If you are using binding then yes, the exposed property is the SelectedIndex.
For more info click here
//How to get value of picker in Xamarin forms
//We are getting Text and Value from API
//xaml page
<controls:BorderlessPicker
x:Name="Pickdoctype"
ItemDisplayBinding="{Binding text}"
SelectedIndexChanged="Pickdoctype_SelectedIndexChanged"
HorizontalOptions="FillAndExpand"
Title="Enter Document Type"
FontSize="20"
TextColor="Gray">
</controls:BorderlessPicker>
// xaml.cs page
private void Pickdoctype_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
DocumentTypeModel selectedItem = (DocumentTypeModel)Pickdoctype.SelectedItem;
updatePickerValue = selectedItem.value;
}
catch (Exception ex)
{
}
}
// class
public class DocumentTypeModel
{
public string text { get; set; }
public string value { get; set; }
}

how can i call method in web page from class library

I have a page sample.aspx. In this file page I use ajax to call a handler which in class library. I do some job in class lib. After doing this, I want to call method from sample.cs.
For example:
addstudent.aspx page calls ajax request to addstudent.ashx (this code is in handler.cs). I am adding student with params, and after adding students I want to call a method which is in addstudent.cs for example public afteraddingstudent(){}
How can I call after adding student in class lib?
Make your afteraddingstudent method as public static and after that you can call this method in handler as
public static void afteraddingstudent()
{
}
Handler,
WebApplication1.addstudent.afteraddingstudent();
//in my Handler class lib,it is separated project
public class gnric : IHttpHandler, IRequiresSessionState
{
public enum DBAction
{
INSERT = 0,
UPDATE = 1,
DELETE = 2
}
public class DatabaseEventArgs : EventArgs
{
private DBAction prmAction;
private int prmID;
public DatabaseEventArgs(DBAction Action, int ID)
{
this.prmAction = Action;
this.prmID = ID;
}
public int ID
{
get { return prmID; }
}
public DBAction Action
{
get { return prmAction; }
}
}
public delegate void DBChangeEventHandler(object sender, DatabaseEventArgs e);
[Category("DB"), Description(" when a record is Inserted, Updated, or Deleted.")]
public event DBChangeEventHandler DBChange;
protected virtual void OnDBChange(DatabaseEventArgs e)
{
if (DBChange != null)
DBChange(this, e);
}
public void ProcessRequest(HttpContext context)
{
//DO SOME DBJOBS
OnDBChange(new DatabaseEventArgs(DBAction.UPDATE, 22));
//in my apsx file
protected void LNA_DBChange(object sender,HandlerClass.gnric.DatabaseEventArgs e) //how can I bind this method
{
EasySQL DBA = new EasySQL();
DBA.cmd("INSERT INTO TEST ALAN VALUES(3)");
}

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;
}
}

MVMLight Messaging and Silverlight

I am trying to get a sample to work using MVVM Light and the Messaging Class. In the sample, I have a test project created from the MVVM Template for Silveright 4. I have added a button on the main page. When the button is clicked, it updates a property on the ViewModel. When the property is updated, I want to show a messagebox with the new value.
The key line of code is:
Messenger.Default.Register(this, new Action(ShowMessage));
I can get this to work in WPF, but not silverlight. It should call ShowMessage with the string parameter when the property changes, but it does not. If I use:
Messenger.Default.Send("Hello MVVM");
This works and the string is sent as a message to ShowMessage.
However, the message does not get sent if the property changes, even though the property was created with the MVVMINPC snippet and has the following line:
RaisePropertyChanged(MyPropertyPropertyName, oldValue, value, true);
This should have the same effect as Messager.Default.Send but it seems to be ignored. ThePropertyChangedEvent is indeed raised, but the messanger part seems to be disconnected.
Am I doing something wrong? Here is the full MainViewModel:
public class MainViewModel : ViewModelBase
{
public RelayCommand MyRelayCommand { get; set; }
public const string MyPropertyPropertyName = "MyProperty";
private string _myProperty = "test";
public string MyProperty
{
get
{
return _myProperty;
}
set
{
if (_myProperty == value)
{
return;
}
var oldValue = _myProperty;
_myProperty = value;
RaisePropertyChanged(MyPropertyPropertyName, oldValue, value, true);
}
}
public void DoSomething()
{
//Messenger.Default.Send("Hello MVVM"); //Works
this.MyProperty = "Hello World"; //Doesn't work.
}
public void ShowMessage(string message)
{
MessageBox.Show(message);
}
/// <summary>
/// Initializes a new instance of the MainViewModel class.
/// </summary>
public MainViewModel()
{
Messenger.Default.Register(this, new Action<string>(ShowMessage));
MyRelayCommand = new RelayCommand(new Action(DoSomething));
this.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(MainViewModel_PropertyChanged);
}
void MainViewModel_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
MessageBox.Show(e.PropertyName);
}
}public class MainViewModel : ViewModelBase
{
public RelayCommand MyRelayCommand { get; set; }
public const string MyPropertyPropertyName = "MyProperty";
private string _myProperty = "test";
public string MyProperty
{
get
{
return _myProperty;
}
set
{
if (_myProperty == value)
{
return;
}
var oldValue = _myProperty;
_myProperty = value;
RaisePropertyChanged(MyPropertyPropertyName, oldValue, value, true);
}
}
public void DoSomething()
{
//Messenger.Default.Send("Hello MVVM"); //Works
this.MyProperty = "Hello World"; //Doesn't work.
}
public void ShowMessage(string message)
{
MessageBox.Show(message);
}
/// <summary>
/// Initializes a new instance of the MainViewModel class.
/// </summary>
public MainViewModel()
{
Messenger.Default.Register(this, new Action<string>(ShowMessage));
MyRelayCommand = new RelayCommand(new Action(DoSomething));
this.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(MainViewModel_PropertyChanged);
}
void MainViewModel_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
MessageBox.Show(e.PropertyName);
}
}v
OK, I found that the Register line should look like this:
Messenger.Default.Register(this, new Action<PropertyChangedMessage<string>>(ShowMessage));
The point being there are different types of messages, and you have to register the PropertyChangedMessage type to recieve property changed messages.
Then also, the Action that recieves the message needs to take the correct parameter, like this:
public void ShowMessage(PropertyChangedMessage<string> e)
{
MessageBox.Show(e.NewValue.ToString());
}

ASP:RadioButtonList Databind Custom Object

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;
}

Resources