Witam,
W mojej aplikacji ASP.NET ładuję dynamicznie na pewnej stronie kontrolki. Zależnie od wyboru przycisku na pewnym panelu ładowane są odpowiednie kontrolki. Robię to tak:
public partial class UserPanelLogin : System.Web.UI.UserControl
{
public Panel PanelControl { get; set; }
public UserControl ViewUCControl {get; set;}
public QuestionnaireLib.User UserSelected { get; set; }
private QuestionnaireLib.UserControl userCtrl = new QuestionnaireLib.UserControl();
protected void Page_Load(object sender, EventArgs e)
{
if (userCtrl.IsLoggedUser() == false)
Response.Redirect("Default.aspx");
this.lblLogin.Text = "Login: " + UserSelected.Login;
this.lblGroup.Text = "Grupa: " + UserSelected.Group.NameGroup;
}
private void LoadControlForPanel<T>(string nameFile)
where T : UserControl
{
this.ViewUCControl = Page.LoadControl(nameFile) as T;
this.PanelControl.Controls.Clear();
this.PanelControl.Controls.Add(this.ViewUCControl);
}
public void LoadDefaultControl()
{
this.LoadControlForPanel<Controls_DataUserControl>("DataUserControl.ascx");
((Controls_DataUserControl)this.ViewUCControl).ViewUser(this.UserSelected);
}
protected void btnLogout_Click(object sender, EventArgs e)
{
QuestionnaireLib.UserControl.Logout();
Response.Redirect("Default.aspx");
}
protected void btnSimpleDataUser_Click(object sender, EventArgs e)
{
this.LoadDefaultControl();
}
protected void btnPassChange_Click(object sender, EventArgs e)
{
this.LoadControlForPanel<ChangePasswordControl>("ChangePasswordControl.ascx");
}
protected void btnLoginChange_Click(object sender, EventArgs e)
{
this.LoadControlForPanel<ChangeLoginControl>("ChangeLoginControl.ascx");
}
protected void btnDeleteAccount_Click(object sender, EventArgs e)
{
this.LoadControlForPanel<QuestionYesNoControl>("QuestionYesNoControl.ascx");
((QuestionYesNoControl)this.ViewUCControl).SetQuestion("Czy chcesz usunąć konto?");
}
}
Kontrolki te ładują się poprawnie, ale niestety za pierwszym razem kontrolka gdy się załaduje nie wykonuje żadnej akcji po kliknięciu na niej. No chcemy wyświetlić / mienić dane publiczne danego usera to wybieramy na panelu przycisk, który wywołuje metodę: LoadDefaultControl()
Metoda dodaje kontrolkę:
public partial class Controls_DataUserControl : System.Web.UI.UserControl
{
private QuestionnaireLib.User userSelected = null;
protected void Page_Load(object sender, EventArgs e)
{
}
public void ViewUser(QuestionnaireLib.User userSelected)
{
this.userSelected = userSelected;
this.tbName.Text = userSelected.Name;
this.tbLastName.Text = userSelected.LastName;
this.tbEmail.Text = userSelected.Email;
}
protected void btnSave_Click(object sender, EventArgs e)
{
this.lblEditStatus.Text = string.Empty;
QuestionnaireLib.UserControl ctrl = new QuestionnaireLib.UserControl();
this.userSelected.Name = this.tbName.Text;
this.userSelected.LastName = this.tbLastName.Text;
this.userSelected.Email = this.tbEmail.Text;
if (ctrl.EditUser(this.userSelected) == false)
this.lblEditStatus.Text = "Edycja nie powiodła się.";
}
Niestety za pierwszym razem przycisk naciśnięty nawet nie wchodzi w tryb debugu. Następuje refresh strony i dopiero wtedy zaczyna działać przycisk. Dlaczego tak się dzieje i jak to naprawić?
Pozdrawiam,