1

我正在尝试使用链接按钮从网格视图中删除数据。我需要逻辑方面的帮助,并使链接按钮在单击时从数据库中删除行数据。

网格视图

        <asp:GridView Style="width: 100%" ID="GvReportResults" runat="server"                       AutoGenerateColumns="False" EmptyDataText="No data" ShowHeaderWhenEmpty="True">
            <Columns>
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:LinkButton ID="lnkBtnEdit" runat="server" CausesValidation="false" >Edit</asp:LinkButton>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:LinkButton ID="LnkBtnRemove" runat="server" CausesValidation="false" CommandName="DeleteItem" CommandArgument='<%# Eval("OtherDataID") %>'>Delete</asp:LinkButton>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:BoundField DataField="SourceID" HeaderText="ID" />
                <asp:BoundField DataField="LastName" HeaderText="Last Name" />
                <asp:BoundField DataField="FirstName" HeaderText="First Name" />
                <asp:BoundField DataField="MiddleName" HeaderText="Middle Name" />
                <asp:BoundField DataField="Title" HeaderText="Title" />
                <asp:BoundField DataField="NationalID" HeaderText="SSN" />
                <asp:BoundField DataField="DOB" HeaderText="DOB" />
                <asp:BoundField DataField="HireDate" HeaderText="Hire Date" />
                <asp:BoundField DataField="Address1" HeaderText="Address" />
                <asp:BoundField DataField="City" HeaderText="City" />
                <asp:BoundField DataField="State" HeaderText="State" />
                <asp:BoundField DataField="PostalCode" HeaderText="Zip Code" />
            </Columns>
        </asp:GridView>

存储将数据导入到gridview的程序

    private void BindGrid()
{
    //set up arguments for the stored proc
    int? FacilityID = (ddlFacility2.SelectedValue.Equals("-1")) ? (int?)null : int.Parse(ddlFacility2.SelectedValue);
    int? OtherDataID = null;

    //bind
    GvReportResults.DataSource = this.DataLayer.model.MS_spGetOtherData(FacilityID, OtherDataID);
    GvReportResults.DataBind();
}
4

3 回答 3

1

OnClick例如,为 LinkBut​​ton添加属性(添加OnClick到您的链接按钮代码中)

<asp:LinkButton ID="LnkBtnRemove" runat="server" CausesValidation="false" CommandName="DeleteItem" CommandArgument='<%# Eval("OtherDataID") %>' OnClick='LnkBtnRemove_Click'>Delete</asp:LinkButton>

让听众OnClick作为

protected void LnkBtnRemove_Click(object sender,EventArgs e)
{
   string id = ((LinkButton)sender).CommandArgument;//CommandArgument is always returns string
   Do_DeleteRow(id);//method to delete
   BindGrid();
}

private void Do_DeleteRow(string id)
{
   //your delete code will be added here
}
于 2013-05-28T05:38:37.980 回答
0

将您的boundfiled 转换为项目模板,这里使用sourceid将触发delete查询。
代码如下所示:

protected void GvReportResults_RowCommand(object sender, GridViewCommandEventArgs e)
 {
    if (e.CommandName == "DeleteItem")
    {
        int getrow = Convert.ToInt32(e.CommandArgument);
        Label lblSourceID = (Label)GvReportResults.Rows[getrow].FindControl("lblSourceID");
        bool flag=deleteRecord(lblSourceID.text);
        if(flag)
        {
         succes msg!
        }
         else{ failed msg}
        }
    }

    public bool deleteRecord(String SourceID)
    {
    try
       {
        SqlCommand cmd = new SqlCommand("Your Delete Query where condtion  ", conn);
        conn.Open();
        cmd.ExecuteNonQuery();
        conn.Close();
        return true;
    }   
    catch(Exception ac)
    {
        return false;
    }

 }
于 2013-05-28T06:02:05.770 回答
0

只需将ItemCommand事件附加到您的网格

在您的网格定义中的网页上添加:

<asp:DataGrid OnItemCommand="Grid_ItemCommand" />

之后在后面的代码中:

void Grid_ItemCommand(Object sender, DataGridCommandEventArgs e)
{
    var id = Int32.Parse(e.CommandArgument.ToString()); //use parse if OtherDataID is int

    //here goes logic for deleting row
    this.DataLayer.model.spDeleteData(id);

    //after deleting rebind grid
    BindGrid();
}
于 2013-05-27T17:27:34.123 回答