ASP.NET GridView focus selected row - c#

I am fairly new to ASP.NET. I have a GridView control which has many rows. Based on selection of each row I need to enable/disable few list and text controls on the collapsible panel in a ASCX.
The issue I am facing is whenever I select any row the GridView sets the focus on the first row all the time due to postback.
How do I set the focus on the selected row?
Can I use GetPostBackClientHyperlink in the RowDataBound somehow to register a client-side script?
Reply with some code is much appreciated.

Add a handler to the SelectedIndexChanged event of your GridView and insert the following code into this handler:
GridView1.SelectedRow.Focus(); // where GridView1 is the name of your GridView

You can also use jQuery. I added a <div> with an ID that is only shown when a row is using the edit template.
$('#hiddenDIV').focus();

Related

How could I edit at the same time the GridView and DetailsView

I´m working with a detailsView ans a GridView, within the DetailsView I have the ModeChanging event that allows the fields to be editable, I´d like that when I click on the modify button also allows edit the GridView fields without of the AutoGenerateEditbutton attribute, so that with this I could edit at the same time the DetailsView and GridView.
Is this possible?
thanks.
Set the EditIndex of your GridView to the row you want to edit on the modechange event of the detailsview. And remember to DataBind().

ASP GridView and DropDownList

I have an asp page with a GridView control. One of the columns in the grid view is a dropdownlist. the drop down list are filled using the RowDataBound event on the page load event. However, one of the options in the list, when selected, will popup a dialog that will allow the user to add a new item to the list, and that newly added item becomes the selected item for that drop down. This part I have working fine, but I want to be able to add the new item to the drop down list for every row in the grid. How do I, in a way, re bind the drop down list in all rows after the page has loaded without having to loop through each field?
If all the DDL's have a shared DataSource then it should just be a matter of forcing a postback after adding the new listitem. But how are you adding the new listitem?
If your popup is simply adding a new html <option> tag to an html <select> tag, this is not the same thing as updating the DDL DataSource.
For all the DDL's to show the new listitem your popup would have to perform a postback that updates the DDL datasource(s) and all the DDL controls in the GridView would need to be rebound.
This would happen automatically on a postback assuming that the DDL controls are connected to their datasource via the DataSourceID property. If you are making use of the DataSource property then you will have to call DataBind() explicitly for each DDL control, which I think you said you are doing in the GridView RowDataBound event.
Alternatively you could loop through all your ddl controls clientside and add the same item to each control, but you would lose all those changes the next time the page refreshed and I don't think that's what you want.

How does one turn a Gridview Row clickable which open a modalpopup in C#?

How does one turn a Gridview Row clickable which open a modalpopup in C#?
I have a gridview with several columns. Each row (the entire width of the row) needs to be clickable which onclick opens a modalpopupextender displaying a popup panal with detailed info on the row.
I have the modalpopupextender & panal working properly when an ImageButton is clicked. However I am removing the ImageButton and replacing its functionality with the row onClick.
I appreciate your time!
You should change the SelectionMode property to FullRowSelect.
You could use the SelectionChanged event to determine the current row selected.
GridView.SelectedRows[0].Index
will give you the index of the selected row. You will have to be sure MultiSelect is disabled as well.
So you have the index of the row selected, so now open whatever form needs opened depending on this index
Check this:
Edit Update GridView With Ajax ModalPopUpExtender Example
This Example explains How To Edit Update GridView Rows Records With Ajax ModalPopUpExtender In Asp.Net Using C# and VB.NET.

How to update a single column of a selected row in gridview using a button?

I have a Select button at the end of each row in gridview. I also have a separate button that I want to use to update one of the column of the selected row. This column contains a checkbox, I want to put a check in it when I click the button.
I do not want to use an Update button inside the gridview because I do not want the user to be able to change any other column.
Can anyone help? I have been researching this for a while now.
You've got a lot of option regarding your problem..
You could use Gridview's RowCreated or RowDataBound events or even on the buttons onclick event if that button is on the same row of the gridview(just get the sender etc//based on your logic).
here are some code snippet
If
e.Row.RowType = DataControlRowType.DataRow Then
e.Row.Attributes.Add("onclick", Page.ClientScript.GetPostBackEventReference(sender,
"Select$" & e.Row.RowIndex.ToString))
End If

How to get selected Row data on edit link click in gridview

I have a gridview with two extra buttons as Edit and Add on each row and I have a click event on each Edit and Add Linkbuttons through which I am opening a ModelViewExtender Dialog. I want when I am clicking on these linkbuttons on each row of gridview, all row data should accessed means Row Data of particular column (cells)from the row of clicked Edit link.
You can get better understanding thorough the below image of GridView, as:
Please suggest me any solution regarding the same.
Thanks in advance.
You have tow ways, either you use the AutoGenereateEdit property to true which will generate these hyperlinks. then on the row editing event you can get all these values easily by using the "e.NewEditIndex" which is the rowIndex of the GridViewRow your editing.
The second way is that you have put these hyperlinks "edit" and "add" as templates, in order to access them you need to parse the the sender object to it's control on the hyperlink click event and then get it's parent which will return the GridViewRow that the control on, which will allow you to get all the values you want from that row like this:
//Debug it and just make sure that tow parents return the GridViewRow
GridViewRow row = (GridViewRow)(((HyperLink)(sender)).Parent.Parent);

Categories