C# ASP.NET AJAX CascadingDropDown Selected value property problem
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:DropD开发者_StackOverflowownList>
<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();
}
UPDATE:
my master page
...................................
<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Master.master.cs" Inherits="IMAM_APPLICATION.Master" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<%--<asp:ScriptManager ID="ScriptManager1" runat="server" />--%>
<asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
</asp:ToolkitScriptManager>
</div>
<div>
<table class="style1" frame="vSides">
<tr>
<td class="style17" bgcolor="#FFFF99">
</td>
<td class="style18" style="">
<img alt="" src="Images/banner1.gif"
style="width: 950px; height: 89px; margin-bottom: 0px;" /></td>
<td class="style17" bgcolor="#FFFF99">
</td>
</tr>
<tr>
<td class="style15">
</td>
<td class="style16" bgcolor="#FFFF99">
<asp:Menu ID="Menu1" runat="server" Width="44px" Orientation="Horizontal"
DataSourceID="SiteMapDataSource1" BackColor="#FFFBD6"
DynamicHorizontalOffset="2" Font-Names="Verdana" Font-Size="0.8em"
ForeColor="#990000" StaticDisplayLevels="2" StaticSubMenuIndent="10px">
<StaticMenuStyle BorderStyle="Dotted" />
<StaticSelectedStyle BackColor="#FFCC66" />
<StaticMenuItemStyle HorizontalPadding="5px" VerticalPadding="2px" />
<DynamicHoverStyle BackColor="#990000" ForeColor="White" />
<DynamicMenuStyle BackColor="#FFFBD6" />
<DynamicItemTemplate>
<%# Eval("Text") %>
</DynamicItemTemplate>
<DynamicSelectedStyle BackColor="#FFCC66" />
<DynamicMenuItemStyle HorizontalPadding="5px" VerticalPadding="2px" />
<StaticHoverStyle BackColor="#990000" ForeColor="White" />
<StaticItemTemplate>
<%# Eval("Text") %>
</StaticItemTemplate>
</asp:Menu>
</td>
<td class="style15">
</td>
</tr>
<tr>
<td class="style19" bgcolor="#FFFF99">
</td>
<td class="style20">
<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
</asp:ContentPlaceHolder>
</td>
<td class="style19" bgcolor="#FFFF99">
</td>
</tr>
<tr>
<td class="style7">
</td>
<td class="style8" style="" bgcolor="#FFFF99">
Copyright Imam Mahdi Association of Marjeya</td>
<td class="style7">
</td>
</tr>
</table>
</div>
<asp:SiteMapDataSource ID="SiteMapDataSource1" runat="server" />
</form>
</body>
</html>
Simple; set the SelectedValue
property of CascadingDropDown
control, instead of DropDown
control. It's working for me.
Eg. CascadingDropDown1.SelectedValue = "KY"
I have the same problem. As a workaround, I just used the PromptText property to bind to the value in the database. Hope it helps!
CascadingDropDown box2 = (CascadingDropDown)GridView1.Rows[rowIndex].Cells[2].FindControl("CascadingDropDown1");
CascadingDropDown box3 = (CascadingDropDown)GridView1.Rows[rowIndex].Cells[3].FindControl("CascadingDropDown2");
Try this code for cascading dropdown inside GridView...
I think that I will look for alternative solution which will bind the data to labels and if I want to update the value I will use CascadingDropDown.
精彩评论