0

我有 2 个 gridview 和 detailsview,使用 objectdatasource 检索。

当我选择 GridView1 时,它将在 GridView2 中显示其详细信息,并且可见的 DetailsView 为假。detailsview 中的值将传递给gridview。但是,现在该值无法传递到网格视图中。在进入 detailsview 日期绑定之前,系统总是会先执行 GridView2_RowDataBound。

这是代码:

   <asp:DropDownList ID="DropDownList1" 
            runat="server" 
            Height="19px" 
            Width="154px" 
            AppendDataBoundItems="True" 
            AutoPostBack="True" 
            DataTextField="invoiceStatus" 
            DataValueField="invoiceStatus" 
            DataSourceID="getStatus" >

</asp:DropDownList> 


&nbsp;<asp:ObjectDataSource ID="getStatus" runat="server" SelectMethod="getStatus" 
    TypeName="iWineBlInvoice">
    <SelectParameters>
        <asp:Parameter Name="poNum" Type="Int32" />
        <asp:Parameter Name="invoiceNum" Type="Int32" />
        <asp:Parameter Name="invoiceStatus" Type="String" />
    </SelectParameters> 

</asp:ObjectDataSource>
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" 
    SelectMethod="getInvoice" TypeName="iWineBlInvoice" FilterExpression="[invoiceStatus]='{0}'" >
    <SelectParameters>
        <asp:Parameter Name="invoiceNum" Type="Int32" />

    </SelectParameters>

</asp:ObjectDataSource>
<br />
    <asp:GridView ID="GridView1" runat="server" DataSourceID="ObjectDataSource1" 
    onrowdatabound="GridViewInvoice_RowDataBound" 
    onselectedindexchanged="GridViewInvoice_SelectedIndexChanged">
    <Columns>
        <asp:CommandField ShowSelectButton="True" />

        <asp:BoundField DataField="invoiceNum" HeaderText="Invoice No" 
            SortExpression="invoiceNum" />
        <asp:BoundField DataField="supplierName" HeaderText="Supplier " 
            SortExpression="supplierName" />
        <asp:CheckBoxField DataField="certifyGood" HeaderText="Goods Received" 
        SortExpression="certifyGood"  />

    </Columns>
    </asp:GridView>


  <asp:ObjectDataSource ID="ObjectDataSource3" runat="server" 
    SelectMethod="getDetails" TypeName="iWineBlInvoice">
    <SelectParameters>
        <asp:Parameter Name="supplierLogo" Type="String" />
        <asp:Parameter Name="supplierName" Type="String" />
        <asp:Parameter Name="supplierAddr" Type="String" />
        <asp:ControlParameter ControlID="GridViewInvoice" Name="poNum" 
            PropertyName="SelectedValue" Type="Int32" />
        <asp:Parameter Name="invoiceNum" Type="Int32" />

    </SelectParameters>
    </asp:ObjectDataSource>

       <asp:GridView ID="GridView2" runat="server" DataSourceID="ObjectDataSource3" 
    DataKeyNames="poNum" AutoGenerateColumns="False" Height="16px" 
    style="text-align: center" Width="696px" 
     onrowdatabound="GridView1_RowDataBound" >
    <Columns>
        <asp:BoundField DataField="poNum" HeaderText="Purchase Order No" 
            SortExpression="poNum" />
        <asp:BoundField DataField="invoiceDate" HeaderText="Invoice Date" 
            SortExpression="invoiceDate" />
        <asp:BoundField DataField="invoiceDueDate" HeaderText="Invoice Due Date" 
            SortExpression="invoiceDueDate" />
        <asp:BoundField DataField="deliveryDate" HeaderText="Delivery Date " 
            SortExpression="deliveryDate" />

        <asp:TemplateField HeaderText="Payment Date " SortExpression="datePaid">
            <ItemTemplate>
                <asp:Label ID="paymentDate" runat="server"></asp:Label>
            </ItemTemplate>

        </asp:TemplateField>

    </Columns>
    </asp:GridView>

    <asp:ObjectDataSource ID="ObjectDataSource2" runat="server" SelectMethod="getPayment" 
    TypeName="iWineBlInvoice">
    <SelectParameters>
        <asp:ControlParameter ControlID="GridViewInvoice" Name="poNum" 
            PropertyName="SelectedValue" Type="Int32" />
        <asp:Parameter Name="datePaid" Type="String" />
    </SelectParameters>
</asp:ObjectDataSource>

<br />

 <asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False" 
    DataSourceID="ObjectDataSource2" Height="50px" Width="125px" 
    ondatabound="DetailsView1_DataBound">
    <Fields>
        <asp:BoundField DataField="poNum" HeaderText="poNum" SortExpression="poNum" />

        <asp:TemplateField HeaderText="datePaid" SortExpression="datePaid">
            <ItemTemplate>
                <asp:Label ID="paymentDateLbl" runat="server" Text='<%# Bind("datePaid") %>'>      </asp:Label>
            </ItemTemplate>

        </asp:TemplateField>
    </Fields>
    </asp:DetailsView>

这是后面的代码:

String payment2 = string.Empty; 

protected void Page_Load(object sender, EventArgs e)
{

}

protected void DetailsView1_DataBound(object sender, EventArgs e)
{

    Label payment = (Label)DetailsView1.FindControl("paymentDateLbl");
    if (payment != null)
    {
        payment2 = payment.Text; 

    }

}

protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        Label aaa = new Label();
        aaa.Text = DropDownList1.SelectedItem.Text;

        if (aaa.Text == "Paid")
        {
            Label paymentDate = (Label)e.Row.FindControl("paymentDate");
            paymentDate.Text = payment2;  //payment 2 has become null 
        }

      } 
   }
4

0 回答 0