I have a GridView that displays a list of names. On double-clicking a name, the name needs to be set in a textbox. This needs to happen w/o any postback The RowCreated event of GridView should add a javascript event handler for the OnDblClick event as follows:
ASPX Page:
<asp:TextBox ID="txtSearch" runat="server"/>
<asp:GridView ID="gvMain" runat="server" AutoGenerateColumns="false"
OnRowCreated="gvMain_RowCreated">
<Columns>
<asp:BoundField HeaderText="Name" DataField="Name" />
</Columns>
</asp:GridView>
RowCreated Event Handler:
protected void gvMain_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("OnDblClick",
"setSearch(" + e.Row.RowIndex + ");");
}
}
The Javascript for the SetSearch() is below:
<script language="javascript" type="text/javascript">
function setSearch(row) {
var tbl = document.getElementById("gvMain");
var txt = document.getElementById("txtSearch");
txt.value = tbl.rows[row+1].cells[0].innerText;
}
</script>
Since GridView is rendered as table, the individual cell can be accessed as tbl.rows[rowIndex].cells[cellIndex].innerText
|