2013-12-16 114 views
1

我希望能够双击GridView的行并将其重定向到显示被点击的行的详细信息的页面。双击GridView的行

我有我正在使用的代码,它似乎应该工作,但我收到错误“对象引用未设置为对象的实例”。

出于某种原因,该行出现空,我不确定原因。我的代码如下。有人可以看到我做错了什么吗?

<asp:GridView ID="GridView1" runat="server" AllowPaging="True" EnableViewState="true" OnDataBound="GridView1_DataBound" OnRowDataBound="GridView1_RowDataBound" 
    AllowSorting="True" AutoGenerateColumns="False" BorderStyle="Solid" GridLines="Both" HeaderStyle-BackColor="#990033" Width="1000px" 
    DataSourceID="EntityDataSource1"> 
    <HeaderStyle ForeColor="White"></HeaderStyle> 
    <Columns>    
     <asp:TemplateField> 
      <ItemTemplate> 
       <asp:HyperLink ID="HyperLink1" runat="server" Text='<%# Bind("intBatchID") %>' NavigateUrl="TestPage1.aspx?intBatchID={0}" Target="_blank"></asp:HyperLink> 
      </ItemTemplate> 
     </asp:TemplateField> 
     <asp:BoundField DataField="vcharName" HeaderText="Name" ReadOnly="True" 
      SortExpression="vcharName" /> 
     <asp:BoundField DataField="dtmScheduled" HeaderText="Date Scheduled" 
      ReadOnly="True" SortExpression="dtmScheduled" /> 
     <asp:BoundField DataField="intBatchPriorityLevel" 
      HeaderText="Priority Level" ReadOnly="True" 
      SortExpression="intBatchPriorityLevel" /> 
    </Columns> 
    <PagerSettings Mode="NumericFirstLast" Position="TopAndBottom" PageButtonCount="4" PreviousPageText="Previous" NextPageText="Next" FirstPageText="First" LastPageText="Last" /> 
    <PagerStyle HorizontalAlign="Center" />   
</asp:GridView> 
You are viewing page <%=GridView1.PageIndex + 1%> of <%=GridView1.PageCount%> 
<asp:EntityDataSource ID="EntityDataSource1" runat="server" OnSelected="EntityDataSource1_Selected" 
    ConnectionString="name=TestEntities" DefaultContainerName="TestEntities" 
    EnableFlattening="False" EntitySetName="tbl_Batch" 
    Select="it.[intBatchID], it.[vcharName], it.[dtmScheduled], it.[intBatchPriorityLevel]" OrderBy="it.[intBatchID]"> 
</asp:EntityDataSource> 



protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) 
    { 
     if (e.Row.RowType == DataControlRowType.DataRow) 
     { 
      System.Data.DataRowView drv = e.Row.DataItem as System.Data.DataRowView; 
      e.Row.Attributes.Add("ondblclick", String.Format("window.location='TestPage1.aspx?intBatchID={0}'", drv["intBatchID"])); 
     } 

     if (e.Row.RowType == DataControlRowType.DataRow) 
     { 
      e.Row.Attributes.Add("onMouseOver", "Highlight(this)"); 
      e.Row.Attributes.Add("onMouseOut", "UnHighlight(this)"); 
     } 
    } 

回答

0

首先,我会进行一些检查。其次,从你的评论看来,你看起来像是在处理MaterializedDataRecord,所以试图将它作为DataRowView投射是行不通的。



C#

尝试铸造它作为一个DbDataRecord

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) 
{ 
    if (e.Row.RowType == DataControlRowType.DataRow) 
    { 
     var dr = e.Row.DataItem as System.Data.Common.DbDataRecord; 

     if (dr != null && !Convert.IsDBNull(dr["intBatchID"])) 
     { 
      int intBatchId; 
      int.TryParse(dr["intBatchID"].ToString(), out intBatchId); 

      if (intBatchId > 0) 
      { 
       string js = "ChangeBatchId(" + intBatchId.ToString() + ");"; 
       e.Row.Attributes.Add("ondblclick", js); 
      } 
     } 
     e.Row.Attributes.Add("onMouseOver", "Highlight(this);"); 
     e.Row.Attributes.Add("onMouseOut", "UnHighlight(this);"); 
    } 
} 



JAVASCRIPT

function ChangeBatchId(batchId) 
{ 
    // uncomment to debug batch id. 
    // console.log("BatchID: " + batchId.toString()); 

    window.location.href = 'TestPage1.aspx?intBatchID=' + batchId.toString(); 
}