Sep 10, 2008

Double Clickable Rows with GridView

By double clicking on a row the user could open another form with a details view of that row. This type of functionality could easily be achieved with two separate buttons on each row of a GridView but I needed something better than that.
Create an new web application with a default page.Add a GridView and bind some data to it. Add asp:ButtonField controls using the edit columns option on the GridView. Give this button fields command name of DoubleClick. So the entire code looks like:

<Columns>

<asp:ButtonField Text="DoubleClick" CommandName="DoubleClick" Visible="false" />
</Columns>

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{

if (e.CommandName == "DoubleClick")

{
this.Message.Text += "Double clicked GridView row at index "
+ _selectedIndex.ToString();
}
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)

{
if (e.Row.RowType == DataControlRowType.DataRow)
{
LinkButton doubleClickButton = (LinkButton)e.Row.Cells[0].Controls[0];
// Get the javascript which is assigned to this LinkButton

string
_jsDouble =Page.ClientScript.GetPostBackClientHyperlink(doubleClickButton, "");
// Add this javascript to the ondblclick Attribute of the row

e.Row.Attributes["ondblclick"] = _jsDouble;

}

}

protected override void Render(HtmlTextWriter writer)
{
// The client scripts for GridView1 were created in GridView1_RowDataBound

foreach
(GridViewRow r in gvPropertyList.Rows)
{
if
(r.RowType == DataControlRowType.DataRow)
{
Page.ClientScript.RegisterForEventValidation(r.UniqueID + "$ctl00");
Page.ClientScript.RegisterForEventValidation(r.UniqueID + "$ctl01");
}
}
base
.Render(writer);
}


0 comments: