1

我正在使用存储过程来编辑列表视图控件中的数据行。每行都有“编辑”和“删除”链接,因此当用户单击“编辑”时,该行应该可以编辑。我得到的错误是当用户点击“编辑”时,行不会变得可编辑。

使用的存储过程是:

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)

AS BEGIN -- 添加了 SET NOCOUNT ON 以防止额外的结果集 -- 干扰 SELECT 语句。设置无计数;

-- 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;
        }
4

3 回答 3

1

让我们尝试一下它可能会对您有所帮助。

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);
    }

如果您使用的是网格视图,请查看

于 2013-08-19T07:44:46.357 回答
1

您应该在 ItemUpdating 方法中绑定值。为 ItemUpdating 附加带有 listview 的处理程序。

当更新命令被触发时,你应该使用代码。

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();
        }

希望这可以帮助

于 2013-08-19T07:52:39.260 回答
1

试试这个代码

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?

于 2013-08-19T09:06:04.703 回答