Trigger CommandName=Edit/Select from DropDownList in GridView
-
Alright, here’s what I want to do. I have a GridView with columns say EmployeeID, FirstName, LastName, and Department. Now, I want to add an additional column with the DropDownList. The list has the options of Select Edit Delete ( I know how to do this one) Now, if the user selects “Edit” or “Select”, I want that row to open up in a DetailsView either as read only or in editable mode depending on what the user selected. I am able to call the select commandname using a linkbutton. However, how do I call it using a dropdownlist? Also, how do I call it directly in editable form in a DetailsView from the dropdownlist? Thanks in advance for all the help and guidance.
-
Alright, here’s what I want to do. I have a GridView with columns say EmployeeID, FirstName, LastName, and Department. Now, I want to add an additional column with the DropDownList. The list has the options of Select Edit Delete ( I know how to do this one) Now, if the user selects “Edit” or “Select”, I want that row to open up in a DetailsView either as read only or in editable mode depending on what the user selected. I am able to call the select commandname using a linkbutton. However, how do I call it using a dropdownlist? Also, how do I call it directly in editable form in a DetailsView from the dropdownlist? Thanks in advance for all the help and guidance.
In this case my sujjestion is take the selectedvalue of dropdownlist, let us assume 1 for select, 2 for edit, 3 for delete. go to selectedIndexChanged event there capture selectedvalue(or SelectedText), based on this call. take the DetailsView columns as hyperlink column, set the URLFormat string & submit the page ..... Thanks Ramana
-
In this case my sujjestion is take the selectedvalue of dropdownlist, let us assume 1 for select, 2 for edit, 3 for delete. go to selectedIndexChanged event there capture selectedvalue(or SelectedText), based on this call. take the DetailsView columns as hyperlink column, set the URLFormat string & submit the page ..... Thanks Ramana
Sounds good. However, is URL the only way out? Also, how do I know which Employee ID was the SelectedIndexChanged trigerred for? Irfan
-
Sounds good. However, is URL the only way out? Also, how do I know which Employee ID was the SelectedIndexChanged trigerred for? Irfan
Yes, URL only one way, B'Cas this data exists in Grid, so grid hyperlink column will supports only URL,URLField,URLForamtString props. Finding of Employee ID is through EventArguments object e, try it u will get idea regards GV Ramana
-
Yes, URL only one way, B'Cas this data exists in Grid, so grid hyperlink column will supports only URL,URLField,URLForamtString props. Finding of Employee ID is through EventArguments object e, try it u will get idea regards GV Ramana
Hi Ramana, Thanks for the help. However, I will have to bug you a bit more. Unfortunately, I am unable to figure out on how to get the Employee ID from e. Since the DropDownList is going to be inside the TemplateField in GridView, I can't even see it in my .cs file. Hence, am unable to see what all I can do with e. Sorry for the trouble. With best wishes, Irfi
-
Alright, here’s what I want to do. I have a GridView with columns say EmployeeID, FirstName, LastName, and Department. Now, I want to add an additional column with the DropDownList. The list has the options of Select Edit Delete ( I know how to do this one) Now, if the user selects “Edit” or “Select”, I want that row to open up in a DetailsView either as read only or in editable mode depending on what the user selected. I am able to call the select commandname using a linkbutton. However, how do I call it using a dropdownlist? Also, how do I call it directly in editable form in a DetailsView from the dropdownlist? Thanks in advance for all the help and guidance.
Hi there, Because the
SelectedIndexChanged
event does not bubble up to the ItemCommand event of the GridView control like the Click event of the LinkButton, so you can create an event handler for the SelectedIndexChanged event of the dropdownlist to get the command name. Depending on what value the user selects, you can open up the DetailsView control either in read-only or editable mode by setting theDefaultMode
property toReadOnly
orEdit
accordingly. To get the value EmployeeID of the selected row, you might consider the two options below: + You can get from the column that the EmployeeID field is bound to. + You can save and get this values in theDataKeys
collection of the GridView control by specifying theDataKeyNames
property. To access the currently selected row from the event handler of SelectedIndexChanged event, you simply get the naming container of the dropdownlist since it is placed in the template field. The sample looks like this:protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList DropDownList1 = sender as DropDownList;
GridViewRow GridViewRow1 = DropDownList1.NamingContainer as GridViewRow;....
}
-
Alright, here’s what I want to do. I have a GridView with columns say EmployeeID, FirstName, LastName, and Department. Now, I want to add an additional column with the DropDownList. The list has the options of Select Edit Delete ( I know how to do this one) Now, if the user selects “Edit” or “Select”, I want that row to open up in a DetailsView either as read only or in editable mode depending on what the user selected. I am able to call the select commandname using a linkbutton. However, how do I call it using a dropdownlist? Also, how do I call it directly in editable form in a DetailsView from the dropdownlist? Thanks in advance for all the help and guidance.
Alright, this can get a little tricky. Option 1, use buttons or links. You just can add a Select column or just use the smart tag and check the Enable Selection checkbox and the IDE will add the column for you. Then, your detailsview will have to take the value of EmployeeId from the GridView, to do that you have to add EmployeeId to the DataKeyNames collection of the gridview (so the selected value will be the employeeid). Now, this IS tricky, because depending on the datasource your are using, the DetailsView will try to get the data, so you either will have to select one row in the GridView when you load the page or set the default value of the SelectParameter in the DetailsView's DataSource so it will return nothing if nothing is selected. (hope I've been clear) Then your details view could handle the other operations. If you must use a dropdown, then you will have to do everything in code, that is detect which selection was made and then get the data for the DetailsView and explicitly bind it. You will have to do two additionals things. First, you will have the AutoPostBack property of the dropdown to true. Second, if the user selects "Edit", you will have to add this code MyDetailsView.ChangeMode(DetailsViewMode.Edit); otherwise you will force the user to take another step to enter edit mode. hope it helps, it takes a little bit to get used to the new controls but once you do they are great.