简体   繁体   English

ASP.NET C#中的“编辑/删除”按钮

[英]Edit/Delete button in asp.net c#

I have a form that lists customers pulled from a db. 我有一个表格,列出了从数据库中拉出的客户。 The form also consists of edit and delete buttons in order to update an existing customer's record. 该表格还包含编辑和删除按钮,以更新现有客户的记录。 I am able to pull back all the table data for the customers on the first page. 我可以在第一页上为客户拉回所有表数据。 However, when I click the "Edit" button next to a customer it does not bring back any data but just brings back a blank form. 但是,当我单击客户旁边的“编辑”按钮时,它不会带回任何数据,而只会带回一个空白表格。 I am referencing the CustomerID in the "Edit" hyperlink with a NavigateURL link. 我使用NavigateURL链接引用“编辑”超链接中的CustomerID。 I have read and read and am getting confused about how to do this. 我已经阅读并阅读,并且对如何执行此操作感到困惑。 Can anyone see what I may be doing wrong? 谁能看到我可能在做错什么? Getting very frustrated. 变得非常沮丧。 Please help. 请帮忙。

<div>
    <h2>Customer Listing</h2>
    <br />
</div>

<p style="text-align:center">
    <asp:Button ID="btnCustomer" class="button" runat="server" Text="Add New Customer" onclick="btnCustomer_Click" />
</p>


<asp:ListView ID="lv" runat="server" 
    onselectedindexchanged="lv_SelectedIndexChanged">
<LayoutTemplate>
  <table width="110%" class="TableListing">
  <tbody>
    <thead>
    <th width="150">Customer Name</th>
      <th width="150">Email</th>
      <th width="150">City</th>
      <th width="40">State</th>
      <th width="110">Phone</th>
      <th width="80">Modify</th>
    </thead>
    <tr id="itemPlaceholder" runat="server"></tr>
     </tbody>
    </table>  

    <asp:DataPager ID="ItemDataPager" runat="server" PageSize="20">
        <Fields>
            <asp:NumericPagerField ButtonCount="5" />
        </Fields>
    </asp:DataPager>

</LayoutTemplate>

<ItemTemplate>
    <tr>
     <td><%# Eval ("LastName") %>, <%# Eval ("FirstName") %></td>
     <td><%# Eval ("Email") %></td>
     <td><%# Eval ("City") %></td>
     <td><%# Eval ("State") %></td>
     <td><%# Eval ("Phone") %></td>
     <td>
        <asp:HyperLink ID="lnkEdit" runat="server" NavigateUrl='<%# "CustomerEdit.aspx?ID=" + Eval("CustomerID") + Request.QueryString["LastName"] + Eval("LastName") %>' Text="Edit" />
    </td>
    </tr>
</ItemTemplate>

