Problems with AJAX CascadingDropDown and DropDownList SelectedValue in EditItemTemplate

前端 未结 1 1470
一向
一向 2020-12-21 14:12

I am having problem in EditItemTemplate of FormView.

When I use such code in InsertItemTemplate everything works:



        
相关标签:
1条回答
  • 2020-12-21 15:05

    <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!

    0 讨论(0)
提交回复
热议问题