This post deals with 2-way binding of data to a CascadingDropDown. It assumes that the CascadingDropDown is already configured to return data to the list.
I had my DropDownList and CascadingDropDown pairs within a FormView and it is bound to a SqlDataSource.
<
asp
:
Label
ID
="Label1"
runat
="server"
Text
="Mfr" />
<
asp
:
DropDownList
ID
="ddlMfrs"
runat
="server" />
<
cc1
:
CascadingDropDown
ID
="cddMfr"
runat
="server"
TargetControlID
="ddlMfrs"
Category
="MfrID"
PromptText
="Select a manufacturer"
ServicePath
="MfrService.asmx"
ServiceMethod
="GetMfrs"
ContextKey
="M"
SelectedValue
='<%# BIND("MfrID") %>'>
</
cc1
:
CascadingDropDown
>
When the data is being bound (i.e. Selecting) there is no problem. If the MfrID value is a 3 then it selects the item from the drop down list with a 3 like it should. The issue occurred when the data was being updated back to the database. The error I received was that the input was not in the correct format.
When I checked the NewValues of MfrID in FormView ItemUpdating I saw that the value wasn’t a 3 but rather 3:::MfrName. The error makes sense now since the sproc was looking for an integer for its MfrID parameter.
Since this was my only page using the CascadingDropDown I dealt with the error on in its code behind. I added the following to the FormView’s ItemUpdating:
Protected
Sub fv1_ItemUpdating(ByVal sender AsObject, ByVal e As System.Web.UI.WebControls.FormViewUpdateEventArgs) Handles fv1.ItemUpdating
Dim strTemp AsString
Dim strKey AsString
ForEach strKey In e.NewValues.Keys
strTemp = e.NewValues(strKey).ToString
If strTemp.Contains(":::") Then
e.NewValues(strKey) = CleanBindValue(strTemp)
EndIf
Next
End
Sub
And this is the CleanBindValue function that gets called:
Private
Function CleanBindValue(ByVal DirtyValue AsString) AsString
'CascadingDropDown returns BIND values as value:::text
'and needs to be cleaned prior to database update
Dim strSplit() AsString
strSplit = DirtyValue.Split(":::")
Return strSplit(0).ToString
End
Function
Now the CleanBindValue function returns just the integer and the updates proceeds normally.
C# Code for the above two functions:
protectedvoid FormView1_ItemUpdating(object sender, FormViewUpdateEventArgs e)
{
string strTemp;
foreach (string strKey in e.NewValues.Keys)
{
strTemp = (string)e.NewValues[strKey];
if (strTemp.Contains(":::"))
{
e.NewValues[strKey] = CleanBindValue(strTemp);
}
}
}
privatestring CleanBindValue(string DirtyValue)
{
string[] strSplit;
string strDelimiter = ":::";
char[] chDelimiter = strDelimiter.ToCharArray();
strSplit = DirtyValue.Split(chDelimiter);
return strSplit[0];
}
Technorati Tags:
ASP.Net,
AJAX,
CSharp