asp.net使用C#编辑模板

时间:2013-08-19 06:40:08

标签: c# asp.net sql-server-2008 data-binding

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

使用的存储过程是:

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 开始      - 添加SET NOCOUNT ON以防止出现额外的结果集      - 干扰SELECT语句。     SET NOCOUNT ON;

-- Update statements for procedure here

    Update dbo.register
        SET fname = @fname, 
             lname = @lname,
            company=@company,
            email=@email
        WHERE dbo.register.id=@id
END     

GO

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

3 个答案:

答案 0 :(得分: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);
    }

如果您使用网格视图,则see

答案 1 :(得分:1)

您应该在ItemUpdating方法中绑定值。 将Handler与ListUpdating的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();
        }

希望这有帮助

答案 2 :(得分: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?