Solved

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

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

785 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