ASP.NET Repeater - 如何刷新按钮提交上的数据
我看到过类似的帖子,但有些不太清楚。
这是中继器头:
<asp:Repeater ID="rptGetAll" OnItemCommand="Buttons_OnItemCommand" runat="server" OnLoad="rptGetAll_Load">
我有一个按钮:
<asp:Button ID="Submit" runat="server" OnClick="Submit_Click" Text="Save" />
我在数据库中有代码隐藏插入/更新数据,然后我设置:
using (SqlConnection con = new SqlConnection(conString))
{
con.Open();
using (SqlCommand cmd = new SqlCommand("administratorUpdate", con))
{
cmd.Parameters.Add("@originalID", SqlDbType.VarChar).Value = hfID.Value;
cmd.Parameters.Add("@firstName", SqlDbType.VarChar).Value = MyGlobals.SafeSqlLiteral(txtFirstName.Text, 2);
cmd.Parameters.Add("@lastName", SqlDbType.VarChar).Value = MyGlobals.SafeSqlLiteral(txtLastName.Text, 2);
cmd.Parameters.Add("@userName", SqlDbType.VarChar).Value = MyGlobals.SafeSqlLiteral(txtUserName.Text, 1);
cmd.Parameters.Add("@emailAddress", SqlDbType.VarChar).Value = MyGlobals.SafeSqlLiteral(txtEmailAddress.Text, 2);
cmd.Parameters.Add("@password", SqlDbType.VarChar).Value = MyGlobals.SafeSqlLiteral(txtPassword.Text, 1);
cmd.Parameters.Add("@isActive", SqlDbType.VarChar).Value = cbIsActive.Checked;
cmd.CommandType = CommandType.StoredProcedure;
cmd.ExecuteNonQuery();
}
con.Close();
}
rptGetAll.DataSource = dt;
rptGetAll.DataBind();
//Output Success Message
Label lblErrorMessage = (Label)Master.FindControl("lblErrorMessage");
new MyGlobals().DisplayUserMessage("success", "Administrator Updated!", lblErrorMessage);
AdminForm.Visible = false;
但是当页面完成时,数据不会更新。我缺少什么?
更新: 这是 Repeater_Load:
protected void rptGetAll_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlCommand cmd = new SqlCommand("administratorGetAll", con))
{
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
{
da.Fill(dt);
}
}
}
rptGetAll.DataSource = dt;
rptGetAll.DataBind();
}
I saw similar posts but some weren't clear.
Here is the repeater head:
<asp:Repeater ID="rptGetAll" OnItemCommand="Buttons_OnItemCommand" runat="server" OnLoad="rptGetAll_Load">
I have a button:
<asp:Button ID="Submit" runat="server" OnClick="Submit_Click" Text="Save" />
and I have the code-behind insert/update data in the database and then I set:
using (SqlConnection con = new SqlConnection(conString))
{
con.Open();
using (SqlCommand cmd = new SqlCommand("administratorUpdate", con))
{
cmd.Parameters.Add("@originalID", SqlDbType.VarChar).Value = hfID.Value;
cmd.Parameters.Add("@firstName", SqlDbType.VarChar).Value = MyGlobals.SafeSqlLiteral(txtFirstName.Text, 2);
cmd.Parameters.Add("@lastName", SqlDbType.VarChar).Value = MyGlobals.SafeSqlLiteral(txtLastName.Text, 2);
cmd.Parameters.Add("@userName", SqlDbType.VarChar).Value = MyGlobals.SafeSqlLiteral(txtUserName.Text, 1);
cmd.Parameters.Add("@emailAddress", SqlDbType.VarChar).Value = MyGlobals.SafeSqlLiteral(txtEmailAddress.Text, 2);
cmd.Parameters.Add("@password", SqlDbType.VarChar).Value = MyGlobals.SafeSqlLiteral(txtPassword.Text, 1);
cmd.Parameters.Add("@isActive", SqlDbType.VarChar).Value = cbIsActive.Checked;
cmd.CommandType = CommandType.StoredProcedure;
cmd.ExecuteNonQuery();
}
con.Close();
}
rptGetAll.DataSource = dt;
rptGetAll.DataBind();
//Output Success Message
Label lblErrorMessage = (Label)Master.FindControl("lblErrorMessage");
new MyGlobals().DisplayUserMessage("success", "Administrator Updated!", lblErrorMessage);
AdminForm.Visible = false;
But when the page is done the data isn't updated. What am I missing?
UPDATE:
This is the Repeater_Load:
protected void rptGetAll_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlCommand cmd = new SqlCommand("administratorGetAll", con))
{
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
{
da.Fill(dt);
}
}
}
rptGetAll.DataSource = dt;
rptGetAll.DataBind();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您正在将中继器与先前填充的数据表 dt 绑定。使用最新数据重新填充数据表,然后单击“保存”按钮将其绑定到转发器。
You are binding the repeater with previous filled datatable dt. Refill the datatable with the latest data and bind it to repeater on the Save button click.
添加这样的参数值时尝试不同的方法
try different approach when adding your parameter values like this