62,065
社区成员
发帖
与我相关
我的任务
分享
<asp:DataList ID="DataList1" runat="server" width="400px"
OnItemCommand="DataList1_ItemCommand" OnItemDataBound="DataList1_ItemDataBound">
<HeaderTemplate>
任务列表
</HeaderTemplate>
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" Text="SingleClick" CommandName="SingleClick" Visible="false"/>
<asp:LinkButton ID="LinkButton2" runat="server" Text="DoubleClick" CommandName="DoubleClick" Visible="false"/>
<asp:Panel ID="Panel1" runat="server">
<span style="float:left;">
<b>编号: </b><%# Eval("Id") %>
<b>任务: </b><%# Eval("Task") %>
</span>
<span style="float:right;">
<b>是否完成: </b><%# Eval("IsDone") %>
</span>
</asp:Panel>
</ItemTemplate>
<HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" />
<ItemStyle BackColor="#F7F7DE" BorderStyle="Solid" BorderColor="lightgray" BorderWidth="1px" />
<AlternatingItemStyle BackColor="white" />
<SelectedItemStyle BackColor="#CE5D5A" />
</asp:DataList>
#region DataList1
protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
// Get the first LinkButton control
LinkButton _singleClickButton = (LinkButton)e.Item.Controls[1];
// Get the javascript which is assigned to this LinkButton
string _jsSingle = ClientScript.GetPostBackClientHyperlink(_singleClickButton, "");
// To prevent the first click from posting back immediately
// (therefore giving the user a chance to double click) pause the
// postback for 300 milliseconds by using setTimeout
_jsSingle = _jsSingle.Insert(11, "setTimeout(\"");
_jsSingle += "\", 300)";
// Get the panel wrapping the item
Panel itemPanelSingle = (Panel)e.Item.Controls[5];
// Add this javascript to the onclick Attribute of the panel
itemPanelSingle.Attributes["onclick"] = _jsSingle;
// Get the second LinkButton control
LinkButton _doubleClickButton = (LinkButton)e.Item.Controls[3];
// Get the javascript which is assigned to this LinkButton
string _jsDouble = ClientScript.GetPostBackClientHyperlink(_doubleClickButton, "");
// Get the panel wrapping the item
Panel _itemPanelDouble = (Panel)e.Item.Controls[5];
// Add this javascript to the ondblclick Attribute of the panel
_itemPanelDouble.Attributes["ondblclick"] = _jsDouble;
}
}
protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
{
DataList _dataList = (DataList)source;
// Get the selected index and the command name
int _selectedIndex = e.Item.ItemIndex;
string _commandName = e.CommandName;
switch (_commandName)
{
case ("SingleClick"):
_dataList.SelectedIndex = _selectedIndex;
this.Message.Text += "Single clicked DataList row at index " + _selectedIndex.ToString() + "<br />";
break;
case ("DoubleClick"):
this.Message.Text += "Double clicked DataList row at index " + _selectedIndex.ToString() + "<br />";
break;
}
}