4

我有一个用于“创建新配置文件”的表格。我的问题是关于 DropDownLists。

第一个 DropDown 根据其值动态填充第二个。

看这张照片:

http://image.noelshack.com/fichiers/2013/22/1369819471-picture-help.png

以及下图:

http://image.noelshack.com/fichiers/2013/22/1369830738-help2.png

您可以看到我的第二个 ddl(“Fonction”)已正确填写,但是当我单击提交按钮时,该值变为空值(“Sélectionnez...”),因此我的 RequiredFieldValidator 使页面无效!

即使不是因为我的第一个 DropDownList 的 SelectedIndexChanged,我的第二个 DropDownList 似乎在每次回发时都是有界的。第一个 DropDownList 的 SelectedIndexChanged 始终在回发时调用,因此它在每个回发时抛出“populateDdl()”(如果选择了一个值)。

当我单击提交按钮时,它会在我的数据库中注册一个空白值。

我错过了什么?

ASP代码:

<asp:DropDownList ID="ddlTypePN" runat="server" DataSourceID="SqlTypePN" EnableViewState="true" 
        DataTextField="libelle" DataValueField="valeur" AutoPostBack="true" OnSelectedIndexChanged="ddlTypePN_SelectedIndexChanged" 
        OnDataBound="ddlTypePN_DataBound" > </asp:DropDownList> 

<asp:DropDownList runat="server" ID="ddlFctPN" AppendDataBoundItems="false" OnDataBound="ddlFctPN_DataBound" > </asp:DropDownList> 

后面的代码:

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        ddlTypeProf.DataBind(); // don't care
        ddlSsoSrc.DataBind(); // don't care
        ddlTypePN.DataBind(); // The ddl that populate my 2nd ddl
    }
}

protected void ddlTypePN_SelectedIndexChanged(object sender, EventArgs e)
{
    string type = ddlTypePN.SelectedValue.ToString().Trim();
    // if PNT
    if (type.ToUpper().Trim().Equals("PNT"))
    {               
        ddlFctPN.Enabled = true;
        ddlTypeAv.Enabled = true;
        rfvTypeAv.Enabled = true;
        populateDdl();

    }
    else if (type.ToUpper().Trim().Equals("PNC"))
    {                
        ddlFctPN.Enabled = true;
        ddlTypeAv.Enabled = false;
        rfvTypeAv.Enabled = false;
        populateDdl();
    }     
}

void populateDdl()
{
    string val = "fct"+ddlTypePN.SelectedValue.ToString().Trim(); // Used for SELECT
    SqlConnection sqlConn = new SqlConnection(ConfigurationManager.ConnectionStrings["My_DB"].ConnectionString);
    ddlFctPN.Items.Clear();
    DataTable subjects = new DataTable();
    try
    {
        SqlDataAdapter adapter = new SqlDataAdapter("My SELECT", sqlConn);
        adapter.Fill(subjects);

        ddlFctPN.DataSource = subjects;
        ddlFctPN.DataTextField = "libelle";
        ddlFctPN.DataValueField = "valeur";
        ddlFctPN.DataBind();
    }
    catch (Exception ex)
    {
        lblErr.Text = ex.Message;
    }
    ddlFctPN.Items.Insert(0, new ListItem("Sélectionnez...", "null"));
}
4

9 回答 9

2
if(!IsPostBack)

 {

  populateDdl();

 }
于 2013-05-29T10:16:37.827 回答
2

解决方案移至问题的答案:

我终于找到了问题所在。每个控件都在一个<asp:Table>中,我必须EnableViewState="true"在此表上进行设置,以便能够在回发后保留该值。

于 2020-03-13T16:29:42.790 回答
1

从您的标记开始,您尚未在第二个下拉列表中设置 AutoPostBack 属性。因此,当第二个下拉索引发生更改时,它不应触发回发(除非您以编程方式导致回发)。

我已将您的代码复制到我的解决方案中,它似乎表现...

<asp:Label ID="lblErr" runat="server"></asp:Label>
<asp:DropDownList ID="ddlTypePN" runat="server" EnableViewState="true"
    AutoPostBack="true" OnSelectedIndexChanged="ddlTypePN_SelectedIndexChanged"
    OnDataBound="ddlTypePN_DataBound">
</asp:DropDownList>

<asp:DropDownList runat="server" ID="ddlFctPN" AppendDataBoundItems="false" OnDataBound="ddlFctPN_DataBound">
</asp:DropDownList>

而且代码...

protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ListItemCollection items = new ListItemCollection();
                items.Add(new ListItem("PNT", "PNT"));
                items.Add(new ListItem("PNC", "PNC"));

                ddlTypePN.DataSource = items;
                ddlFctPN.DataBind();
                ddlTypePN.DataBind(); // The ddl that populate my 2nd ddl

                ddlTypePN.Items.Insert(0, new ListItem("Sélectionnez...", "null"));
            }
        }

        protected void ddlTypePN_SelectedIndexChanged(object sender, EventArgs e)
        {
            string type = ddlTypePN.SelectedValue.ToString().Trim();

            // if PNT
            if (type.ToUpper().Trim().Equals("PNT"))
            {
                ddlFctPN.Enabled = true;
                populateDdl();

            }
            else if (type.ToUpper().Trim().Equals("PNC"))
            {
                ddlFctPN.Enabled = true;
                populateDdl();
            }        
        }

        protected void ddlTypePN_DataBound(object sender, EventArgs e)
        {

        }

        protected void ddlFctPN_DataBound(object sender, EventArgs e)
        {

        }

        void populateDdl()
        {

            ddlFctPN.Items.Clear();
            lblErr.Visible = false;

            try
            {
                ListItemCollection items = new ListItemCollection();
                items.Add(new ListItem("One", "1"));
                items.Add(new ListItem("Two", "2"));
                items.Add(new ListItem("Three", "3"));

                ddlFctPN.DataSource = items;
                ddlFctPN.DataBind();
            }
            catch (Exception ex)
            {
                lblErr.Text = ex.Message;
                lblErr.Visible = true;
            }


            ddlFctPN.Items.Insert(0, new ListItem("Sélectionnez...", "null"));

        }

    }
于 2013-05-29T10:51:39.963 回答
1

将此代码置于此条件下

if(!Page.IsPostBack)
            {

       // Your Code Here

            }
于 2013-05-29T10:14:39.730 回答
1

你是每个回帖中的人口 DD1

避免这种使用

if(!IsPostBack)    
 {
   populateDdl();
 }
于 2013-05-29T10:28:38.783 回答
0

你可以使用CascadingDropDown像:

<ajaxToolkit:CascadingDropDown ID="CDD1" runat="server"
    TargetControlID="DropDownList2"
    Category="Model"
    PromptText="Please select a model"
    LoadingText="[Loading models...]"
    ServicePath="CarsService.asmx"
    ServiceMethod="GetDropDownContents"
    ParentControlID="DropDownList1"
    SelectedValue="SomeValue" />
于 2013-05-29T10:27:07.650 回答
0

就我而言,发生此问题是因为分配给下拉列表的 DataValueField 值为空。dropdownName.DataValueField 对于所有条目必须是唯一的。

于 2020-05-18T13:55:43.503 回答
-1

经过数小时试图找出类似的问题以及为什么我的下拉菜单不会改变后,我检查了我的数据,虽然 DataTextField 信息不同,但 DataTextValues 是相同的,每次都只是拉第一个。检查您的数据并查看它们是否具有不同的值。

于 2014-04-03T14:21:42.957 回答
-1

好的解决方案

<script type="text/javascript">
    function MtCambioRegion() {
        // con JQUERY



        //var regionId = $('#<%= ddlregion.ClientID %>').val();
        // sin JQUERY
        var regionId = document.getElementById('ContentBody_ddlRegion').value;
       //  var regionId = document.getElementById('ContentBody_CtrContenedoAjax_ddlRegion').value
       // alert('metodo region : ' + regionId );
        GetCitiesOfRegion(regionId);
    }

    function GetCitiesOfRegion(regionId) {
        // alert('Funcion ' + regionId );
        var actionData = "{'regionId': '" + regionId + "'}";
        $.ajax({
            type: "POST",
            url: "WebTespRegionComuna.aspx/GetProComunas",
            data: actionData,
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (r) {
                var ddlComuna = $("[id*=ddlComuna]");
                ddlComuna.empty().append('');

                $.each(r.d, function () {
                    ddlComuna.append($("<option></option>").val(this['id']).html(this['nombre']));
                });
            }
        });

    }

    function FnSelComuna() {
        var x = document.getElementById("ContentBody_ddlComuna").value;
        // alert(x);
        document.getElementById('ContentBody_txtComunaHiden').value = x;
    }

//公式aspx

Public Class WebTespRegionComuna
Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    If Not Page.IsPostBack Then
        'ControlSystema.GetSearhRegionList(ddlRegion)

        Dim _ControlSystema As New ControlSystema
        With _ControlSystema
            .MtRegionList(ddlRegion)
        End With
        _ControlSystema = Nothing


    End If

End Sub


<System.Web.Services.WebMethod()> _
Public Shared Function GetProComunas(regionId As String) As List(Of ComunaList)
    Dim _ControlSystema As New ControlSystema
    Dim _lista As List(Of ComunaList)
    With _ControlSystema
        _lista = .GetSearchComunaList(regionId)
    End With
    _ControlSystema = Nothing
    Return _lista
End Function

Private Sub btnGuardarDatos_Click(sender As Object, e As System.EventArgs) Handles btnGuardarDatos.Click
    Try
        Dim valorcomuna As String = ddlComuna.SelectedValue
        valorcomuna = txtComunaHiden.Text


        Dim valorregion As String = ddlRegion.SelectedValue.ToString()
        Dim _valor As String = "punto de quiebre"

    Catch ex As Exception

    End Try
End Sub End Class
于 2017-02-23T18:28:41.883 回答