【问题标题】:Filling a textbox from DropDownList Selection filled itself with CascadingDropDown从 DropDownList 填充一个文本框 选择用 CascadingDropDown 填充自己
【发布时间】:2016-04-26 14:23:12
【问题描述】:

我对 Visual Studio 还是很陌生。我现在正在使用 Visual Studio Express 2013,我正在尝试在 ASP.net 中填充某个文本框

我会尽我所能总结一切(我不是以英语为母语的人)

首先我遇到了无效回发的问题。我在这里只找到了一个正确的解决方案:https://johanleino.wordpress.com/2009/11/17/cascadingdropdown-causes-invalid-postback-or-callback-argument-error/ 所以,这就是为什么我有 NoValidationDropDownList 而不是经典的 dropDownList

另外,我有 2 个使用 ajax CascadingDropDown 的 DropDownList(我根据第一个的选定值填充第二个)

这是我的看法:

<asp:TableCell>
     <asp:NoValidationDropDownList OnSelectedIndexChanged="DropDownListVille_SelectedIndexChanged" ID="DropDownListVille" runat="server" class="ddlVille" ></asp:NoValidationDropDownList>
     <ajax:CascadingDropDown ID="CascadingDropDown1" runat="server" Category="Ville"
             TargetControlID="DropDownListVille" PromptText="Non définie" LoadingText="Chargement des villes"
             ServiceMethod="AfficherVille" ServicePath="CascadingDropDown.asmx"></ajax:CascadingDropDown>
</asp:TableCell>

<asp:TableCell runat="server">
     <asp:NoValidationDropDownList ID="DropDownListRue" runat="server" class="ddlRue" OnSelectedIndexChanged="DropDownListVille_SelectedIndexChanged"></asp:NoValidationDropDownList>
     <ajax:CascadingDropDown ID="ccdRegion" runat="server" Category="Rue" ParentControlID="DropDownListVille"
             TargetControlID="DropDownListRue" PromptText="Non définie" LoadingText="Chargement des rues"
             ServiceMethod="VilleRueLier" ServicePath="CascadingDropDown.asmx"></ajax:CascadingDropDown>
</asp:TableCell>

<asp:TableCell>
     <asp:TextBox ID="TextBoxCP" runat="server" class="tbcp"></asp:TextBox>
</asp:TableCell>

我的第一个下拉列表有城市,第二个有街道,我的文本框有邮政编码。所有数据都来自使用这两个表的一个数据库:

Ville(Id_Ville, nom_ville, code_postal) 翻译成

city(id_city,city_name,postcode)

Rue(Id_Rue,Nom_Rue) 翻译成

street(id_street,street_name)

我想根据所选城市的 id 动态更改邮政编码(此 id 作为值存储在下拉列表中)。

看,当用户想要的城市不在数据库中时,他可以选择第一个下拉框的特殊值,并添加一个新城市。

当他这样做时,页面会显示一些带有 ajax 的文本框。

在那里,用户可以添加一个带有邮政编码的新城市。否则,邮政编码 TextBox 处于只读状态。

但是当他选择一个列出的城市时,我希望文本框自己填满。

这是我的网络服务方法,链接到 cascadingDropDown :

[System.Web.Script.Services.ScriptService()]
public class WebService1 : System.Web.Services.WebService
{
private Passerelle.Passerelle passerelle = new Passerelle.Passerelle();


[WebMethod]
public CascadingDropDownNameValue[] AfficherVille(string knownCategoryValues, string category)
{


            List<CascadingDropDownNameValue> VilleDetails = new List<CascadingDropDownNameValue>();

            ListVille listeVille = new ListVille();
            listeVille = passerelle.getListVille();

            foreach (Ville v in listeVille.List)
            {

                string idVille = v.IdVille.ToString();
                string nomVille = v.NomVille.ToString();
                VilleDetails.Add(new CascadingDropDownNameValue(nomVille, idVille));
                Debug.WriteLine("Id Ville = " + idVille + " ----- NomVille = " + nomVille);
            }
            return VilleDetails.ToArray();
    }

[WebMethod]
public CascadingDropDownNameValue[] VilleRueLier(string knownCategoryValues, string category)
    {
    ///GET DATA FROM SQL

    ListRue listeRue = new ListRue();

    StringDictionary VilleDetails = AjaxControlToolkit.CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues);

    int idVille = Convert.ToInt32(VilleDetails["Ville"]);

    List<CascadingDropDownNameValue> countrydetails = new List<CascadingDropDownNameValue>();

    //Here I get the data from my table 'Rue' with the ID coming from
    listeRue = passerelle.getListRue(idVille);

            countrydetails.Add(new CascadingDropDownNameValue("Pas dans la liste", "-1"));

            foreach (Rue r in listeRue.list)
            {
                string idRue = r.idRue.ToString();
                string nomRue = r.nomRue.ToString();
                countrydetails.Add(new CascadingDropDownNameValue(nomRue, idRue));
                Debug.WriteLine("Id rue = " + idRue + " ----- NomRue = " + nomRue);
            }      

            return countrydetails.ToArray();

    }

}

我已经尝试了很多事情...一些 ajax 函数调用的经典事件。 我就是不知道该怎么做……

当然,我遵循的是经典的 MVC 模式。所以...视图中没有sql请求。

我在 ajax 方面做得不好。 我可能缺少另一个 webMethod 的一些很棒的 ajax/asp.net 功能。

非常感谢任何可以帮助我的人。

对于所有的拼写/语法错误,我们深表歉意。

我将在几个小时内无法回答(我会在大约 12 小时后回来)如果您有任何问题......我会在那里回答您。

【问题讨论】:

    标签: c# asp.net drop-down-menu cascadingdropdown


    【解决方案1】:

    好吧,我的错。

    我的活动无法正常工作,因为我忘记了 AutoPostBack=true。

    在我的视图项目周围使用 UpdatePanel 来拒绝刷新页面,我现在已经修复了所有问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-10
      • 2020-03-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多