我有一个用于“创建新配置文件”的表格。在这种形式下,我有一些DropDownLists和TextBoxes,我的问题是关于DropDownLists。
4个DropDownLists在我的页面上。
让我们关注最后2个DropDownLists:
第一个DropDown在其值的函数中动态填充第二个DropDown。
看这张图片:http://image.noelshack.com/fichiers/2013/22/1369819471-picture-help.png
第一个ddl:
<asp:DropDownList ID="ddlTypePN" runat="server" DataSourceID="SqlTypePN" EnableViewState="true"
DataTextField="libelle" DataValueField="valeur" AutoPostBack="true" OnSelectedIndexChanged="ddlTypePN_SelectedIndexChanged"
OnDataBound="ddlTypePN_DataBound" > </asp:DropDownList>
第二个ddl:
<asp:DropDownList runat="server" ID="ddlFctPN" AppendDataBoundItems="false" OnDataBound="ddlFctPN_DataBound" > </asp:DropDownList>
填充方法:
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"));
}
[当我在第二个ddl中选择一个项目并且发生了PostBack(即使它来自我之前提到的其他下拉列表)时,SelectedValue成为第一个值。 (“ Selectionnez ...”)
似乎我的第二个DropDownList在每次回发中都受到限制,即使不是因为我的第一个DropDownList的SelectedIndexChanged也是如此。第一个DropDownList的SelectedIndexChanged总是在回发时调用,因此它在每个回发时都引发“ populateDdl()”(如果选择了一个值)。
当我单击提交按钮时,它将在我的数据库中注册一个空白值。
我想念什么?
这里是PageLoad:
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
}
}
这是第一个ddl SelectedIndexChanged:
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();
}
}
请参见下图:
http://image.noelshack.com/fichiers/2013/22/1369830738-help2.png
[当我单击提交按钮时,您看到我的第二个ddl(“ Fonction”)已正确填充:但该值变为空值(“Sélectionnez...”),因此我的RequiredFieldValidator使该页面无效!
if(!IsPostBack)
{
populateDdl();
}
在此条件下输入此代码
if(!Page.IsPostBack)
{
// Your Code Here
}
您在每个回发区中都是DD1人口
为避免这种使用
if(!IsPostBack)
{
populateDdl();
}
从您的标记开始,您还没有在第二个下拉菜单中设置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"));
}
}
U可以使用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" />
解决方案从问题中移出答案:
我终于找到了问题所在。每个控件都位于
<asp:Table>
中,我必须在此表上设置EnableViewState="true"
,以便能够在回发后保留该值。
[经过数小时的尝试来找出类似的问题以及为什么下拉列表不会更改后,我检查了我的数据,尽管DataTextField信息不同,但DataTextValues相同,并且每次仅提取第一个。检查您的数据,看看它们是否具有不同的值。
ok solucion
<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;
}
// Formulario 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