1

我正在使用一个网格视图,它显示存储在数据库中的队列中的人的姓名。我有一个 ItemTemplate 为每个人生成“签入”和“取消”按钮。单击其中一个按钮后,我想调用一个存储过程,该过程更改我的数据库中的一列,使其在我的网格视图中下降。

我遇到的问题是如何在我的 C# 中定位一个单独的框,所以我知道我传递给我的存储过程的内容。我不知道如何将该按钮连接到该行其余部分的信息。

<asp:GridView ID="Queue" runat="server"
               GridLines="None" SkinID="StatusGridSkin"
               AutoGenerateColumns="False" DataSourceID="DisplayQueueSource"
               EmptyDataText="No Wait Time"
               CellPadding="0" CellSpacing="10"
               OnItemCommand="OnGridItemCommand" PropertName="Date" DataKeyField="ID">
    <Columns>
         <asp:BoundField DataField="FirstName" HeaderText="Name"
                         ReadOnly="True" SortExpression="FirstName" />
         <asp:BoundField DataField="LastName" HeaderText=""
                         ReadOnly="True" SortExpression="LastName" />
         <asp:BoundField DataField="Date" DataFormatString="{0:hh:mm tt}"
                         HeaderText="Check In" HtmlEncode="False"
                         SortExpression="EndTime" />
         <asp:TemplateField ShowHeader="False"> 
             <ItemTemplate> 
                  <asp:Button ID="Checkin" runat="server"
                              CausesValidation="false" CommandName="Checkin"
                              Text="Check in"  CommandName="Checkin"/> 
             </ItemTemplate> 
         </asp:TemplateField> 
         <asp:TemplateField ShowHeader="False"> 
             <ItemTemplate> 
                 <asp:Button ID="Cancel" CommandName="MyButtonClick"
                             CommandArgument='<%# Container.DataItemIndex %>'
                             runat="server" CausesValidation="false"
                             Text="Cancel"  CommandName="Cancel"/> 
             </ItemTemplate> 
         </asp:TemplateField> 
     </Columns>
 </asp:GridView>
4

1 回答 1

2

向网格添加一些标记;

OnRowCommand="OnGridRowCommand"

并将 CommandName 添加到按钮。请记住,如果您使用“取消”或“删除”等“内置”名称之一,则应改为监听特定事件,例如 Deleted 事件。已识别命令的完整列表可在 msdn – GridView.RowCommand 事件中找到。无论如何,在按钮上,添加命令名和参数。

CommandName="RemoveRow" CommandArgument='<%# Container.DataItemIndex %>'

然后,在代码隐藏中

protected override void OnLoad(EventArgs e) {
    base.OnLoad(e);
    if (!Page.IsPostBack) {
        // on postback the grid is created thanks to the viewstate
        // that's why we don't bind it
        gridview.DataBind();
    }
}

protected override void OnInit(EventArgs e) {
    base.OnInit(e);
    gridview.DataBinding += bindGridView;
}

protected void OnGridRowCommand(object sender, GridViewCommandEventArgs args) {
    // The commandargument is set on the button, so a unique index for an item
    // should be used to identify it from the db.
    // in this case, commandargument is a string (not an int) so parse it
    int index = Int32.Parse((string)args.CommandArgument);
    switch (args.CommandName) {
        case "RemoveRow": {
                // remember, gridview.DataSource can be null here
                // so act on the database directly
                getSource().RemoveAt(index);
                gridview.DataBind();
            }
            break;
    }
}

private void bindGridView(object sender, EventArgs e) {
    // set the source from the database
    gridview.DataSource = getSource();
}

// this represents the db
private List<ItemViewModel> source;
private IList<ItemViewModel> getSource() {
    if (source == null) {
        source = new List<ItemViewModel>();
        source.Add(new ItemViewModel("Karl"));
        source.Add(new ItemViewModel("Urban"));
        source.Add(new ItemViewModel("Bill"));
    }
    return source;
}

class ItemViewModel {
    private Guid id;
    public ItemViewModel(string name) {
        id = Guid.NewGuid();
        FirstName = name;
    }

    public Guid Id { get { return id; } }
    public string FirstName { get; private set; }
}
于 2012-10-22T18:54:49.193 回答