我有一个带DataGrid的ASP.Net 4.6.1网页。在DataGrid内部,在TemplateColumn中,我有一个DropDownList。 DropDownList具有一个OnSelectedIndexChanged事件处理程序:
<asp:DataGrid runat="server" ID="dgPartnerApprovalPending" DataKeyField="OrderID"
CellPadding="10" CellSpacing="10" AlternatingItemStyle-BackColor="WhiteSmoke" HeaderStyle-BackColor="WhiteSmoke" AutoGenerateColumns="false"
AllowSorting="true" OnSortCommand="dgPartnerApprovalPending_SortCommand">
<Columns>
<asp:BoundColumn DataField="AttorneyName" HeaderText="Attorney Name" SortExpression="AttorneyName" />
<asp:BoundColumn DataField="Title" HeaderText="Title" SortExpression="Title" />
<asp:BoundColumn DataField="Description" HeaderText="Description" SortExpression="Description" />
<asp:BoundColumn DataField="RequestDate" HeaderText="Request Date" SortExpression="RequestDate" />
<asp:BoundColumn DataField="TotalAmount" HeaderText="Total Amount" SortExpression="TotalAmount" />
<asp:BoundColumn DataField="OrderStatus" HeaderText="Order Status" SortExpression="OrderStatus" />
<asp:TemplateColumn HeaderText="Status">
<ItemTemplate>
<asp:DropDownList runat="server" ID="ddlStatus" OnSelectedIndexChanged="ddlStatus_SelectedIndexChanged" AutoPostBack="true"
DataSourceID="sdsStatuses" DataTextField="OrderStatus" DataValueField="OrderStatusID" />
</ItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn DataField="OrderID" HeaderText="Order ID" SortExpression="OrderID" />
<asp:HyperLinkColumn Text="Files" DataNavigateUrlField="OrderID" DataNavigateUrlFormatString="~/Files/{0}" />
</Columns>
</asp:DataGrid>
OnSelectedIndexChanged事件处理程序工作正常,直到我向DataGrid添加一个OnItemDataBound事件处理程序:
<asp:DataGrid runat="server" ID="dgPartnerApprovalPending" DataKeyField="OrderID" OnItemDataBound="dgPartnerApprovalPending_ItemDataBound"
CellPadding="10" CellSpacing="10" AlternatingItemStyle-BackColor="WhiteSmoke" HeaderStyle-BackColor="WhiteSmoke" AutoGenerateColumns="false"
AllowSorting="true" OnSortCommand="dgPartnerApprovalPending_SortCommand">
<Columns>
<asp:BoundColumn DataField="AttorneyName" HeaderText="Attorney Name" SortExpression="AttorneyName" />
<asp:BoundColumn DataField="Title" HeaderText="Title" SortExpression="Title" />
<asp:BoundColumn DataField="Description" HeaderText="Description" SortExpression="Description" />
<asp:BoundColumn DataField="RequestDate" HeaderText="Request Date" SortExpression="RequestDate" />
<asp:BoundColumn DataField="TotalAmount" HeaderText="Total Amount" SortExpression="TotalAmount" />
<asp:BoundColumn DataField="OrderStatus" HeaderText="Order Status" SortExpression="OrderStatus" />
<asp:TemplateColumn HeaderText="Status">
<ItemTemplate>
<asp:DropDownList runat="server" ID="ddlStatus" OnSelectedIndexChanged="ddlStatus_SelectedIndexChanged" AutoPostBack="true"
DataSourceID="sdsStatuses" DataTextField="OrderStatus" DataValueField="OrderStatusID" />
</ItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn DataField="OrderID" HeaderText="Order ID" SortExpression="OrderID" />
<asp:HyperLinkColumn Text="Files" DataNavigateUrlField="OrderID" DataNavigateUrlFormatString="~/Files/{0}" />
</Columns>
</asp:DataGrid>
这是触发ItemDataBound事件时调用的代码:
protected void dgPartnerApprovalPending_ItemDataBound(object sender, DataGridItemEventArgs e)
{
ListItemType itemType = (ListItemType)e.Item.ItemType;
if (itemType == ListItemType.Item || itemType == ListItemType.AlternatingItem)
{
DataRowView view = (DataRowView)e.Item.DataItem;
string OrderID = view["OrderID"].ToString();
for (int i = 0; i <= 6; i++)
{
e.Item.Cells[i].Attributes.Add("OnClick", "window.location.href='PartnerApprovalDetail.aspx?oid="
+ OrderID
+ "'"
);
}
}
}
简而言之,ItemDataBound事件使每一行都可单击,并将用户重定向到另一个页面。添加此事件处理程序并加载页面后,每当我单击任意行中的DropDownList时,页面将变为空白。如果我在调试器中执行此操作,则永远不会触发SelectedIndexChanged事件。在使行可单击之后,我尝试将数据绑定到OnItemDataBound处理程序中的DropDownList。我还尝试了在PostBack之后将数据绑定到PageLoad()中的DropDownList。都没有帮助。我该如何解决?
答案 0 :(得分:1)
您要在表单元格<td>
上添加onclick事件处理程序,并且下拉列表将呈现在表单元格内。因此,当您尝试单击下拉列表时,click事件会被父级定义的事件处理程序捕获。表格单元格级别,使用户感到迷惑。
您可以创建一个新的模板列以呈现“详细信息”链接:
<asp:TemplateColumn HeaderText="Status">
<ItemTemplate>
<asp:HyperLink ID="DetailsUrls" runat="server" href="PartnerApprovalDetail.aspx?oid=<%#Eval("OrderID")%>">Detail</asp:HyperLink>
</ItemTemplate>
</asp:TemplateColumn>