簡體   English   中英

綁定按鈕ItemTemplate以檢查已單擊的行

[英]Binding a button ItemTemplate to check in which row has been clicked

我有一個具有以下外觀的gridview:

第一眼

Edit是經典的Edit / Update / Cancel buttonColumn。 升級是集成了按鈕的ItemTemplate。 單擊“升級/降級”按鈕之一后,gridview的外觀如下:

按下按鈕后

現在,到目前為止,我已經實現了發送“保存”按鈕事件,以便僅獲取checkboxList之一(與“保存”按鈕相關聯的一個)的值。 但是,我想將CheckboxList / Save按鈕的每個Block關聯到單個Upgrade / Downgrade按鈕。 因此,如果單擊第一行上的按鈕,則需要禁用第二行上的塊,反之亦然。 我現在所擁有的是以下內容:

在升級/降級按鈕中單擊:

protected void Button2_Click(object sender, EventArgs e)
    {
        GridView1.Columns[2].Visible = true;
        button2Clicked = true;
        Session["buttonClicked"] = button2Clicked;
    }

在保存按鈕中單擊:

 protected void Button3_Click(object sender, EventArgs e)
    {

        CheckBoxList chb = new CheckBoxList();
        Button bt3 = (Button)sender;

            chb = (CheckBoxList)bt3.FindControl("Checkbox1");

            if(chb.SelectedValue=="Upgrade")
                Response.Write("Upgrade");
            else if (chb.SelectedValue == "Downgrade")
                Response.Write("Downgrade");
                   else
                        Response.Write("Not Allowed!");


    }

第二個版本的Save buttonClick如下:

 protected void Button3_Click(object sender, EventArgs e)
    {

        CheckBoxList chb = new CheckBoxList();
        Button bt3 = (Button)sender;

            chb = (CheckBoxList)bt3.FindControl("Checkbox1");
            if ((bool)Session["buttonClicked"])
            {
                if (chb.SelectedValue == "Upgrade")
                    Response.Write("Upgrade");
                else if (chb.SelectedValue == "Downgrade")
                    Response.Write("Downgrade");
                else
                    Response.Write("Not Allowed!");
            }
            else { Response.Write("Wrong Button clicked!"); }


    }

似乎“上/降級”按鈕單擊未存儲在任何地方。 我有什么事想念嗎?

因此,在@Andrei關於使用RowCommand事件的提示和找到的教程之后: 在這里這里(這在c#和VB中),我將代碼修改如下:

首先,我這樣修改按鈕:

 <asp:Button ID="Button2" runat="server" Text="Upgrade/Downgrade" OnClick="Button2_Click" CommandName="Command" CommandArgument="<%# Container.DataItemIndex %>" />

使用本教程了解Container.DataItemIndex

同樣,您必須向Gridview添加一個RowCommand事件,如下所示:

     <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
     onrowcancelingedit="GridView1_RowCancelingEdit"  OnRowCommand="GridView1_RowCommand"
    onrowediting="GridView1_RowEditing" onrowupdating="GridView1_RowUpdating" 
    onrowdatabound="GridView1_RowDataBound" EnableModelValidation="True" BackColor="White" BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px" CellPadding="3" ForeColor="Black" GridLines="Vertical">
              <AlternatingRowStyle BackColor="#CCCCCC" />

在后面的代碼中,有一些修改:

 protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {


            if (e.CommandName == "Command")
            {
                //Determine the RowIndex of the Row whose Button was clicked.
                int rowIndex = Convert.ToInt32(e.CommandArgument);

                //Reference the GridView Row.
                GridViewRow row = GridView1.Rows[rowIndex];

                //Fetch value of CheckboxList.
                CheckBoxList chb = (row.FindControl("Checkbox1") as CheckBoxList);
                chb.Enabled = true;
            }

    }

看起來很簡單!

暫無
暫無

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

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