【发布时间】:2018-02-01 21:53:31
【问题描述】:
我在 Asp.net 中有一个 GridView 行,单击事件正在处理它
我需要在我的网格行上添加一个双击事件。
我该怎么做?
请帮助我。
【问题讨论】:
-
是的,它在 Windows 窗体中
标签: javascript c# asp.net gridview grid
我在 Asp.net 中有一个 GridView 行,单击事件正在处理它
我需要在我的网格行上添加一个双击事件。
我该怎么做?
请帮助我。
【问题讨论】:
标签: javascript c# asp.net gridview grid
请关注下面的帖子,这对你真的很有帮助。
Clickable and Double Clickable Rows with GridView and DataList Controls
【讨论】:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
// Get the LinkButton control in the first cell
LinkButton _singleClickButton = (LinkButton)e.Row.Cells[0].Controls[0];
// 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)";
// Add this javascript to the onclick Attribute of the row
e.Row.Attributes["onclick"] = _jsSingle;
// Get the LinkButton control in the second cell
LinkButton _doubleClickButton = (LinkButton)e.Row.Cells[1].Controls[0];
// Get the javascript which is assigned to this LinkButton
string _jsDouble = ClientScript.GetPostBackClientHyperlink(_doubleClickButton, "");
// Add this javascript to the ondblclick Attribute of the row
e.Row.Attributes["ondblclick"] = _jsDouble;
}
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
GridView _gridView = (GridView)sender;
// Get the selected index and the command name
int _selectedIndex = int.Parse(e.CommandArgument.ToString());
string _commandName = e.CommandName;
switch (_commandName)
{
case ("SingleClick"):
_gridView.SelectedIndex = _selectedIndex;
this.Message.Text += "Single clicked GridView row at index " + _selectedIndex.ToString() + "<br />";
break;
case ("DoubleClick"):
Response.Write("<script type='text/javascript'>detailedresults=window.open('Patient//PatientDicomViewPage.aspx');</script>");
// Response.Redirect("ViewPatient.aspx");
this.Message.Text += "Double clicked GridView row at index " + _selectedIndex.ToString() + "<br />";
break;
}
}
Design of grid
<asp:GridView ID="GridView1" runat="server" BackColor="White" BorderColor="#DEDFDE"
BorderStyle="None" BorderWidth="1px" CellPadding="4" ForeColor="Black" GridLines="Vertical"
OnRowDataBound="GridView1_RowDataBound" OnRowCommand="GridView1_RowCommand">
<FooterStyle BackColor="#CCCC99" />
<Columns>
<asp:ButtonField Text="SingleClick" CommandName="SingleClick" Visible="false"/>
<asp:ButtonField Text="DoubleClick" CommandName="DoubleClick" Visible="false"/>
</Columns>
<RowStyle BackColor="#F7F7DE" />
<HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" />
<SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
<asp:Label id="Message" runat="server" ForeColor="Red" Font-Bold="true"></asp:Label>
【讨论】:
这是另一个只处理双击的解决方案:
在前端添加这些
OnRowDataBound="YourGrid_RowDataBound" OnRowEditing="YourGrid_RowEditing"
在后台
protected void YourGrid_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes["ondblclick"] = Page.ClientScript.GetPostBackClientHyperlink(YourGrid, "Edit$" + e.Row.RowIndex);
e.Row.Attributes["style"] = "cursor:pointer";
}
}
protected void YourGrid_RowEditing(object sender, GridViewEditEventArgs e)
{
// Excecute your code here
// Example:
// get the grid
GridView gv1 = (GridView)sender;
//get the row
GridViewRow gvr1 = (GridViewRow)gv1.Rows[e.NewEditIndex];
// get data from a cell for the row
string dataInCellOne= gvr1.Cells[1].Text.ToString();
}
【讨论】: