1

データベースに保存されているキューにいる人の名前を表示するグリッドビューを使用しています。各人に「チェックイン」ボタンと「キャンセル」ボタンを生成する ItemTemplate があります。これらのボタンの 1 つをクリックすると、データベース内の列を変更するストアド プロシージャを呼び出して、グリッドビューで行が削除されるようにします。

私が抱えている問題は、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をボタンに追加します。「キャンセル」や「削除」などの「組み込み」の名前のいずれかを使用している場合は、代わりに、削除済みイベントなどの特定のイベントをリッスンする必要があることに注意してください。認識されるコマンドの完全なリストは、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 に答える