Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Datagrid Control > Setting Focus in DataGrid - Solution

Reply
Thread Tools

Setting Focus in DataGrid - Solution

 
 
Kubuli John
Guest
Posts: n/a
 
      04-30-2004
I googled for a solution to this and didn't find a clear one, so I
thought I'd pass along what I ended up with.

In the PreRender event of the DataGrid, I execute the following code:

private void myGrid_PreRender(object sender, System.EventArgs e)
{
foreach (DataGridItem item in myGrid.Items)
{
foreach (Control ctl in item.Cells[0].Controls)
{
if (ctl.GetType() == typeof(System.Web.UI.WebControls.TextBox))
{
controlToFocus = ctl.UniqueID;
return;
}
}
}
}

In this method, controlToFocus is a page-level protected string. You
can customize the innermost foreach and if statements to find any
particular control you want to set the focus to - this example sets
the focus on the first TextBox it finds in the first column. Since I
only have TextBoxes when I'm ready to add or edit, this is fine.

Then, in the html, I have the following bits:

function setFocus()
{
ctl = "<%= controlToFocus %>";
if (ctl != "")
{
document.forms("my_form").elements(ctl).focus();
}
}

And:

<body onload="setFocus();">

Hope you find this useful...

John H.
 
Reply With Quote
 
 
 
 
Řystein Fallo
Guest
Posts: n/a
 
      04-30-2004
Excellent, John.

I've done alot of dirty workarounds to achive similar functionality.
Additionally, I would consider to emit the clientside-script using
Page.RegisterClientScriptBlock and Page.RegisterStartupScript:

Page.RegisterClientScriptBlock("SetFocus","<script
language=JavaScript>function setFocus(ctl,formname){if (ctl !=
""){document.forms("my_form").elements(ctl).focus( );}}</script>");
Page.RegisterStartupScript("SetFocusStartup","<scr ipt
language=JavaScript>SetFocus('"+controlToFocus+"', 'my_form');</script>");

Or, maybe even better, encapsulate the entire thing in a custom control

rgds
Řystein Fallo


"Kubuli John" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> I googled for a solution to this and didn't find a clear one, so I
> thought I'd pass along what I ended up with.
>
> In the PreRender event of the DataGrid, I execute the following code:
>
> private void myGrid_PreRender(object sender, System.EventArgs e)
> {
> foreach (DataGridItem item in myGrid.Items)
> {
> foreach (Control ctl in item.Cells[0].Controls)
> {
> if (ctl.GetType() == typeof(System.Web.UI.WebControls.TextBox))
> {
> controlToFocus = ctl.UniqueID;
> return;
> }
> }
> }
> }
>
> In this method, controlToFocus is a page-level protected string. You
> can customize the innermost foreach and if statements to find any
> particular control you want to set the focus to - this example sets
> the focus on the first TextBox it finds in the first column. Since I
> only have TextBoxes when I'm ready to add or edit, this is fine.
>
> Then, in the html, I have the following bits:
>
> function setFocus()
> {
> ctl = "<%= controlToFocus %>";
> if (ctl != "")
> {
> document.forms("my_form").elements(ctl).focus();
> }
> }
>
> And:
>
> <body onload="setFocus();">
>
> Hope you find this useful...
>
> John H.



 
Reply With Quote
 
 
 
 
Saravana [MVP]
Guest
Posts: n/a
 
      05-05-2004
Check out this articles,
http://aspalliance.com/aldotnet/exam...utoscroll.aspx
http://www.dotnetjunkies.com/howto/default.aspx?id=38


--
Saravana
Microsoft MVP - ASP.NET
www.extremeexperts.com



"Kubuli John" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> I googled for a solution to this and didn't find a clear one, so I
> thought I'd pass along what I ended up with.
>
> In the PreRender event of the DataGrid, I execute the following code:
>
> private void myGrid_PreRender(object sender, System.EventArgs e)
> {
> foreach (DataGridItem item in myGrid.Items)
> {
> foreach (Control ctl in item.Cells[0].Controls)
> {
> if (ctl.GetType() == typeof(System.Web.UI.WebControls.TextBox))
> {
> controlToFocus = ctl.UniqueID;
> return;
> }
> }
> }
> }
>
> In this method, controlToFocus is a page-level protected string. You
> can customize the innermost foreach and if statements to find any
> particular control you want to set the focus to - this example sets
> the focus on the first TextBox it finds in the first column. Since I
> only have TextBoxes when I'm ready to add or edit, this is fine.
>
> Then, in the html, I have the following bits:
>
> function setFocus()
> {
> ctl = "<%= controlToFocus %>";
> if (ctl != "")
> {
> document.forms("my_form").elements(ctl).focus();
> }
> }
>
> And:
>
> <body onload="setFocus();">
>
> Hope you find this useful...
>
> John H.



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
emulating window.focus in Body onload() event and setting focus to a control on same page Jason ASP .Net 4 05-07-2007 05:54 PM
this.window.focus() vs. window.focus() vs. this.focus() Roger Javascript 3 03-08-2007 08:53 PM
Setting Focus on Edit Row in Datagrid - how do I reference new clientid name??? jason@cyberpine.com ASP .Net 1 07-15-2005 08:16 PM
setting focus to a control within a datagrid Craig G ASP .Net 2 12-15-2004 06:12 PM
setting focus to datagrid subcontrol Bonj ASP .Net 1 11-13-2004 09:47 PM



Advertisments