2

我在 FormView 的 EditItemTemplate 中遇到问题。

当我在 InsertItemTemplate 中使用此类代码时,一切正常:

<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 中使用完全相同的代码时,我收到一个错误,即 SelectedValue 错误,因为它不存在于元素列表中。我认为问题在于在服务填充之前检查了 DropDownList 的值。当我运行调试器时,错误发生在服务方法中的断点之前。

如何解决这个问题呢?

4

1 回答 1

1

<rant>我发现 CCD 非常笨重,并且充满了记录不充分的解决方法</rant>,但这里是您如何做一些简单的事情,例如在填充 ddl 时选择一个值。请注意,所选值未在 DDL 上设置,而是传递给完成选择的 Web 服务。

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

后面的代码:

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

并且在您从中获取数据的 Web 服务中,您需要将上下文密钥添加到签名中,因为它区分大小写。然后,您检查所选值的返回值并设置 selected = true。如果您想要选定的值而不是选定的文本,请检查 x.value 而不是 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;
}

希望这可以帮助!

于 2010-10-04T03:04:50.510 回答