Наверное, это что-то маленькое, я не вижу... У меня есть UserControls_LoginPopUp с одним из свойств:
public string urlForRedirecting {get; set;}
Этот пользовательский элемент управления содержит modalpopupextender и метод для входа в систему:
public void Login_Click(object sender, EventArgs e)
{
string user = txtUser.Text;
string passwordMD5 = UtilsStatic.GetMD5Hash(txtPassword.Text);
int id = checkUserAtLogin(user, passwordMD5);
if (id != -1)
{
//MySession.Current.userId = id;
lblStatus.Text = "Autentificare reusita!";
loginPopUp.Hide();
//The user will be redirected
Response.Redirect(this.urlForRedirecting);
this.urlForRedirecting = "";
}
else
{
MySession.Current.userId = -1;
lblStatus.Text = "Autentificare esuata!";
loginPopUp.Show();
}
}
Теперь, с другой страницы, пользователь нажимает на ссылку, а метод запускается там, где показан модальный расширитель, чтобы он мог войти в систему. Обратите внимание, что я заполняю свойство urlForRedirecting:
public void redirectToWishList(object sender, EventArgs e)
{
if (UtilsStatic.getUserLoggedInId() == -1)
{
ASP.usercontrols_loginpopup_ascx loginUserControl = (ASP.usercontrols_loginpopup_ascx)UtilsStatic.FindControlRecursive(Page, "loginPopUp");
ModalPopupExtender modal = (ModalPopupExtender)loginUserControl.FindControl("loginPopUp");
modal.Show();
//put the link to which the redirect will be done if the user will succesfully login in
loginUserControl.urlForRedirecting = getWishListLink();
}
else
Response.Redirect(getWishListLink());
}
Проблема заключается в том, что после успешного входа в logr пользователя url имеет значение null (но я уже завершил это!)
Response.Redirect(this.urlForRedirecting);
Вы понимаете, почему?
Вы всегда должны обрезать свои значения для имени пользователя/паролей, чтобы удалить пустое пространство.
string user = txtUser.Text.Trim();
string passwordMD5 = UtilsStatic.GetMD5Hash(txtPassword.Text.Trim());
Я считаю, что GetMD5Hash будет создавать разные значения, если у вас есть значение "Значение" и "Значение".
Использовать ViewState между postbacks, иначе значение свойства будет потеряно.
public string UrlForRedirecting
{
get
{
object urlForRedirecting = ViewState["UrlForRedirecting"];
if (urlForRedirecting != null)
{
return urlForRedirecting as string;
}
return string.Empty;
}
set
{
ViewState["UrlForRedirecting"] = value;
}
}
Когда вы нажмете строку кода:
modal.Show();
Будет отображаться ваш пользовательский элемент управления, после чего вы установите значение после этого, чтобы оно не было установлено при открытии формы.
Попробуйте переместить код так, чтобы он выглядел следующим образом:
ASP.usercontrols_loginpopup_ascx loginUserControl = (ASP.usercontrols_loginpopup_ascx)UtilsStatic.FindControlRecursive(Page, "loginPopUp");
loginUserControl.urlForRedirecting = getWishListLink();
ModalPopupExtender modal = (ModalPopupExtender)loginUserControl.FindControl("loginPopUp");
modal.Show();
Это установит свойство urlForRedirecting, прежде чем вы откроете форму, что означает, что вы можете получить к ней доступ после ее открытия.