okay, I apologize. I promised to do this weeks ago and am only finally getting it up. Hopefully it will be worth the wait...
If you are planning on simply using a DDL (and not Cascading), look here -> http://www.myfriedmind.com/techBlog/2009/06/04/DropDownListGridViewAndDynamicallySelectingTheAppropriateListItemDuringEdit.aspx
Note that this example uses a remote webservice (rather than a PageMethod) for the CascadingDropDownList calls and I have not included them to simplify the amount of code on this page, but they are fairly straightforward -> http://www.asp.net/AJAX/AjaxControlToolkit/Samples/CascadingDropDown/CascadingDropDown.aspx
Sample DataObject (in c#)
class MyObject{ public int ObjId { get; set; } public string StateAbbreviation { get; set; } public string ZipCode { get; set; }}
Sample DataObject (in vb.net)
Class MyObject Public ObjId As Integer Public StateAbbreviation As String Public ZipCode As StringEnd Class
Entry on SampleGridView.aspx page (as simple as you can get it)
<asp:GridView runat="server" ID="lstMyData" DataKeyNames="ObjId" AutoGenerateColumns="false" EnableViewState="true" AutoGenerateEditButton="true" OnRowDataBound="lstMyData_RowDataBound" > <Columns> <asp:TemplateField> <ItemTemplate> <asp:Label runat="server" ID="lblState" Text='<%# Eval("StateAbbreviation") %>' /> </ItemTemplate> <EditItemTemplate> <asp:DropDownList runat="server" ID="uxState" /> <aKit:CascadingDropDown ID="ajxState" runat="server" TargetControlID="uxState" Category="State" ServiceMethod="GetStates" /> </EditItemTemplate> </asp:TemplateField> <asp:TemplateField> <ItemTemplate> <asp:Label runat="server" ID="lblZipCode" Text='<%# Eval("ZipCode") %>' /> </ItemTemplate> <EditItemTemplate> <asp:DropDownList runat="server" ID="uxZipCode" /> <aKit:CascadingDropDown ID="ajxZipCode" runat="server" TargetControlID="uxZipCode" Category="ZipCode" LoadingText="[Loading Zipcode...]" ServicePath="WebServiceToReturnData.asmx" ServiceMethod="GetZipCode" ParentControlID="uxState" /> </EditItemTemplate> </asp:TemplateField> </Columns></asp:GridView>
Codebehind (in c#) SampleGridView.apx.cs (note this does NOT include the code to return the values)
protected void lstMyData_RowDataBound(object sender, GridViewRowEventArgs e){ // verify this is a DataRow, not a Header, Footer, etc... if (e.Row.RowType == DataControlRowType.DataRow) { // in case you want to do something else here.... // see if this is the Row used for Editing if ((e.Row.RowState & DataControlRowState.Edit) != 0) { // get the DataItem that is bound // and cast it accordingly MyObject _myObject = (MyObject)e.Row.DataItem; // get the CascadingDropDowns CascadingDropDown _ajxState = (CascadingDropDown) e.Row.FindControl("ajxState"); CascadingDropDown _ajxZipCode = (CascadingDropDown) e.Row.FindControl("ajxZipCode"); // the data is automatically updated via the Ajax, // so we merely need to select the item _ajxState.SelectedValue = _myObject.StateAbbreviation // _ajxState.ContextKey = this can be set here if you are using it; _ajxZipCode.SelectedValue = myObject.ZipCode; } }}
Codebehind (in vb.net) SampleGridView.apx.cs (note this does NOT include the code to return the values)
Protected Sub lstMyData_RowDataBound(ByVal sender As Object, _ ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) _ Handles lstMyData.RowDataBound If e.Row.RowType = DataControlRowType.DataRow Then
' in case you want to do something else here....
' see if this is the Row used for Editing If (e.Row.RowState And DataControlRowState.Edit) <> 0 Then
' get the DataItem that is bound ' and cast it accordingly Dim _myObject As MyObject = _ DirectCast(e.Row.DataItem, MyObject)
' get the CascadingDropDowns Dim _ajxState As CascadingDropDown = _ DirectCast(e.Row.FindControl("ajxState"), CascadingDropDown) Dim _ajxZipcode As CascadingDropDown = _ DirectCast(e.Row.FindControl("ajxZipCode"), CascadingDropDown)
' ddl population should be handled by the Ajax ' ServiceMethod of the CascadingDropDown
' select the appropriate item _ajxState.SelectedValue = _myObject.StateAbbreviation _ajxZipCode.SelectedValue = _myObject.ZipCode
End If End IfEnd Sub
dasBlog theme by Mads Kristensen
Disclaimer The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.