0

我有一个带有模板字段的网格视图,可以将数据绑定到其中。在我的网格视图中,我还有一个命令字段(选择),用于在报告中打印相应的选定行数据。当我将模板字段设置为绑定字段时,GridView_SelectedIndexChanged()方法可以正常工作。但是我需要通过保持模板字段原样(不更改为绑定字段)来获得相同的功能。

我的网格视图是

<asp:GridView ID="dgvGeneralBillList" runat="server" style="font-size:11px;margin:0px auto auto 30px;width:auto;" AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" OnSelectedIndexChanged="dgvGeneralBillList_SelectedIndexChanged">
            <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
            <Columns>            
                <asp:TemplateField HeaderText="Bill ID">
                    <EditItemTemplate>
                        <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("BillID") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="lblBillID" runat="server" Text='<%# Bind("BillID") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Bill No">
                    <EditItemTemplate>
                        <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("SerialNo") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="lblSerialNo" runat="server" Text='<%# Bind("SerialNo") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Billed Week">
                    <EditItemTemplate>
                        <asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("BilledWeekNo") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="lblBilledWeekNo" runat="server" Text='<%# Bind("BilledWeekNo") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Billed Date">
                    <EditItemTemplate>
                        <asp:TextBox ID="TextBox4" runat="server" Text='<%# Bind("BilledWeekDate") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="lblBilledWeekDate" runat="server" Text='<%# Bind("BilledWeekDate") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Amount">
                    <EditItemTemplate>
                        <asp:TextBox ID="TextBox5" runat="server" Text='<%# Bind("Amount") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="lblAmount" runat="server" Text='<%# Bind("Amount") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Bill Status">
                    <EditItemTemplate>
                        <asp:TextBox ID="TextBox6" runat="server" Text='<%# Bind("BillStatus") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="lblBillStatus" runat="server" Text='<%# Bind("BillStatus") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:CommandField SelectText="print" ShowSelectButton="True" />
            </Columns>
            <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
            <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
            <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <EditRowStyle BackColor="#999999" />
            <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
        </asp:GridView>

功能是

protected void dgvGeneralBillList_SelectedIndexChanged(object sender, EventArgs e)
    {
        clsBill[] oBillList = new clsBill[1];
        clsBill oBill = new clsBill();
        //oBill.BillID = Convert.ToDouble(dgvGeneralBillList.SelectedRow.Cells[0].Text.ToString());
        oBill.BillID = Convert.ToDouble(dgvGeneralBillList.SelectedRow.FindControl("lblBillID").ToString());
        oBillList[0] = oBill;

        if (oBillList.Length < 1)
        {
            lblMessage.Text = "Error : No Bill Entry found";
            return;
        }

        BLBill oBLBill = new BLBill();
        string sErrorMessage = string.Empty;
        Object oOutput = oBLBill.Execute((int)BOCollectionType.ACTION_BILL.ACTION_BILL_GET_SINGLE_REPORT, oBillList, ref sErrorMessage);
        DSBillReport oDSBillReport = new DSBillReport();

        if (sErrorMessage != "")
        {
            lblMessage.Text = sErrorMessage;
            return;
        }
        else
        {
            oDSBillReport = (DSBillReport)oOutput;
            if (oDSBillReport.BillReport.Rows.Count > 0)
            {
                Session["GeneralBill"] = oDSBillReport;
                Session["MedicalBill"] = null;
                Response.Redirect("frmReportHolder.aspx");
            }
        }

        return;
    }

请帮忙 !

4

1 回答 1

0

好吧,看起来这个问题的一个非常简单的解决方案。这是我目前正在使用的代码。我只需将SelectedRow类型转换为GridViewRow,然后将所需的标签类型转换为新的标签对象(在try...catch块内)。到目前为止,这对我有用。谢谢大家。

protected void dgvGeneralBillList_SelectedIndexChanged(object sender, EventArgs e)
    {
        clsBill[] oBillList = new clsBill[1];
        clsBill oBill = new clsBill();

        try
        {
            GridViewRow oGridRow = (GridViewRow)dgvGeneralBillList.SelectedRow;
            Label lblID = (Label)oGridRow.FindControl("lblBillID");
            oBill.BillID = Convert.ToDouble(lblID.Text.ToString());
        }
        catch (Exception ex)
        { 

        }

        oBillList[0] = oBill;

        if (oBillList.Length < 1)
        {
            lblMessage.Text = "Error : No Bill Entry found";
            return;
        }

        BLBill oBLBill = new BLBill();
        string sErrorMessage = string.Empty;
        Object oOutput = oBLBill.Execute((int)BOCollectionType.ACTION_BILL.ACTION_BILL_GET_SINGLE_REPORT, oBillList, ref sErrorMessage);
        DSBillReport oDSBillReport = new DSBillReport();

        if (sErrorMessage != "")
        {
            lblMessage.Text = sErrorMessage;
            return;
        }
        else
        {
            oDSBillReport = (DSBillReport)oOutput;
            if (oDSBillReport.BillReport.Rows.Count > 0)
            {
                Session["GeneralBill"] = oDSBillReport;
                Session["MedicalBill"] = null;
                Response.Redirect("frmReportHolder.aspx");
            }
        }

        return;
    }
于 2012-05-17T10:26:33.970 回答