views:

156

answers:

1

Hi guys.

I am having problem in EditItemTemplate of FormView.

When I use such code in InsertItemTemplate everything works:

<asp:DropDownList ID="Lic_PosiadaczLicencjiIDDropDownList" runat="server" 
    SelectedValue='<%# Bind("Lic_PosiadaczLicencjiID") %>' />
<asp:CascadingDropDown ID="CascadingDropDown1" runat="server" 
    TargetControlID="Lic_PosiadaczLicencjiIDDropDownList" Category="Knt_Kod" 
    ServicePath="~/ManagerLicencjiService.asmx" ServiceMethod="GetKontrahenci">
</asp:CascadingDropDown>  

But when I use exactly the same code in EditItemTemplate I am getting an error that SelectedValue is wrong cause it doesn't exists on the list of elements. I think that the problem is that DropDownList is checked for the values before it is populated by the service. When I run debugger the error occured before breakpoint in the service method.

How to solve this problem?

+1  A: 

<rant>I've found the CCD very clunky and full of poorly-documented workarounds</rant> but here is how you do something as simple as selecting a value when filling the ddl. Note that the selected value is not set on the DDL and that it is being passed to the web service where the selecting is done.

<asp:ScriptManager ID="sm1" runat="server"></asp:ScriptManager>
<asp:FormView ID="fv1" runat="server" DataSourceID="yourDataSource">
    <EditItemTemplate>
        <asp:DropDownList ID="Lic_PosiadaczLicencjiIDDropDownList" runat="server" />
        <asp:CascadingDropDown ID="CascadingDropDown1" runat="server" 
            TargetControlID="Lic_PosiadaczLicencjiIDDropDownList" Category="Knt_Kod" 
            ServicePath="~/ManagerLicencjiService.asmx" ServiceMethod="GetKontrahenci"
            UseContextKey="true" ContextKey='<%# Bind("Lic_PosiadaczLicencjiID") %>'>
        </asp:CascadingDropDown>
    </EditItemTemplate>
</asp:FormView>

<asp:sqldatasource id="yourDataSource"
    selectcommand="select Lic_PosiadaczLicencjiID FROM yourdatabase"
    UpdateCommand="Update yourdatabase set Lic_PosiadaczLicencjiID = @newvalue WHERE Lic_PosiadaczLicencjiID = @Lic_PosiadaczLicencjiID"
    connectionstring="<%$ ConnectionStrings:yourConnectionString %>" 
    runat="server" 
    onupdating="yourDataSource_Updating">
    <UpdateParameters>
        <asp:Parameter Name="newvalue" DbType="String" />
    </UpdateParameters>
</asp:sqldatasource>

code behind:

protected void yourDataSource_Updating(object sender, SqlDataSourceCommandEventArgs e)
{
    e.Command.Parameters["@newvalue"].Value = ((DropDownList)fv1.FindControl("Lic_PosiadaczLicencjiIDDropDownList")).SelectedValue;
}

and in your web service where you are getting your data from you need to add the context key to the signature exactly as shown as it is case sensitive. You then check your returned values for the selected value and set selected = true. If you want selected value instead of selected text then check for x.value instead of x.name.

[WebMethod]
public CascadingDropDownNameValue[] GetKontrahenci(string knownCategoryValues, string category, string contextKey)
{
     CascadingDropDownNameValue[] results = getdata();

     CascadingDropDownNameValue selectedVal = (from x in results where x.name == contextKey select x).FirstOrDefault();
     if (selectedVal != null)
         selectedVal.isDefaultValue = true;

    return results;
}

Hope this helps!

JumpingJezza
I fully support your rant! ;-)Not to mention that Microsoft shifts towards jQuery.Unfortunately I can't mark your post as an answer because, that does not solve the main problem which is that ASP requires items(selected item)from DDL before DDL is filled with them by the service method. I've discovered a solution for this but an ugly one: I am filling DDL with one known item on the server side before databinding. However, you've helped me with another problem: http://stackoverflow.com/questions/3838203/how-to-pass-an-additional-parameter-to-cascadingdropdown-servicemethod/3839134#3839134 +1 :)
Wodzu
By removing the "SelectedValue" tag from the DDL as shown above the aspx page shouldn't require "items(selected item)from DDL before DDL is filled with them by the service method."?
JumpingJezza
I did as you said and it doesn't return an error. I see items on the list, contextKey is working. However, now update operation doesn't work. By doesn't work I mean that newly selected values are not passed to the database. Instead the old ones are passed so there is no update.
Wodzu
Are you using onitemUpdating to update the database? I get the new value when I test my app.
JumpingJezza
Then I must be doing something wrong... I am not using ItemUpdating event. But I am using both controls DDL and CDD in EditItemTemplate. Does it work for you in EditItemTemplate?
Wodzu
Yep all I've got in my EditItemTemplate is the code above + an update button. The formview has OnItemUpdating="fv1_ItemUpdating" referring to my code behind method which does the updating.
JumpingJezza
Could you try to perform automatic update via DataSource instead of doing it manually in the event?
Wodzu
well I generally don't use DataSources as I don't have much of a clue how to debug them without using sql profiler and distrust "automagic" stuff like that (back in my day we coded in DOS on a black and green screen), but I have updated my answer above :)
JumpingJezza
So the code behind is an unavoidable :| Quite strange that I do not need to provide value (explicitly) for dataset in Insert mode but I must do it in Update. Thanks for your time :)
Wodzu
np! btw ASP is not the same as asp.net so you should probably retag question :)
JumpingJezza