C# ASP.NET AJAX CascadingDropDown Selected value propriety problem

Posted by Eyla on Stack Overflow See other posts from Stack Overflow or by Eyla
Published on 2010-05-07T17:36:46Z Indexed on 2010/05/07 17:38 UTC
Read the original article Hit count: 945

Filed under:
|
|
|

Greetings, I have a problem to use selected value propriety of CascadingDropDown.

I have 3 asp dropdown controls with ajax CascadingDropDown for each one of them.

I have no problem to bind data to the 3 CascadingDropDown but my problem is to rebind CascadingDropDown.

simply what I want to do is to select a record from Gridview which has the selected values for the CascadingDropDown that I want to pass then rebind the CascadingDropDown with selected value.

I'm posting my code down which include:

1-ASP.NET code.

2-Code behind to handle selected record from grid view.

3- web servisice that handle binding data to the 3 CascadingDropDown.

please advice how to rebind data to CascadingDropDown with selected value.

by the way I used selected value proprety as showning in my code but it is not working and there is no error.

Thank you,

........................ ASP.NET code ........................

 <%@ Page Title="" Language="C#" MasterPageFile="~/Master.Master" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="IMAM_APPLICATION.WebForm1" %>

<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
        DataKeyNames="idcontact_info" DataSourceID="ObjectDataSource1" 
        onselectedindexchanged="GridView1_SelectedIndexChanged">
        <Columns>
            <asp:CommandField ShowSelectButton="True" />
            <asp:BoundField DataField="idcontact_info" HeaderText="idcontact_info" 
                InsertVisible="False" ReadOnly="True" SortExpression="idcontact_info" />
            <asp:BoundField DataField="Work_Field" HeaderText="Work_Field" 
                SortExpression="Work_Field" />
            <asp:BoundField DataField="Occupation" HeaderText="Occupation" 
                SortExpression="Occupation" />
            <asp:BoundField DataField="sub_Occupation" HeaderText="sub_Occupation" 
                SortExpression="sub_Occupation" />

        </Columns>
    </asp:GridView>
    <asp:Label ID="lbl" runat="server" Text="Label"></asp:Label>
    <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" 
        DeleteMethod="Delete" InsertMethod="Insert" 
        OldValuesParameterFormatString="original_{0}" SelectMethod="GetData" 
        TypeName="IMAM_APPLICATION.DSContactTableAdapters.contact_infoTableAdapter" 
        UpdateMethod="Update">
        <DeleteParameters>
            <asp:Parameter Name="Original_idcontact_info" Type="Int32" />
        </DeleteParameters>
        <UpdateParameters>

            <asp:Parameter Name="Work_Field" Type="String" />
            <asp:Parameter Name="Occupation" Type="String" />
            <asp:Parameter Name="sub_Occupation" Type="String" />

            <asp:Parameter Name="Original_idcontact_info" Type="Int32" />
        </UpdateParameters>
        <InsertParameters>
            <asp:Parameter Name="Work_Field" Type="String" />
            <asp:Parameter Name="Occupation" Type="String" />
            <asp:Parameter Name="sub_Occupation" Type="String" />
        </InsertParameters>
    </asp:ObjectDataSource>

  <asp:DropDownList ID="cmbWorkField" runat="server" Style="top: 715px; left: 180px;
                    position: absolute; height: 22px; width: 126px">


                </asp:DropDownList>

                <asp:DropDownList runat="server" ID="cmbOccupation" Style="top: 745px; left: 180px;
                    position: absolute; height: 22px; width: 77px">

                </asp:DropDownList>
                <asp:DropDownList ID="cmbSubOccup" runat="server" 
                    style="position:absolute; top: 775px; left: 180px;">

                </asp:DropDownList>

                <cc1:CascadingDropDown ID="cmbWorkField_CascadingDropDown" runat="server" 
                    TargetControlID="cmbWorkField" 
                    Category="WorkField"  
                    LoadingText="Please Wait ..." 
                    PromptText="Select Wor kField ..." 
                    ServiceMethod="GetWorkField" 
                    ServicePath="ServiceTags.asmx">
                </cc1:CascadingDropDown>

                <cc1:CascadingDropDown ID="cmbOccupation_CascadingDropDown" runat="server" 

                    TargetControlID="cmbOccupation"
                     Category="Occup" 
                    LoadingText="Please wait..." 
                    PromptText="Select Occup ..." 
                    ServiceMethod="GetOccup" 
                    ServicePath="ServiceTags.asmx" 
                    ParentControlID="cmbWorkField">
                </cc1:CascadingDropDown>
                <cc1:CascadingDropDown ID="cmbSubOccup_CascadingDropDown" runat="server" 
                    Category="SubOccup" 
                    Enabled="True"
                     LoadingText="Please Wait..." 
                    ParentControlID="cmbOccupation" 
                    PromptText="Select Sub Occup" 
                    ServiceMethod="GetSubOccup" 
                    ServicePath="ServiceTags.asmx" 
                    TargetControlID="cmbSubOccup">
                </cc1:CascadingDropDown>


</asp:Content>
......................................................
C#  code behind
......................................................

   protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
             string strg = GridView1.SelectedDataKey["idcontact_info"].ToString();
            int index = Convert.ToInt32(GridView1.SelectedDataKey["idcontact_info"].ToString());
            //txtSearch.Text = GridView1.SelectedIndex.ToString();
           // txtSearch.Text = GridView1.SelectedDataKey["idcontact_info"].ToString();

            DSContactTableAdapters.contact_infoTableAdapter GetByIDAdapter = new DSContactTableAdapters.contact_infoTableAdapter();


            DSContact.contact_infoDataTable ByID = GetByIDAdapter.GetDataByID(index);
            //DSSearch.contact_infoDataTable FirstName = FirstNameAdapter.GetDataByFirstNameList(prefixText);
            foreach (DataRow dr in ByID.Rows)
            {
                lbl.Text = dr["Work_Field"].ToString() + "....." + dr["Occupation"].ToString() + "....." + dr["sub_Occupation"].ToString();


            cmbWorkField_CascadingDropDown.SelectedValue = dr["Work_Field"].ToString();
            cmbOccupation_CascadingDropDown.SelectedValue = dr["Occupation"].ToString();
            cmbSubOccup_CascadingDropDown.SelectedValue = dr["sub_Occupation"].ToString();


            }
        }
.......................................................
web Service 
.......................................................
 [WebMethod]
        public CascadingDropDownNameValue[] GetWorkField(string knownCategoryValues, string category)
    {

        //dsCarsTableAdapters.CarsTableAdapter makeAdapter = new dsCarsTableAdapters.CarsTableAdapter();
        //dsCars.CarsDataTable makes = makeAdapter.GetAllCars();
        DSContactTableAdapters.tag_work_fieldTableAdapter GetWorkFieldAdapter = new DSContactTableAdapters.tag_work_fieldTableAdapter();
        DSContact.tag_work_fieldDataTable WorkFields = GetWorkFieldAdapter.GetDataByGetWorkField();
        List<CascadingDropDownNameValue> values = new List<CascadingDropDownNameValue>();
        foreach (DataRow dr in WorkFields)
        {
            string Work_Field = (string)dr["work_Field_name"];
            int idtag_work_field = (int)dr["idtag_work_field"];
            values.Add(new CascadingDropDownNameValue(Work_Field, idtag_work_field.ToString()));
        }
        return values.ToArray();
    }

    [WebMethod]
    public CascadingDropDownNameValue[] GetOccup(string knownCategoryValues, string category)
    {
        StringDictionary kv = CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues);
        int idtag_work_field;
        if (!kv.ContainsKey("WorkField") || !Int32.TryParse(kv["WorkField"], out idtag_work_field))
        {
            return null;
        }
        //dsCarModelsTableAdapters.CarModelsTableAdapter modelAdapter = new dsCarModelsTableAdapters.CarModelsTableAdapter();
        //dsCarModels.CarModelsDataTable models = modelAdapter.GetModelsByCarId(makeId);
        DSContactTableAdapters.tag_OccupTableAdapter GetOccupAdapter = new DSContactTableAdapters.tag_OccupTableAdapter();
        DSContact.tag_OccupDataTable Occups = GetOccupAdapter.GetByOccup_ID(idtag_work_field);
        //           
        List<CascadingDropDownNameValue> values = new List<CascadingDropDownNameValue>();
        foreach (DataRow dr in Occups)
        {
            values.Add(new CascadingDropDownNameValue((string)dr["Occup_Name"], dr["idtag_Occup"].ToString()));
        }
        return values.ToArray();
    }

    [WebMethod]
    public CascadingDropDownNameValue[] GetSubOccup(string knownCategoryValues, string category)
    {
        StringDictionary kv = CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues);
        int idtag_Occup;
        if (!kv.ContainsKey("Occup") || !Int32.TryParse(kv["Occup"], out idtag_Occup))
        {
            return null;
        }

        //dsModelColorsTableAdapters.ModelColorsTableAdapter adapter = new dsModelColorsTableAdapters.ModelColorsTableAdapter();
        //dsModelColors.ModelColorsDataTable colors = adapter.GetColorsByModelId(colorId);

        DSContactTableAdapters.tag_Sub_OccupTableAdapter GetSubOccupAdapter = new DSContactTableAdapters.tag_Sub_OccupTableAdapter();
        DSContact.tag_Sub_OccupDataTable SubOccups = GetSubOccupAdapter.GetDataBy_Sub_Occup_ID(idtag_Occup);
        List<CascadingDropDownNameValue> values = new List<CascadingDropDownNameValue>();
        foreach (DataRow dr in SubOccups)
        {
            values.Add(new CascadingDropDownNameValue((string)dr["Sub_Occup_Name"], dr["idtag_Sub_Occup"].ToString()));
        }
        return values.ToArray();
    }

© Stack Overflow or respective owner

Related posts about c#

Related posts about ASP.NET