2012-11-25 185 views
1

我有一个表格列出了客户从数据库中拉出。该表格还包含编辑和删除按钮,以更新现有客户的记录。我能够在第一页上为客户拉回所有表格数据。但是,当我点击客户旁边的“编辑”按钮时,它不会返回任何数据,而只是返回空白表单。我使用NavigateURL链接在“编辑”超链接中引用CustomerID。我已阅读并阅读,并对如何做到这一点感到困惑。任何人都可以看到我可能做错了什么?变得非常沮丧。请帮忙。在asp.net编辑/删除按钮c#

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

然后我重定向到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 [email protected]"; 

    //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"; 

    } 

} 

}

int CustomerID = 0; 

有:

+1

我想我错过了解析是在查询字符串在你编辑的ID码页。 – rene

+0

对不起,我不明白你的意思。你能澄清一下吗?在我的示例代码中有一个tryparse语句,但它指的是用户是否输入了CustomerID。但我并没有要求用户输入。 – user1576304

回答

1

在你的custmeredit.aspx

的Page_Load中替换此

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

非常感谢你!我一直试图弄清楚这个好几个小时。 – user1576304

+0

如果我可以问另一个问题,请:我的Customers.aspx页面有一个选项,可以添加另一个也重定向到CustomerEdit.aspx页面的客户。有没有办法将代码分开来添加新客户和更新现有客户?当点击“添加客户”按钮或“编辑”客户超链接时,用户将被重定向到EditCustomer.aspx页面加载事件。是否有可能编写一个if语句来说明如果customerID是空白的(因此添加了新的客户),它可以运行其他代码? – user1576304

+0

你最好开始一个新的问题(引用这个问题)。但我可以想象你给一个ID = 0作为查询字符串,并基于此做一个更新或插入。 – rene

相关问题