Solved

GridView: Change selected row THEN select a control in that row

Posted on 2010-09-22
5
511 Views
Last Modified: 2013-12-16
Using Visual Studio 2008...In a GridView, I want to click on a control (textbox) of a non-selected row and have THAT row become selected AND give focus to the control that was clicked. I can get this code to select the row:
e.Row.Cells[colseqClientName].Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink(this.GridViewSearchResults, "Select$" + e.Row.RowIndex);

But, I haven't figured out how to subsequently give the clicked textbox focus.
Is that possible?
(The GridView data is supplied by a SQL datasource. My coding options are: C# and javascript.)

Thanks,

-Ron
0
Comment
Question by:RonCoderre
  • 2
  • 2
5 Comments
 
LVL 41

Assisted Solution

by:guru_sami
guru_sami earned 500 total points
ID: 33736096
You can try doing that in GridView SelectedIndexChanged event:

protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        TextBox tb = (TextBox)GridView1.SelectedRow.FindControl("txtABCD");
        if (tb != null)
        {
            tb.Focus();
        }
    }
0
 
LVL 2

Author Comment

by:RonCoderre
ID: 33736443
Thanks for replying, guru_sami

The gridview row has many fields, many with textboxes and I don't know which will be selected before the code runs.

Example:
If row_10 is selected and a user clicks the BillingPerson textbox on row_4,
I want row_4 selected THEN the BillingPerson textbox on row_4 to receive the focus.

Upon clicking a textbox, perhaps there's a way to:
• store the clientID of the selected textbox
• Select the gridview row that contains the selected textbox (that action removes the focus from the textbox)
• Re-select the textbox from the stored clientID

I haven't found a way to combine this C# code:
ClientScript.GetPostBackClientHyperlink(this.GridViewSearchResults, "Select$" + e.Row.RowIndex);
with the javascript that assigns focus to a control:
document.getElementById(controlID).focus();
and hook that combination to the onClick event of the textbox (or anyplace else...as long as the end result is the same)
0
 
LVL 41

Accepted Solution

by:
guru_sami earned 500 total points
ID: 33738963
Ok I see what you mean but don't know exact solution.
What I think is even if you set that in javascript, the PostBack is going to reset it.
Not sure but see if using AJAX in someway helps.
0
 
LVL 2

Author Comment

by:RonCoderre
ID: 33740347
I stumbled upon some code that looks very promising.
I partially implemented it today and it seems to be getting me most of the way to where I want to be.
I'll finish the implementation tomorrow and see if I can tie up the loose ends.
If it works I'll post the solution.

(BTW...I already am using AJAX)
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now