更新表单不更新详细信息
本文关键字:更新 详细信息 表单 | 更新日期: 2023-09-27 18:04:22
我创建了一个简单的表单来显示用户详细信息,包含以下字段:
First Name
Surname
Team Name
Team Stadium
Email
Receive Reminder
Receive Summary
检索登录用户的详细信息,并允许对字段进行编辑以更新。但是,当更新按钮的on-click事件被触发时,字段将恢复到它们的原始值。
下面是我用c#写的代码
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection(WebConfigurationManager.ConnectionStrings["RaiseFantasyLeagueConnectionString"].ConnectionString);
SqlCommand cmd = new SqlCommand("[dbo].[GetUsersProfile]", conn);
cmd.CommandType = CommandType.StoredProcedure;
string userId = Membership.GetUser().ProviderUserKey.ToString();
SqlParameter userIDParam = new SqlParameter("@userId", userId);
cmd.Parameters.Add(userIDParam);
conn.Open();
SqlDataReader dReader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
while (dReader.Read())
{
FirstName.Text = (dReader["ManagerFirstName"].ToString());
Surname.Text = (dReader["ManagerSurname"].ToString());
TeamName.Text = (dReader["TeamName"].ToString());
StadiumName.Text = (dReader["TeamStadium"].ToString());
Email.Text = (dReader["Email"].ToString());
Reminder.Checked = (bool)dReader["RecieveReminder"];
Prediction.Checked = (bool)dReader["RecieveSummary"];
}
dReader.Close();
conn.Close();
}
protected void UpdateDetailsButton_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection(WebConfigurationManager.ConnectionStrings["RaiseFantasyLeagueConnectionString"].ConnectionString);
SqlCommand cmd = new SqlCommand("[dbo].[UpdateUserDetails]", conn);
cmd.CommandType = CommandType.StoredProcedure;
string userId = Membership.GetUser().ProviderUserKey.ToString();
SqlParameter userIDParam = new SqlParameter("@userId", userId);
TextBox FirstNameTextBox = (TextBox)Master.FindControl("ContentPlaceHolderMain").FindControl("FirstName");
SqlParameter FirstNameParam = new SqlParameter("@ManagerFirstName", FirstNameTextBox.Text);
TextBox SurnameTextBox = (TextBox)Master.FindControl("ContentPlaceHolderMain").FindControl("Surname");
SqlParameter SurnameParam = new SqlParameter("@ManagerSurname", SurnameTextBox.Text);
TextBox TeamNameTextBox = (TextBox)Master.FindControl("ContentPlaceHolderMain").FindControl("TeamName");
SqlParameter TeamNameParam = new SqlParameter("@TeamName", TeamNameTextBox.Text);
TextBox TeamStadiumNameTextBox = (TextBox)Master.FindControl("ContentPlaceHolderMain").FindControl("StadiumName");
SqlParameter TeamStadiumNameParam = new SqlParameter("@TeamStadium", TeamStadiumNameTextBox.Text);
TextBox EmailTextBox = (TextBox)Master.FindControl("ContentPlaceHolderMain").FindControl("Email");
SqlParameter EmailParam = new SqlParameter("@Email", EmailTextBox.Text);
SqlParameter RecieveReminderParam = new SqlParameter("@RecieveReminder", 1);
SqlParameter RecieveSummaryParam = new SqlParameter("@RecieveSummary", 1);
cmd.Parameters.Add(userIDParam);
cmd.Parameters.Add(FirstNameParam);
cmd.Parameters.Add(SurnameParam);
cmd.Parameters.Add(TeamNameParam);
cmd.Parameters.Add(TeamStadiumNameParam);
cmd.Parameters.Add(EmailParam);
cmd.Parameters.Add(RecieveReminderParam);
cmd.Parameters.Add(RecieveSummaryParam);
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
}
对于ASP
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolderMain" runat="server">
<div class="row">
<div class="span12">
<h3>
My Profile</h3>
</div>
</div>
<div class="row">
<div class="span2">
<label class="control-label" for="firstNameTextBox">
First Name:</label>
</div>
<div class="span3">
<asp:TextBox ID="FirstName" runat="server"></asp:TextBox>
</div>
</div>
<div class="row">
<div class="span2">
<label class="control-label" for="surnameTextBox" >
Surname:</label>
</div>
<div class="span3">
<asp:TextBox ID="Surname" runat="server"></asp:TextBox>
</div>
</div>
<div class="row">
<div class="span2">
<label class="control-label" for="teamNameTextBox">
Team Name:</label>
</div>
<div class="span3">
<asp:TextBox ID="TeamName" runat="server"></asp:TextBox>
</div>
</div>
<div class="row">
<div class="span2">
<label class="control-label" for="stadiumNameTextBox">
Stadium Name:</label>
</div>
<div class="span3">
<asp:TextBox ID="StadiumName" runat="server"></asp:TextBox>
</div>
</div>
<div class="row">
<div class="span2">
<label class="control-label" for="emailTextBox">
Email Address:</label>
</div>
<div class="span3">
<asp:TextBox ID="Email" runat="server"></asp:TextBox>
</div>
</div>
<div class="row">
<div class="span2">
<label class="control-label" for="reminderCheckBox">
Recieve Reminder Email:</label>
</div>
<div class="span3">
<input type="checkbox" id="Reminder" runat="server"/>
</div>
</div>
<div class="row">
<div class="span2">
<label class="control-label" for="predictionCheckBox">
Recieve Prediction Email:</label>
</div>
<div class="span3">
<input type="checkbox" id="Prediction" runat="server"/>
</div>
</div>
<div class="row">
<div class="span5">
<asp:Button ID="UpdateDetailsButton"
CssClass="btn btn-primary rightAlignButton" runat="server" Text="Confirm"
onclick="UpdateDetailsButton_Click" />
</div>
</div>
我试过在SQL Management Studio中使用存储过程,它按预期更新,所以由于某种原因,字段在单击更新时没有读取更新的值。
任何建议将是伟大的,谢谢!
使用IsPostBack在Page_Load事件
if(!IsPostBack)
{
SqlConnection conn = new SqlConnection(WebConfigurationManager.ConnectionStrings["RaiseFantasyLeagueConnectionString"].ConnectionString);
SqlCommand cmd = new SqlCommand("[dbo].[GetUsersProfile]", conn);
cmd.CommandType = CommandType.StoredProcedure;
string userId = Membership.GetUser().ProviderUserKey.ToString();
SqlParameter userIDParam = new SqlParameter("@userId", userId);
cmd.Parameters.Add(userIDParam);
conn.Open();
SqlDataReader dReader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
while (dReader.Read())
{
FirstName.Text = (dReader["ManagerFirstName"].ToString());
Surname.Text = (dReader["ManagerSurname"].ToString());
TeamName.Text = (dReader["TeamName"].ToString());
StadiumName.Text = (dReader["TeamStadium"].ToString());
Email.Text = (dReader["Email"].ToString());
Reminder.Checked = (bool)dReader["RecieveReminder"];
Prediction.Checked = (bool)dReader["RecieveSummary"];
}
dReader.Close();
conn.Close();
}
您应该只在第一次从数据库加载数据,因此您必须检查它是否不是Postback。
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
LoadDataFromDatabase();
}
}
private void LoadDataFromDatabase()
{
SqlConnection conn = new SqlConnection(WebConfigurationManager.ConnectionStrings["RaiseFantasyLeagueConnectionString"].ConnectionString);
SqlCommand cmd = new SqlCommand("[dbo].[GetUsersProfile]", conn);
cmd.CommandType = CommandType.StoredProcedure;
string userId = Membership.GetUser().ProviderUserKey.ToString();
SqlParameter userIDParam = new SqlParameter("@userId", userId);
cmd.Parameters.Add(userIDParam);
conn.Open();
SqlDataReader dReader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
while (dReader.Read())
{
FirstName.Text = (dReader["ManagerFirstName"].ToString());
Surname.Text = (dReader["ManagerSurname"].ToString());
TeamName.Text = (dReader["TeamName"].ToString());
StadiumName.Text = (dReader["TeamStadium"].ToString());
Email.Text = (dReader["Email"].ToString());
Reminder.Checked = (bool)dReader["RecieveReminder"];
Prediction.Checked = (bool)dReader["RecieveSummary"];
}
dReader.Close();
conn.Close();
}