如何从链接按钮单击的gridview获取命令参数的值?

时间:2014-12-31 06:12:45

标签: c# asp.net

我在aspx文件中创建了GridView

      <asp:GridView ID="gvAgentassigned" runat="server" AutoGenerateColumns="False" EmptyDataText="There are no agents."
                                DataKeyNames="AgentId" OnPageIndexChanging="gvAgentassigned_PageIndexChanging" OnRowCommand="gvAgentassigned_RowCommand"
                                GridLines="None" CellPadding="0" CellSpacing="0" CssClass="table">
                                <HeaderStyle />
                                <AlternatingRowStyle />
                                <Columns>

                                   <asp:BoundField DataField="AgentId" Visible="false">
                                        <HeaderStyle HorizontalAlign="left" />
                                    </asp:BoundField>

                                    ...
                                    ...
                                    ...
                                      <asp:LinkButton ID="lnkDelete" runat="server" CssClass="colorlnkbtndelete"
                                                        CommandArgument='<%# DataBinder.Eval (Container.DataItem, "AgentId") +"|"+  DataBinder.Eval (Container.DataItem, "StateId") +"|"+  DataBinder.Eval (Container.DataItem, "LineOfAuthorityId")%>'
                                                    ToolTip="Delete"><i class="icon-trash"></i></asp:LinkButton>

                                    <cc1:ConfirmButtonExtender ID="cnfbtn" TargetControlID="lnkDelete" DisplayModalPopupID="ModalPopupExtender1"
                                                runat="server">
                                            </cc1:ConfirmButtonExtender>
                                            <asp:Panel ID="pnlConfirm" runat="server" Style="display: none" CssClass="pnlCss Filterpopup Containerpopup">
                                                <Con:Confirm ID="UCConfirm" runat="server" />
                                                <div class="footer2">
                                                    <asp:Button ID="btnOk" class="btn btn-danger" runat="server" Text="Yes" OnClick="Lbtn_Delete_Click" />
                                                    <asp:Button ID="btnCancelConfirm" class="btn btn-danger" runat="server" Text="No" />
                                                </div>
                                            </asp:Panel>
                                            <cc1:ModalPopupExtender ID="ModalPopupExtender1" runat="server" TargetControlID="lnkDelete"
                                                PopupControlID="pnlConfirm" CancelControlID="btnCancelConfirm" BackgroundCssClass="modalBackground">
                                            </cc1:ModalPopupExtender>
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                </Columns>
                            </asp:GridView>

在aspx.cs文件中我写的是

protected void Lbtn_Delete_Click(object sender, EventArgs e)
                              {

                              CompanyAgentBL bl;

                                LinkButton btndetails = sender as LinkButton;
                                GridViewRow gvrow = (GridViewRow)btndetails.NamingContainer;
        string[] arguments = btndetails.CommandArgument.ToString().Split(new char[] { '|' });

        int AgentId = Convert.ToInt32(arguments[0].ToString());
        int StateId = Convert.ToInt32(arguments[1].ToString());
        int LineOfAuthorityId = Convert.ToInt32(arguments[2].ToString());

        //int CompanyId = Convert.ToInt32(gvAgentassigned.DataKeys[gvrow.RowIndex].Values["CompanyId"]);


        bl = new CompanyAgentBL(0);
        DataSet ds = new DataSet();
        // Cltpager.PageIndex = 1;
        try
        {

           // bl.AgentDeleteAGentCompany(ds, CompanyId, AgentId);
            SucessMessage("Agent has been  Successfully Deleted");
        }
        catch (Exception ee)
        {
            //ErrorMessage(ee.Message);
        }
        FillAgentGrid();

        }

现在问题是我无法从AgentId,StateId,LineOfAuthorityId整数变量中的命令参数获取值。请帮帮我!!!

1 个答案:

答案 0 :(得分:1)

我不确定但尝试使用此

而不是这个

string[] arguments = btndetails.CommandArgument.ToString().Split(new char[] { '|' });

试试这个

LinkButton ln =(LinkButton)gvAgentassigned.Rows[gvrow.index].FindControl("lnkDelete");

 string[] arguments = ln.CommandArgument.ToString().Split(new char[] { '|' });