簡體   English   中英

Asp.Net WebForms Gridview OnRowCommand 事件未命中代碼隱藏方法

[英]Asp.Net WebForms Gridview OnRowCommand Event not hitting code behind method

我正在嘗試在網格視圖中合並一個編輯按鈕。 但是 on row 命令似乎沒有觸發。 我在后面的代碼中實現的 onrowcommand 方法上設置了一個斷點。

這是視圖(我在這里用 xxx 替換了 imageurl 作為隱私問題):

    <asp:GridView ID="GridViewContacts1" runat="server" CssClass="data_table" AutoGenerateColumns="false" OnRowCommand="GridViewContacts1_OnRowCommand" EmptyDataText="There are no contacts for this prospect." >
    <AlternatingRowStyle CssClass="alternating_row" />
    <Columns>
        <asp:BoundField DataField="ContactName" HeaderText="Name" />
        <asp:BoundField DataField="ContactTitle" HeaderText="Title" />
        <asp:TemplateField HeaderText="Phone" SortExpression="PhoneNumber">
            <ItemTemplate>
                <%# FormatPhone(Eval("PhoneNumber").ToString()) %>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:BoundField DataField="Email" HeaderText="Email" SortExpression="Email"></asp:BoundField>
        <asp:TemplateField HeaderText="Edit" ItemStyle-HorizontalAlign="Center">
            <ItemTemplate>
                <asp:ImageButton ID="icmdEditContact" Runat="server" ImageUrl="xxxxx" BorderStyle="None" CommandName="EditContact" CommandArgument='<%# Eval("ContactId") %>'/>
            </ItemTemplate>
        </asp:TemplateField>
    
        <asp:TemplateField HeaderText="Delete" ItemStyle-HorizontalAlign="Center">
            <ItemTemplate>
                <asp:LinkButton  CommandArgument='<%# Eval("ContactId") %>' CausesValidation="false" CommandName="DeleteItem" OnClientClick="javascript:return confirm('Are you sure you want to delete this record?')" Runat="server" ID="Linkbutton1"><img src="xxxxx" border="0"/></asp:LinkButton>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

正如您將看到的網格視圖的 OnRowCommand 屬性是:GridViewContacts1_OnRowCommand

這是背后的代碼在此處輸入圖像描述

我遇到的問題是這沒有受到打擊 在此處輸入圖像描述

有什么想法或建議嗎?

我什至嘗試通過僅使用普通按鈕和單擊事件在沒有 rowcommand 的情況下進行合並,但它仍然不會命中該單擊事件的指定代碼隱藏方法。 在此處輸入圖像描述

在此處輸入圖像描述

兩個按鈕都不起作用,還是只是刪除按鈕?

確保 GV 的視圖狀態沒有關閉,並且“總是”給每個按鈕一個“id”——你的刪除按鈕丟失了一個。

另一方面?

我經常,事實上 BEYOND 通常只是轉儲 GV 的內置命令。 你實際上不需要它們。

對於您放入 GV(模板列)的任何按鈕、圖像按鈕等,您只需添加/使用/擁有一個普通的 jane click 事件,然后使用它。

“獎勵”是您可以自由添加新按鈕,並且您不必使用或含糊不清地將所有命令跳轉到那個行上命令例程中。

所以,請檢查:

 make sure view state of gv not turned off
 make sure each button has a "id"
 make sure you not re-binding the gv on page load each time
 (you look to have the all important !IsPostback (good).

但是,如前所述,我不再使用行命令。

你可以這樣做:

說這個GV。

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
    DataKeyNames="ID"  width="40%" CssClass="table table-hover" >
    <Columns>
        <asp:BoundField DataField="FirstName" HeaderText="FirstName"  />
        <asp:BoundField DataField="LastName" HeaderText="LastName"  />
        <asp:BoundField DataField="HotelName" HeaderText="HotelName"  />
        <asp:BoundField DataField="Description" HeaderText="Description"  />
        <asp:TemplateField HeaderText="Active" ItemStyle-HorizontalAlign="Center">
            <ItemTemplate>
                <asp:CheckBox ID="chkActive" runat="server" 
                    Checked='<%# Eval("Active") %>'/>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:Button ID="cmdView" runat="server" Text="View" CssClass="btn"
                    OnClick="cmdView_Click" />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

和加載代碼:

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            LoadGrid();
    }

    void LoadGrid()
    {
        using (SqlConnection conn = new SqlConnection(Properties.Settings.Default.TEST4))
        {
            string strSQL = 
                @"SELECT * FROM tblHotelsA ORDER BY HotelName";
            using (SqlCommand cmdSQL = new SqlCommand(strSQL, conn))
            {
                conn.Open();
                DataTable rstData = new DataTable();
                rstData.Load(cmdSQL.ExecuteReader());
                GridView1.DataSource = rstData;
                GridView1.DataBind();
            }
        }
    }

我們看到/得到這個:

在此處輸入圖像描述

按鈕點擊代碼 - 每個按鈕都是分開的 - 很好!!

所以,這段代碼:

    protected void cmdView_Click(object sender, EventArgs e)
    {
        Button btn = (Button)sender;
        GridViewRow gRow = (GridViewRow)btn.NamingContainer;

        int iPK = (int)GridView1.DataKeys[gRow.RowIndex]["ID"];
        Debug.Print("Database PK = " + iPK);

        Debug.Print("Row index click = " + gRow.RowIndex);
        Debug.Print("Hotel Name = " + gRow.Cells[3].Text);   // non template values

        // get value of check box control on this row
        CheckBox chkActive = (CheckBox)gRow.FindControl("chkActive");
        Debug.Print("Value of active check box control = " + chkActive.Checked);

        // hide gv, show edit area.
        // bla bla bal
    }

現在點擊一個按鈕,我們得到這個 output:

Database PK = 16
Row index click = 0
Hotel Name = Batman's Cave
Value of active check box control = True

所以,請注意我們如何使用數據庫“datakeys”選項。 這對於安全來說非常重要,因為數據庫行 PK 永遠不會暴露在客戶端。

但是,如上所示:

 You can easy get the datakeys (row PK id)
 You can easy get the row index
 you have full use of the row (namingcontainer)
 you can use find control etc.

所以,真的,我看不到使用內置行索引命令的理由。

您可以自由添加/使用/擁有/享受按鈕、圖像按鈕或鏈接按鈕的命令參數,我經常使用它來添加/提供額外信息以傳遞給按鈕)。

但是為了公眾和歷史?

檢查viewestate,將“id”添加到那些沒有的按鈕。

但是,總而言之,由於您可以像放入頁面中的任何其他按鈕一樣使用/添加/單擊按鈕,所以我只是繼續使用簡單的普通簡按鈕單擊,而不必理會 GV 事件 model。

請注意,上面使用的命名容器技巧適用於 repeaters/listview/datalist 等。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM