如何在数据绑定事件中访问detailsview的数据源,因为DataItem始终为null?

时间:2014-05-16 16:00:38

标签: c# asp.net master-detail detailsview

我正在将单个GridView转换为master / detailsview,因为GV列的数量超过2个屏幕。我有一些控件,我在Gridview中动态显示/隐藏,我在墙上复制了DetailsView中的行为。

在我的gridView_RowDatabound事件中,我会做类似的事情:

protected void gridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {            
        DbDataRecord record = (System.Data.Common.DbDataRecord)e.Row.DataItem;
        bool flagW = (bool)record.GetValue(17);
        bool flagD = (bool)record.GetValue(18);
        int Consumption = (int)record.GetValue(21); 
        if (flagW == false)
        {
            WButton.Visible = true;
            WFlag.Visible = false;                
        }
        else
        {
            WButton.Visible = false;
            WFlag.Visible = true;
            WFlag.Checked = true;
            bool WarComplete = CheckWarrantyCompleted("WarrantyPart", record.GetValue(0).ToString());
        }
    }
}

所以,我正在做的是从SQLDataSource访问未绑定到GV控件的字段。 我遇到的问题是如果我在DetailsView_DataBound事件中执行类似的操作,则DataItem为null。 DV只有在PostBack上绑定的数据,因为它填充在GridView_SelectedIndexChanged事件上。

protected void DetailsView1_DataBound(object sender, EventArgs e)
{                
    DataRowView row = (DataRowView)DetailsView1.DataItem;
    DetailsView view = (DetailsView)sender;
    DetailsViewRowCollection rows = view.Rows;
    CheckBox WFlag = (CheckBox)DetailsView1.FindControl("WFlag");
    Button WButton = (Button)DetailsView1.FindControl("WButton");
    CheckBox DFlag = (CheckBox)DetailsView1.FindControl("DFlag");
    Button DButton = (Button)DetailsView1.FindControl("DButton");

    if (DetailsView1.DataItem != null)
    {
        DbDataRecord record = (System.Data.Common.DbDataRecord)DetailsView1.DataItem;
        bool flagW = (bool)row[17];
        bool flagD = (bool)row[18];
        int Consumption = (int)row[21];

        if (flagW == false)
        {
            WButton.Visible = true;
            WFlag.Visible = false;
        }    
    }  
}
<asp:GridView ID="GridView1"
              runat="server"
              AutoGenerateColumns="False"
              DataKeyNames="RequestNumber"
              AllowSorting="True"
              Width="450px"
              BackColor="White"
              BorderColor="#999999"
              BorderStyle="None"
              BorderWidth="1px"
              CellPadding="3"
              GridLines="Vertical"
              HorizontalAlign="Justify"                                        
              OnSelectedIndexChanging="GridView1_SelectedIndexChanging"
              EnableModelValidation="True">
<asp:DetailsView
              ID="DetailsView1"
              runat="server"                        
              Height="50px"
              Width="485px"
              CellPadding="3"
              AutoGenerateRows="False"
              DatasourceID="SqlDataSource2"
              ForeColor="White"
              EnableModelValidation="True"
              DataKeyNames="RequestNumber"
              HorizontalAlign="Left"
              Style="margin-right: 0px"
              OnItemCommand="DetailsView1_ItemCommand" 
              OnDataBound="DetailsView1_DataBound"  
              defaultmode="Edit"                       
              >
                    <Fields...</Fields>
</asp:DetailsView>

<asp:SqlDataSource ID="SqlDataSource2" runat="server" SelectCommand="SELECT * FROM Process_PartRequests WHERE (Process_PartRequests.RequestNumber = @RequestNumber)">
        <SelectParameters>
            <asp:ControlParameter ControlID="GridView1" Name="RequestNumber" PropertyName="SelectedValue" Type="Int32" />
        </SelectParameters>
    </asp:SqlDataSource>

我认为没有办法获得原始数据源。所以现在我想知道我唯一的选择是在行[0] .Cells [1]中获取Bound ID并创建一个新的DbDataRecord并执行我的逻辑。对于通过控件本身应该可见的东西来说似乎还有很长的路要走。

这真的是我唯一选择的选择吗?

0 个答案:

没有答案