如何在Datalist中单击Button时删除Session数据表行

时间:2016-11-25 08:24:46

标签: c# asp.net session delete-row datalist

如何使用datalist中的按钮从会话数据表中删除数据?

此代码用于显示添加到购物车内的产品,而bremove按钮用于从datalist中删除该项目

前端代码

<asp:DataList ID="DataList1" runat="server" Width="100%" HorizontalAlign="Center">
  <ItemStyle BorderStyle="Double" />
  <ItemTemplate>
    <center>
      <table class="table" style="border-style: dashed; top: 211px; left: 23px; height: auto; table-layout: fixed; width: 100%;">
        <tr>
          <td>
            <asp:Image ID="Pimg" runat="server" ImageUrl='<%# Eval("Pimg") %>' CssClass="img img-responsive" Height="140px" Width="240px" />
          </td>
          <td>
            <asp:Label ID="Pname" runat="server" Text='<%# Eval("Pname") %>'></asp:Label>
          </td>
          <td>
            <asp:Label ID="Pqty" runat="server" Text='<%# Eval("Pqty") %>'></asp:Label>
          </td>
          <td>
            <asp:Label ID="Price" runat="server" Text='<%# Eval("Price") %>'></asp:Label>
          </td>
          <td>
            <asp:Button ID="bremove" runat="server" Text="Remove" OnClick="bremove_Click" />
          </td>
        </tr>
      </table>
    </center>
  </ItemTemplate>
</asp:DataList>

后端代码

 protected void bremove_Click(object sender, EventArgs e)
    {
        int index = DataList1.SelectedIndex;
        DataTable dt = Session["AddToCard"] as DataTable;
        dt.Rows[index].Delete();
        porductlist = dt;
        BindData();
        Response.Redirect("Default.aspx");
    }
 public void BindData()
    {
        DataList1.DataSource = porductlist;
        DataList1.DataBind();
    }

3 个答案:

答案 0 :(得分:0)

添加@IBOutlet weak var buttonBottomConstraint: NSLayoutConstraint! .... func keyboardWillShow(notification: NSNotification) { if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue { if self.view.frame.origin.y == 0 { self.buttonBottomConstraint.constant -= keyboardSize.height self.view.layoutIfNeeded() } } } func keyboardWillHide(notification: NSNotification) { if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue { if self.view.frame.origin.y != 0 { self.buttonBottomConstraint.constant += keyboardSize.height } } }

AcceptChanges()

但是你打电话给protected void bremove_Click(object sender, EventArgs e) { int index = DataList1.SelectedIndex; DataTable dt = Session["AddToCard"] as DataTable; dt.Rows[index].Delete(); dt.AcceptChanges(); DataList1.DataSource = dt; DataList1.DataBind(); porductlist = dt; BindData(); Response.Redirect("Default.aspx"); } 并重定向,没有必要重定向。除非您想转到另一个页面,否则BindData()不是必需的。

<强>更新

要获取正确的行号,请使用带BindData()而不是Command的按钮,并将行号发送为Click

CommandArgument

答案 1 :(得分:0)

将自定义属性(例如数据库条目的ID)添加到按钮:

<asp:Button ID="bremove" runat="server" Text="Remove" OnClick="bremove_Click" entry-id='<%# Eval("ID") %>' />

然后在codebehind中你可以读出属性:

 protected void bremove_Click(object sender, EventArgs e)
 {
      string id = ((Button)sender).Attributes["data-id"];
      // .. your code .. 
 }

答案 2 :(得分:0)

请将update dt重新分配给会话变量以更新会话值。

 protected void bremove_Click(object sender, EventArgs e)
    {
        int index = DataList1.SelectedIndex;
        DataTable dt = Session["AddToCard"] as DataTable;
        dt.Rows[index].Delete();
        porductlist = dt;
        Session["AddToCard"]= dt
        BindData();
        Response.Redirect("Default.aspx");
    }
 public void BindData()
    {
        DataList1.DataSource = porductlist;
        DataList1.DataBind();
    }