从GridView中的buttonField将行索引传递给模态弹出窗口

时间:2015-03-05 16:04:29

标签: c# asp.net gridview modalpopupextender

我有一个GridView(见下文),它使用ButtonField按钮启动模态弹出窗口。 这是GridView

 <asp:GridView ID="GridView1" runat="server" OnRowCommand="GridView1_ButtonClick"  OnSelectedIndexChanged="GridView1_SelectedIndexChanged" CssClass="mGrid" AllowPaging="True" PagerStyle-CssClass="pgr"  
    AlternatingRowStyle-CssClass="alt" OnRowEditing="GridView1_RowEditing" AutoGenerateColumns="False">
                        <AlternatingRowStyle BackColor="#F7F7F7" CssClass="alt" />
                        <HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#F7F7F7" />
                        <Columns>
                            <asp:BoundField HeaderText="BeaconID" ShowHeader="True" Visible="True" DataField="beaconObjectid" />
                            <asp:BoundField DataField="aliasName" HeaderText="My Beacon Name" />
                            <asp:ButtonField ButtonType="Button" CommandName="AssignName" Text="Assign Name"/>
                            <asp:BoundField HeaderText="Action Type" DataField="actionType" />
                            <asp:BoundField DataField="actionContent" HeaderText="Action Content" />
                            <asp:ButtonField ButtonType="Button" CommandName="AssignAction" Text="Assign/Change Action"/>

                        </Columns>
                        <PagerStyle CssClass="pgr" />
                    </asp:GridView>

使用下面显示的C#代码,可以正常打开这个模态窗口:

 <asp:Button ID="showModal3" runat="server" Text="Show Modal"  CssClass="hiddenButton"/>
    <asp:Panel ID="Panel3" runat="server" CssClass="modalPopup">
        <div class="header">
        Set Alias Name
    </div>
        <div class="body">
            Set a new name for your beacon
            <br />

            <br />

            <div style="float: none;">
               <asp:TextBox ID="AliasNameTextBox" runat="server" Height="21px" Width="300px"></asp:TextBox> 
            </div>

            </div><br/>

         <div class="footer" align="right">
        <asp:Button ID="setAliasOK" runat="server" Text="OK" CssClass="yes" OnClick="setAliasOK_Click" CausesValidation="False" />
        <asp:Button ID="setAliasCancel" runat="server" Text="Cancel" CssClass="no" />
    </div></asp:Panel>
    <ajaxToolkit:ModalPopupExtender ID="SetAliasNameModalPopupExtender" runat="server" CancelControlID="setAliasCancel" DropShadow="True" PopupControlID="Panel3" TargetControlID="showModal3" BackgroundCssClass="modalBackground"></ajaxToolkit:ModalPopupExtender>

在我的C#代码中,我得到了这样的按钮索引,所以我知道哪一行被点击了,这一切都运行良好:

   protected void GridView1_ButtonClick(object sender, GridViewCommandEventArgs e)
        {
            GridViewRow row = GridView1.Rows[Convert.ToInt32(e.CommandArgument)]; // this gets the row
            string id = row.Cells[0].Text;
            string name = row.Cells[1].Text;
            string actionText = row.Cells[3].Text;

            if (e.CommandName == "AssignName")
            {
                SetAliasNameModalPopupExtender.Show();
                AliasNameTextBox.Text = name;
            }

            if (e.CommandName == "AssignAction")
            {
                ChooseActionModalPopupExtender.Show();
            }

            Label1.Text = name;
            Label2.Text = actionText;
            Label3.Text = id;

            currentlySelectedRow = Convert.ToInt32(e.CommandArgument);
            beaconIDnumber = id;
        } 

问题是当我点击模态弹出窗口中的“确定”按钮时。我需要从调用它的gridview按钮区访问行索引。我的OK按钮单击事件如下所示:

protected void setAliasOK_Click(object sender, EventArgs e)
        {
           var newName = AliasNameTextBox.Text;

            ParseChangeAliasName(beaconIDnumber, newName);

            SetAliasNameModalPopupExtender.Hide();
        }

所以我最终要问的是,我如何将gridview行传递给模态窗口,以便我可以使用相应行单元格中的数据 - 在这种情况下是信标ID(gridview中的DataField =“beaconObjectid”) 。 我甚至尝试在GridView1_ButtonClick方法中设置全局变量,然后在setAliasOK_Click方法中引用它们,但是一旦模态窗口打开它们就会被擦除。 理想情况下,我希望在不使用Javascript的情况下执行此操作。

1 个答案:

答案 0 :(得分:0)

以同样的方式

GridViewRow row = GridView1.Rows[Convert.ToInt32(e.CommandArgument)];

您可以设置Button属性:

setAliasOK.CommandName = "AliasOK" ;
setAliasOK.CommandArgument = e.CommandArgument ;

但是你需要处理你的按钮的RowCommand事件而不是Click事件来获取Button的命令参数