I then redirect to the CustomerEdit page: 然后,我重定向到CustomerEdit页面:

    protected void Page_Load(object sender, EventArgs e)
    {
        this.Master.HighlightNavItem("Customers");

        int CustomerID = 0;


        //Declare the connection object
        SqlConnection Conn = new SqlConnection();
        Conn.ConnectionString = ConfigurationManager.ConnectionStrings["MyDatabase"].ConnectionString;

        //Connect to the db
        Conn.Open();

    //Define query
        string sql = "SELECT * FROM Customer where CustomerID=@CustomerID";

    //Declare the Command
    SqlCommand cmd = new SqlCommand(sql, Conn);

    //Add the parameters needed for the SQL query
    //cmd.Parameters.AddWithValue("@LastName", LastName);

    //Declare the DataReader
    SqlDataReader dr = null;

    //Fill the DataReader
    dr = cmd.ExecuteReader();

    //Get the data
    if (dr.Read() == false)
    {
        //No Records
        dr.Close();
        Conn.Close();
        return;
    }

    txtFirstName.Text = dr["FirstName"].ToString();
    txtLastName.Text = dr["LastName"].ToString();

    dr.Close();
    Conn.Close();


    }


    protected void btnCancel_Click1(object sender, EventArgs e)
    {
        Response.Redirect("Customers.aspx");
    }

    protected void btnUpdate_Click(object sender, EventArgs e)
    {

        //Declare the connection object
        SqlConnection Conn = new SqlConnection();
        Conn.ConnectionString = ConfigurationManager.ConnectionStrings["MyDatabase"].ConnectionString;

        //Connect to the db
        Conn.Open();

        //Define query
        string sql = "INSERT INTO Customer (FirstName, LastName, Email, Password, Address1, Address2, City, State, Zip, Phone, Fax) VALUES (@FirstName, @LastName, @Email, @Password, @Address1, @Address2, @City, @State, @Zip, @Phone, @Fax)";
        //sql = "INSERT INTO xSample(Region,RepName,...) VALUES(@Region,@RepName,...)


        //Declare the Command
        SqlCommand cmd = new SqlCommand(sql, Conn);

        //Add the parameters needed for the SQL query
        cmd.Parameters.AddWithValue("@FirstName", txtFirstName.Text);
        cmd.Parameters.AddWithValue("@LastName", txtLastName.Text);
        cmd.Parameters.AddWithValue("@Email", txtEmail1.Text);
        cmd.Parameters.AddWithValue("@Password", txtPassword1.Text);
        cmd.Parameters.AddWithValue("@Address1", txtAddress1.Text);
        cmd.Parameters.AddWithValue("@Address2", txtAddress2.Text);
        cmd.Parameters.AddWithValue("@City", txtCity.Text);
        cmd.Parameters.AddWithValue("@State", txtState.Text);
        cmd.Parameters.AddWithValue("@Zip", txtZip.Text);
        cmd.Parameters.AddWithValue("@Phone", txtPhone.Text);
        cmd.Parameters.AddWithValue("@Fax", txtFax.Text);

        //Execute the query
        int NumRows = 0;
        NumRows = cmd.ExecuteNonQuery();

        Conn.Close();

        lblUpdate.Text = "Updated " + NumRows.ToString() + " record";

    }

}

} }

In your page_load of the custmeredit.aspx 在您的custmeredit.aspx的page_load中

replace this: 替换为:

int CustomerID = 0;

with: 有:

int CustomerID = Int32.Parse(Request.QueryString["ID"]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 GridView ASP.NET C#中的ASP按钮编辑 - ASP Button Edit in GridView ASP.NET C# 表格ASP.NET C#中每一行的删除按钮 - Delete button for each row in a table asp.Net C# 在gridview中单击“编辑”按钮时,如何调用“删除行”事件? asp.net C# - How can I call “delete row ” event when i click on “edit” button in gridview? asp.net C# 使用ASP.net和VB.net(或C#)从XML文件编辑和删除数据 - Edit and Delete data from XML file using ASP.net and VB.net (or C#) 单击Asp.net C#中的“编辑”按钮时,“编辑项目模板”下拉字段中的值未绑定 - Value Not Binding In Edit Item Template Dropdown Field When Click Edit Button In Asp.net C# 在gridview中使用内置的“编辑”和“删除”按钮。 ASP.NET C# - Using built in 'edit' and 'delete' buttons in gridview. ASP.NET C# 使用c#ASP.NET编辑图像并从GridView中删除行 - Edit Image and Delete Row from GridView using c# ASP.NET JQGrid ASP.net C#添加编辑删除未在数据库中更新 - JQGrid ASP.net C# add edit delete not updating in database Gridview带有来自数据库的下拉列表,并在gridview外部添加,编辑删除按钮? ASP.NET C# - Gridview with dropdownlist from the database and outside add,edit delete buttons outside the gridview ? ASP.NET C# 如何使用C#在单击按钮时编辑.aspx页并保存在asp.net中? - How to Edit .aspx page and Save in asp.net on button click using c#?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM