0

我有一个带有以下标记和以下数据源(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 + '%'
4

2 回答 2

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();
于 2012-12-16T16:58:00.963 回答