asp.net使用C#编辑模板
本文关键字:编辑 net 使用 asp | 更新日期: 2023-09-27 18:28:04
我正在使用存储过程编辑listview控件中的数据行。每一行都有"编辑"answers"删除"链接,因此当用户单击"编辑"时,该行应可编辑。我得到的错误是,当用户点击"编辑"时,行不会变成可编辑的。
使用的存储过程是:
CREATE PROCEDURE [dbo].[RegisterUpdate]
-- Add the parameters for the stored procedure here
@id int,
@fname nvarchar(50),
@lname nvarchar(50),
@company nvarchar(50),
@email nvarchar(250)
作为开始--添加SET NOCOUNT ON以防止额外的结果集--干扰SELECT语句。设置NOCOUNT;
-- Update statements for procedure here
Update dbo.register
SET fname = @fname,
lname = @lname,
company=@company,
email=@email
WHERE dbo.register.id=@id
END
转到
Asp.net代码为:
<EditItemTemplate>
<tr style="background-color: #E0FFFF; color: #333333;">
<td>
<asp:TextBox ID="fname" runat="server" Text='<%# Eval("fname") %>'>'></asp:TextBox>
</td>
<td>
<asp:TextBox ID="lname" runat="server" Text='<%# Eval("lname") %>'>'></asp:TextBox>
</td>
<td>
<asp:TextBox ID="company" runat="server" Text='<%# Eval("company") %>'>'></asp:TextBox>
</td>
<td>
<asp:TextBox ID="email" runat="server" Text='<%# Eval("email") %>'>'></asp:TextBox>
</td>
<td>
<asp:LinkButton ID="lnkSave" runat="server" Text="Save" CommandName="Save" />
</td>
<td>
<asp:LinkButton ID="lnkCancel" runat="server" Text="Cancel" CommandName="Cancel" />
</td>
</tr>
</EditItemTemplate>
用于在数据集中保存数据的C#代码是:
protected void ListViewDetails_ItemEditing(object sender, System.Web.UI.WebControls.ListViewEditEventArgs e)
{
con.Open();
SqlCommand cmd = new SqlCommand("dbo.RegisterUpdate", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@fname", con);
cmd.Parameters.AddWithValue("@lname", con);
cmd.Parameters.AddWithValue("@company", con);
cmd.Parameters.AddWithValue("@email", con);
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd;
}
让我们尝试一下它可能会对您有所帮助。
protected void AdminUserControl_ItemEditing(object sender, ListViewEditEventArgs e)
{
AdminUserControl.EditIndex = e.NewEditIndex;
UserBLL userbll = new UserBLL();
AdminUserControl.DataSource = userbll.GetAllUsers();
AdminUserControl.DataBind();
}
protected void AdminUserControl_ItemUpdating(object sender, ListViewUpdateEventArgs e)
{
UserBLL userbll = new UserBLL();
TextBox fname= (TextBox)e.NewValues["fname"];
TextBox lname= (TextBox)e.NewValues["lname"];
TextBox company= (TextBox)e.NewValues["company"];
TextBox email= (TextBox)e.NewValues["email"];
user = new User();
user.fname= Convert.ToInt16(fname.Text);
user.lname= lname.Text;
user.company= company.Text;
user.email= email.Text;
admin.UpdateUsers(user);
}
如果您使用的是网格视图,请参阅
您应该在ItemUpdating方法中绑定值。为ItemUpdating附加带有listview的处理程序。
当update命令被激发时,您应该使用该代码。
protected void ListViewDetails_ItemUpdating(object sender, ListViewUpdateEventArgs e)
{
TextBox fname = (TextBox)e.NewValues["fname"];
TextBox lname = (TextBox)e.NewValues["lname"];
TextBox company = (TextBox)e.NewValues["company"];
TextBox email = (TextBox)e.NewValues["email"];
con.Open();
SqlCommand cmd = new SqlCommand("dbo.RegisterUpdate", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@fname", Convert.ToInt16(fname.Text));
cmd.Parameters.AddWithValue("@lname", lname.Text);
cmd.Parameters.AddWithValue("@company", company.Text);
cmd.Parameters.AddWithValue("@email", email.Text);
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd;
cmd.ExecuteNonQuery();
}
希望这能帮助
试试这个代码
public void ListViewDetails_ItemEditing(object sender, System.Web.UI.WebControls.ListViewEditEventArgs e)
{
string connectionString = "??";
using (SqlConnection sqlConn = new SqlConnection(connectionString)) {
using (SqlCommand cmd = new SqlCommand("dbo.RegisterUpdate", sqlConn)) {
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@id", ????);
cmd.Parameters.AddWithValue("@fname", (TextBox)e.NewValues["fname"]);
cmd.Parameters.AddWithValue("@lname", (TextBox)e.NewValues["lname"]);
cmd.Parameters.AddWithValue("@company", (TextBox)e.NewValues["company"]);
cmd.Parameters.AddWithValue("@email", (TextBox)e.NewValues["email"]);
try {
sqlConn.Open();
}
catch (Exception ex) {
//handle exception
}
try {
cmd.ExecuteNonQuery();
}
catch (Exception ex) {
//handle exception
}
}
}
}
您从哪里获得用户ID