Hi
I have a small requirement I have two user controls in which there are two grids respectively,my requirement is In one usercontrol grid i will populate it with
Country names, on selection of any country name I should take corresponding ID and fetch corresponding states for it and populate it in Grid view of other usercontrol.
Here i am facing isssue of how to pass the control from one usercontrol to other usercontrol moreover It has to be doen asynchronoulsy I mean using AJAX
Please help in this issue if any one has any pointers.
Thanks in advance
Raj BandiPosted Jul 25, 2013, 3:19 AM
One way is using events ,
1) define custom event arguments to pass required data Country name
public class CountryEventArgs: EventArgs
{
public string Country {get; set;}
}
2) define a custom event in UserControl1 (in which countries data exist) ,
public class usercontrol1
{
public event EventHandler CountryChanged
public void OnCountryChanged(StateEventArgs e)
{
if(CountryChanged!= null)
{
CountryChanged(this, e);
}
}
}
3) Fire the above event whenever your country value changes. if you are using a dropdown, do it selectedindexchanged
OnCountryChanged(new CountryEventArgs{ Country = selectedstate });
4) Expose a method in usercontrol2(in which state data exists) that takes country as parameter and refresh data.
public void RefreshStates(string country)
{
//refresh your grid
}
5) in the host control/page(where the two controls reside), subscribe to usercontrol1 CountryChanged event with some code
usercontrol1.CountryChanged += Refresh_CountryData;
void Refresh_CountryData(object sender, CountryEventArgs args)
{
usercontrol2.RefreshStates(args.Country);
}
For ajax refreshing use updatepanel for each control or separately. If separately, user control2 should be refreshed only(change updatemode to conditional) when user control1 country value changes, to do that add asyncposttrigger with eventname CountryChanged)
Hope this gives an idea,
Cheers,
Raj