[英]Problemswith DropDownList in customized ASP.NET CreateUserWizard
我在Web應用程序中使用了自定義的ASP.NET CreateUserWizard
。
在這里,我使用dropdownlist
填充了用戶注冊時的國家/地區。 在page load
,按預期填充國家/地區。
var query = GetNationality();
var national = (DropDownList)RegisterUser.CreateUserStep.ContentTemplateContainer.FindControl("Nationality");
national.DataSource = query;
national.DataTextField = "CountryName";
national.DataValueField = "Id";
national.DataBind();
var item = new ListItem("Select Country", "");
national.Items.Insert(0, item);
但是當我嘗試從OnCreatedUser
事件中的dropdownlist
獲取值時,它產生了一條錯誤消息
System.FormatException:輸入字符串的格式不正確
我在做的是OnCreatedUser
是
var national = (DropDownList)RegisterUser.CreateUserStep.ContentTemplateContainer.FindControl("Nationality");
var nationality = Convert.ToInt32(national.SelectedValue); <<-(where the error is)
該頁面的完整代碼如下
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
FillDropdown();
}
}
protected void RegisterUser_CreatedUser(object sender, EventArgs e)
{
var newUser = Membership.GetUser(RegisterUser.UserName);
var newUserId = (Guid)newUser.ProviderUserKey;
var name1 = (TextBox)RegisterUser.CreateUserStep.ContentTemplateContainer.FindControl("fname");
var name2 = (TextBox)RegisterUser.CreateUserStep.ContentTemplateContainer.FindControl("lname");
var comp = (TextBox)RegisterUser.CreateUserStep.ContentTemplateContainer.FindControl("Company");
var post = (TextBox)RegisterUser.CreateUserStep.ContentTemplateContainer.FindControl("Position");
var birth = (TextBox)RegisterUser.CreateUserStep.ContentTemplateContainer.FindControl("Bday");
var mob = (TextBox)RegisterUser.CreateUserStep.ContentTemplateContainer.FindControl("Mobile");
var aphone = (TextBox)RegisterUser.CreateUserStep.ContentTemplateContainer.FindControl("altPhone");
var aemail = (TextBox)RegisterUser.CreateUserStep.ContentTemplateContainer.FindControl("altEmail");
var national = (DropDownList)RegisterUser.CreateUserStep.ContentTemplateContainer.FindControl("Nationality");
var news = (CheckBox)RegisterUser.CreateUserStep.ContentTemplateContainer.FindControl("Newsletter");
var title = tit.Text.Trim();
var nationality = national.Text;
var preferred = method.Text.Trim();
var newsleter = news.Checked;
FormsAuthentication.SetAuthCookie(RegisterUser.UserName, false /* createPersistentCookie */);
var continueUrl = RegisterUser.ContinueDestinationPageUrl;
if (String.IsNullOrEmpty(continueUrl))
{
continueUrl = "~/";
}
Response.Redirect(continueUrl);
}
public void FillDropdown()
{
var query = GetNationality();
var national = (DropDownList)RegisterUser.CreateUserStep.ContentTemplateContainer.FindControl("Nationality");
national.DataSource = query;
national.DataTextField = "CountryName";
national.DataValueField = "Id";
national.DataBind();
var item = new ListItem("Select Country", "");
national.Items.Insert(0, item);
}
}
任何想法將不勝感激。 謝謝
能否將填充DropDownList
的代碼放在!IsPostBack
下的Page_Load
方法中?
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
var query = GetNationality();
var national = (DropDownList)RegisterUser.CreateUserStep.ContentTemplateContainer.FindControl("Nationality");
national.DataSource = query;
national.DataTextField = "CountryName";
national.DataValueField = "Id";
national.DataBind();
var item = new ListItem("Select Country", "");
national.Items.Insert(0, item);
}
}
可能是當您回發時,您的DropDownList受到重新綁定,因此您始終會獲得第一項,並且嘗試將空字符串轉換為會給您錯誤消息的int。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.