我希望能够双击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 :(得分:0)
首先,我会做一些检查。其次,根据您的评论,看起来您正在处理MaterializedDataRecord
,因此尝试将其转换为DataRowView
是行不通的。
尝试将其转换为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);");
}
}
function ChangeBatchId(batchId)
{
// uncomment to debug batch id.
// console.log("BatchID: " + batchId.toString());
window.location.href = 'TestPage1.aspx?intBatchID=' + batchId.toString();
}