如何在gridview控件中检测链接按钮触发命令按钮

时间:2011-07-08 09:20:40

标签: c# asp.net gridview

我有一个奇怪的问题,就是我开发了一个Web应用程序,其中一种形式是我使用Gridview控件和命令按钮来查看行数据。如果在Gridview数据只有记录然后链接按钮工作正常,如果它是多个记录不工作链接按钮我无法忍受,这对我来说是非常奇怪的问题我有用!Page.IsPostBack也在pageload事件中,请帮帮我......

     protected void grdmanageloans_RowCommand(object sender, GridViewCommandEventArgs e)
    {
       if (e.CommandName == "Info") 
        {
            try
        {
            int loanid = Convert.ToInt32(e.CommandArgument);
            Session["Loanid"] = loanid;
            Session["Edit"] = "Edit";
            TabContainer1.ActiveTabIndex = 1;
            Session["TabContainer1"] ="loantab";
            Session["Tabloan"] = "Tabloan";
            Response.Redirect("Mortgageclient.aspx");

        }
        catch { }
                    }
       if (e.CommandName == "Delete")
        {
            try
            {

                int LoanId = Convert.ToInt32(e.CommandArgument);

                var PmtScheduleHistory = from del in mortgageentity.Pmt_Schedule_History where del.Loan.Loan_ID == LoanId select del;
                var LoanPayment = from del in mortgageentity.Payments where del.Loan_ID == LoanId select del;

                if (PmtScheduleHistory.Count() > 0)
                {
                    var DelPmtScheduleHistory = (from del in mortgageentity.Pmt_Schedule_History where del.Loan.Loan_ID == LoanId select del).First();
                    mortgageentity.DeleteObject(DelPmtScheduleHistory);
                    mortgageentity.SaveChanges();
                }
                var Getpayments = from db in mortgageentity.Payments where db.Loan_ID == LoanId select db;
                if (Getpayments.Count() > 0)
                {
                    foreach (var i in Getpayments)
                    {
                        mortgageentity.DeleteObject(i);
                        mortgageentity.SaveChanges();
                    }
                } 

                if (LoanPayment.Count() > 0)
                {
                    var DelPmtScheduleHistory = (from del in mortgageentity.Payments where del.Loan_ID == LoanId select del.Payment_Status.PaymentStatus_ID).First();
                    mortgageentity.DeleteObject(DelPmtScheduleHistory);
                    mortgageentity.SaveChanges();
                }

                var deletedata = (from del in mortgageentity.Loans where del.Loan_ID == LoanId select del).First();
                mortgageentity.DeleteObject(deletedata);
                mortgageentity.SaveChanges();
                BindData();
            }
            catch { }
        }

        if (e.CommandName == "AddNewloan")
        {
            Session["Addnewloan"] = "Addloan";
            Response.Redirect("Information.aspx");
        }
    }

这是我的.aspx页面

     <asp:GridView ID="grdmanageloans" runat="server" AutoGenerateColumns="False" AllowSorting="True"
                                                            GridLines="Both" PageSize="10" AllowPaging="true" OnRowCommand="grdmanageloans_RowCommand"
                                                            OnSelectedIndexChanged="grdmanageloans_SelectedIndexChanged" ShowFooter="true"
                                                            OnPageIndexChanging="grdmanageloans_PageIndexChanging" OnRowDataBound="grdmanageloans_Rowdatabound">
                                                            <AlternatingRowStyle BackColor="#F3F9FB" />
                                                            <RowStyle BackColor="#FEFEFE" VerticalAlign="Top" />
                                                            <HeaderStyle BackColor="#F3F9FB" />
                                                            <FooterStyle BackColor="#F3F9FB" />
                                                            <RowStyle Wrap="False" />
                                                            <HeaderStyle ForeColor="#1F476F" />
                                                            <Columns>
                                                                <asp:TemplateField HeaderText="LoanID" Visible="true">
                                                                    <ItemStyle HorizontalAlign="Center" />
                                                                    <ItemTemplate>
                                                                        <asp:Label ID="lblloanid" runat="server" Text='<%# Bind("Loan_ID") %>'></asp:Label>
                                                                    </ItemTemplate>
                                                                </asp:TemplateField>
                                                                <asp:TemplateField HeaderText="Loan Number">
                                                                    <ItemStyle HorizontalAlign="Center" />
                                                                    <ItemTemplate>
                                                                        <asp:Label ID="lblLoanNumber" runat="server" Text='<%# Bind("LoanNumber") %>'></asp:Label>
                                                                    </ItemTemplate>
                                                                </asp:TemplateField>
                                                                <asp:TemplateField HeaderText="Month Pay Amt" HeaderStyle-Wrap="false">
                                                                    <ItemStyle HorizontalAlign="Center" />
                                                                    <ItemTemplate>
                                                                        <asp:Label ID="lblMonthPayAmt" runat="server" Text='<%#Getammount(Eval("MonthPayAmt","{0:F2}")) %>'></asp:Label>
                                                                    </ItemTemplate>
                                                                </asp:TemplateField>
                                                                <asp:TemplateField HeaderText="Address">
                                                                    <ItemStyle HorizontalAlign="Center" />
                                                                    <ItemTemplate>
                                                                        <asp:Label ID="lblpropaddress" runat="server" Text='<%# Bind("PropAddress") %>'></asp:Label>
                                                                    </ItemTemplate>
                                                                </asp:TemplateField>
                                                                <asp:TemplateField HeaderText="City">
                                                                    <ItemStyle HorizontalAlign="Center" />
                                                                    <ItemTemplate>
                                                                        <asp:Label ID="lblpropcity" runat="server" Text='<%# Bind("PropCity") %>'></asp:Label>
                                                                    </ItemTemplate>
                                                                </asp:TemplateField>
                                                                <asp:TemplateField HeaderText="State">
                                                                    <ItemStyle HorizontalAlign="Center" />
                                                                    <ItemTemplate>
                                                                        <asp:Label ID="lblpropstate" runat="server" Text='<%# Bind("PropState") %>'></asp:Label>
                                                                    </ItemTemplate>
                                                                </asp:TemplateField>
                                                                <asp:TemplateField HeaderText="ClientID" Visible="false">
                                                                    <ItemStyle HorizontalAlign="Center" />
                                                                    <ItemTemplate>
                                                                        <asp:Label ID="lblclientid" runat="server" Text='<%# Bind("Client_ID") %>'></asp:Label>
                                                                    </ItemTemplate>
                                                                </asp:TemplateField>
                                                                <asp:TemplateField>
                                                                    <ItemStyle HorizontalAlign="Center" />
                                                                    <ItemTemplate>


                                                                      <asp:LinkButton ID="lnkeinfo" runat="server" CausesValidation="false" CommandName="Info"
                                                                            CommandArgument='<%#Eval("Loan_ID")%>'>Information</asp:LinkButton>
                                                                    </ItemTemplate>
                                                               </asp:TemplateField>
                                                            </Columns>
                                                        </asp:GridView>

2 个答案:

答案 0 :(得分:0)

为什么不使用RESTful架构在Session中保存这些参数并使页面紧密耦合?只需在网格中创建名为信息的链接,而不是按钮(发回数据),并将这些参数作为查询字符串附加到其末尾。这是更好的方法,结果是一样的。

答案 1 :(得分:0)

试试这个:

 <asp:LinkButton ID="lnkeinfo" runat="server" CausesValidation="false" CommandName="Info"
                                                                            CommandArgument='<%# Bind("Loan_ID") %>'>Information</asp:LinkButton>