如何使用c#从asp.net 2.0中的gridview获取TemlpateField数据?

时间:2012-05-17 06:39:38

标签: asp.net c#-2.0

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

我的网格视图

<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;
    }

请帮忙!

1 个答案:

答案 0 :(得分: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;
    }