我为作者的个人信息设计了一个表格。 所有文本框都获得值,并且在回发后保留其值(当我单击“保存”按钮时) 但是文本框和下拉列表没有获得值,并且在回发后它们的值被清除。 HTML代码:
<table width="100%" cellpadding="0" cellspacing="0">
<tr>
<td height="35" colspan="2" bgcolor="#E1DED9" style="border-left-style: 3">
<strong> Personal Information</strong></td>
</tr>
<tr>
<td height="25" bgcolor="#F3F3E9" class="style13" style="padding-left: 5px" >
Title:</td>
<td height="20" bgcolor="#F3F3E9" style="padding-top: 3px; padding-bottom: 2px">
<asp:TextBox ID="txtTitle" runat="server" BorderColor="#DFDFC6"
BorderStyle="Solid" Height="18px" Width="50px"></asp:TextBox>
</td>
</tr>
<tr>
<td bgcolor="#F3F3E9" class="style13" height="25" style="padding-left: 5px" >
First Name:</td>
<td bgcolor="#F3F3E9" style="padding-top: 3px; padding-bottom: 2px">
<asp:TextBox ID="txtFirstName" runat="server" BorderColor="#DFDFC6"
BorderStyle="Solid" Height="18px"></asp:TextBox>
</td>
</tr>
<tr>
<td height="25" bgcolor="#F3F3E9" class="style13" style="padding-left: 5px" >
Middle Name:</td>
<td height="20" bgcolor="#F3F3E9" style="padding-top: 3px; padding-bottom: 2px">
<asp:TextBox ID="txtMidName" runat="server" BorderColor="#DFDFC6"
BorderStyle="Solid" Height="18px"></asp:TextBox>
</td>
</tr>
<tr>
<td height="25" bgcolor="#F3F3E9" class="style13" style="padding-left: 5px" >
Last Name:</td>
<td height="20" bgcolor="#F3F3E9" style="padding-top: 3px; padding-bottom: 2px">
<asp:TextBox ID="txtLastName" runat="server" BorderColor="#DFDFC6"
BorderStyle="Solid" Height="18px"></asp:TextBox>
</td>
</tr>
<tr>
<td height="25" bgcolor="#F3F3E9" class="style13" style="padding-left: 5px" >
Gender:</td>
<td height="20" bgcolor="#F3F3E9" style="padding-top: 3px; padding-bottom: 2px">
<asp:RadioButton ID="rdbMale" runat="server" Text="Male" GroupName="Gender" />
<asp:RadioButton ID="rdbFemale" runat="server" Text="Female"
GroupName="Gender" />
</td>
</tr>
<tr>
<td height="25" bgcolor="#F3F3E9" class="style13" style="padding-left: 5px" >
Phone:</td>
<td height="20" bgcolor="#F3F3E9" style="padding-top: 3px; padding-bottom: 2px">
<asp:TextBox ID="txtPhone" runat="server" BorderColor="#DFDFC6"
BorderStyle="Solid" Height="18px"></asp:TextBox>
</td>
</tr>
<tr>
<td height="25" bgcolor="#F3F3E9" class="style13" style="padding-left: 5px" >
Degree:</td>
<td height="20" bgcolor="#F3F3E9" style="padding-top: 3px; padding-bottom: 2px">
<asp:DropDownList ID="ddlDegree" runat="server">
<asp:ListItem>BS</asp:ListItem>
<asp:ListItem>MSc</asp:ListItem>
<asp:ListItem>PhD</asp:ListItem>
<asp:ListItem>Prof</asp:ListItem>
</asp:DropDownList>
<asp:CheckBox ID="ckbIsStudent" runat="server" Text="Student" />
</td>
</tr>
<tr>
<td height="25" bgcolor="#F3F3E9" class="style14" >
</td>
<td bgcolor="#F3F3E9" class="style15"
style="padding-top: 3px; padding-bottom: 2px">
</td>
</tr></table>
代码背后:
protected void btnFinish_Click(object sender, EventArgs e)
{
MembershipUser user = Membership.GetUser();
ProfileCommon userProfile = Profile.GetProfile(user.UserName);
userProfile.Title = txtTitle.Text;
userProfile.FirstName = txtFirstName.Text;
userProfile.MiddleName = txtMidName.Text;
userProfile.LastName = txtLastName.Text;
if (rdbMale.Checked==true)
userProfile.Gender = "Male";
else if (rdbFemale.Checked==true)
userProfile.Gender = "Female";
userProfile.Phone = txtPhone.Text;
userProfile.Degree = ddlDegree.SelectedValue.ToString();
if (ckbIsStudent.Checked)
userProfile.IsStudent = true;
else
userProfile.IsStudent = false;
userProfile.Save();
}
答案 0 :(得分:-1)
我在Page_Load()中编写以下代码,问题解决了。
if (!Page.IsPostBack)
{
txtTitle.Text = Profile.Title;
txtFirstName.Text = Profile.FirstName;
txtMidName.Text = Profile.MiddleName;
txtLastName.Text = Profile.LastName;
if (Profile.Gender == "Male")
rdbMale.Checked = true;
else if (Profile.Gender == "Female")
rdbFemale.Checked = true;
txtPhone.Text = Profile.Phone;
ddlDegree.Text = Profile.Degree;
}
if(!Page.IsPostBack)是我的答案! 谢谢极客...