使用AJAX CascadingDropDown和DropDownList的SelectedValue的在EditItemTemplate中存在的问题 [英] Problems with AJAX CascadingDropDown and DropDownList SelectedValue in EditItemTemplate

查看:362
本文介绍了使用AJAX CascadingDropDown和DropDownList的SelectedValue的在EditItemTemplate中存在的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在FormView控件的EditItemTemplate中有问题。

I am having problem in EditItemTemplate of FormView.

当我在InsertTemplate则用这样code一切正常:

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>  

但是,当我在EditItemTemplate中使用完全相同的code我得到一个错误的SelectedValue是错误原因的元素列表上不存在的。
我认为这个问题是DropDownList的被检查的值之前它是由服务填充。当我运行调试中的服务方法断点前发生的错误。

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.

如何解决这个问题呢?

推荐答案

&LT;咆哮&GT; 我已经找到了CCD非常笨重和全较差的记录变通办法&LT; /夸夸其谈&GT; 但这里是你怎么做作为填充DDL时选择一个值一样简单。注意,所选择的值未设置在DDL和它被传递到那里的选择完成Web服务

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

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

和从你需要上下文键添加到签名的严格按照图示,因为它是大小写敏感的地方你得到你的数据的Web服务。然后,您检查您的返回值选定的价值和选择设置为TRUE。如果你想选择的值,而不是选定文本然后检查x.value,而不是x.name。

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;
}

希望这有助于!

这篇关于使用AJAX CascadingDropDown和DropDownList的SelectedValue的在EditItemTemplate中存在的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