网格视图行更新和删除

时间:2012-12-16 16:06:21

标签: asp.net linq

我有一个带有以下标记的网格视图和以下数据(linq查询存储过程),我添加了删除和更新命令字段,但我不知道在删除和更新此网格视图之前我必须添加什么代码的工作原理。

<asp:GridView ID="GridViewDocuments_Search" runat="server" AutoGenerateColumns=False Visible="False" onrowcommand="GridViewDocuments_Search_RowCommand"  
  DataKeyNames="DocID" PageSize="100"  >
    <Columns>
    <asp:TemplateField HeaderText = "Details">
           <ItemTemplate>
      <asp:Button ID ="btn_Show" Text="Details" runat= "server" CommandName= "Details" CommandArgument='<%#
        Container.DataItemIndex%>' />
        </ItemTemplate>
            </asp:TemplateField>

        <asp:HyperLinkField  DataNavigateUrlFields="Docid,Transid" 
            DataNavigateUrlFormatString="~/DocResult.aspx?Docid={0}&TransID={1}"
            DataTextField="DocumentNo" HeaderText="Doc" />
        <asp:BoundField DataField="title" HeaderText="Title" SortExpression="title" />
        <asp:BoundField DataField="transmittal" HeaderText="transmittal" SortExpression="transmittal" />
        <asp:BoundField DataField="Docid" HeaderText="Docid" Visible="false" />
        <asp:CommandField ShowEditButton="True" />
        <asp:CommandField ShowDeleteButton="True" />
    </Columns>
</asp:GridView>


protected void btnSearch_Click(object sender, EventArgs e)
{
    _DataContext = new EDMSDataContext();
    var query = _DataContext.spQuickSearch(txtSearchKeywords.Text);
    GridViewDocuments.Visible = false;
    GridViewDocuments_Search.Visible = true;
    GridViewDocuments_Search.DataSource = query;
    GridViewDocuments_Search.DataBind();
}

存储过程spQuickSearch如下所示:

ALTER proc [dbo].[spQuickSearch]
@Searchtext varchar(50)=null

AS

 select DocId,TransId,DocumentNo,Title,TRANSMITTAL
from DocumentSum2
 where DocumentNo like '%'+@SearchText + '%'
 or Title like '%'+@SearchText + '%'
 or TRANSMITTAL like '%'+@SearchText + '%'

2 个答案:

答案 0 :(得分:0)

你可以处理这样的命令:

    protected void GridViewDocuments_Search_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        string cmdArg = e.CommandArgument.ToString();
        switch (e.CommandName)
        {
            case "Details":
                //TODO: handle your Details command...
                break;
            case "Delete":
                //TODO: handle your Delete command...
                break;
        }
    }

您可以根据需要添加任意数量的命令(CommandName属性用于控件)。例如,您可以添加另一个按钮并设置Delete CommandName,如下所示:

 <asp:Button ID ="btn_Delete" Text="Delete" runat= "server" CommandName= "Delete" CommandArgument='<%#
    Container.DataItemIndex%>' />

命令完成后,您可以调用DataBind来刷新网格:

GridViewDocuments_Search.DataBind();

答案 1 :(得分:0)