Tuesday, February 24, 2009

Keeping selected rows in a scrollable datagrid (or part of a page) in focus after postback

<asp:TemplateColumn HeaderText=”Quantity”>
<ItemTemplate>
<input
UNSELECTABLE=”on”
type=”text”
onmouseover=”this.style.cursor=’default’”
style=”BORDER-TOP-STYLE: none; BORDER-RIGHT-STYLE: none; BORDER-LEFT-STYLE: none; BACKGROUND-COLOR: transparent; BORDER-BOTTOM-STYLE: none; WIDTH: 50px”
value=<%#DataBinder.Eval(Container.DataItem,”Quantity”)%>
id=”HcTxt<%#DataBinder.Eval(Container.DataItem,”JobNumber”)%><%#DataBinder.Eval(Container.DataItem,”LineItem”)%>” NAME=HcTxt<%#DataBinder.Eval(Container.DataItem,”JobNumber”)%><%#DataBinder.Eval(Container.DataItem,”LineItem”)%>>
</ItemTemplate>
</asp:TemplateColumn>
private void DataGrid1_ItemCommand(object source, DataGridCommandEventArgs e)
{
if (e.CommandName == “Select”)
{
this.Page.RegisterStartupScript(“FocusOnControl”, “<script>obj = document.getElementById(\”HcTxt” + e.Item.Cells[1].Text.Trim() + e.Item.Cells[2].Text.Trim() + “\”); if (obj) obj.focus();</script>”);
}
}

Note: If you are not using a div tag, there are user controls available that do a much better job of keeping a part of a page in focus after postback. A search for SmartNav.dll might help.

No comments:

Post a Comment