Solved

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

Posted on 2010-09-22
5
514 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

830 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