问题描述
想法是用数据库中的信息,已调试的代码以及信息(如果存储在下拉列表中)填充下拉列表,问题是它看不到任何建议。
我不会使用更新面板。
javascript函数正在后面的代码中调用方法,唯一的问题是我看不到信息
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<div class="grid3">
<asp:Label runat="server" Text="Provincia"></asp:Label>
<asp:DropDownList ID="droplistProvincia" ClientIDMode="Static" AutopostBack="false" OnSelectedindexChanged="droplistProvincia_SelectedindexChanged" onclick="" Width="100%" runat="server" Height="30px">
<asp:ListItem Text="Seleccione"></asp:ListItem>
</asp:DropDownList>
<asp:Label runat="server" Text="Cantón"></asp:Label>
<asp:DropDownList ID="droplistCanton" CssClass="item15" AutopostBack="true" OnSelectedindexChanged="droplistCanton_SelectedindexChanged" Width="100%" runat="server" Height="30px">
<asp:ListItem Text="Seleccione"></asp:ListItem>
</asp:DropDownList>
<asp:Label runat="server" CssClass="item16" Text="distrito"></asp:Label>
<asp:DropDownList ID="droplistdistrito" CssClass="item17" runat="server" Height="30px">
<asp:ListItem Text="Seleccione"></asp:ListItem>
</asp:DropDownList>
<div runat="server" id="checkProvincia" class="mensaje req6 diseñoReqObli">
<div runat="server" id="mensaje6" class="iconoMensaje">
<i class="fa fa-exclamation-circle fa-2x" aria-hidden="true" ></i>
</div>
<div runat="server" id="txtMensaje6" style="margin-top:5px;"></div>
</div>
<div runat="server" id="checkCanton" class="mensaje req7 diseñoReqObli">
<div runat="server" id="mensaje7" class="iconoMensaje">
<i class="fa fa-exclamation-circle fa-2x" aria-hidden="true" ></i>
</div>
<div runat="server" id="txtMensaje7" style="margin-top:5px;"></div>
</div>
<div runat="server" id="checkdistrito" class="mensaje req8 diseñoReqObli">
<div runat="server" id="mensaje" class="iconoMensaje">
<i class="fa fa-exclamation-circle fa-2x" aria-hidden="true" ></i>
</div>
<div runat="server" id="txtMensaje8" style="margin-top:5px;"></div>
</div>
</div>
</ContentTemplate>
</asp:UpdatePanel>
脚本
provincia.addEventListener('change',(e) => {
e.preventDefault();
PageMethods.Provincia(provincia.value);
console.log(provincia.value);
})
后面的代码
[System.Web.Services.WebMethod()]
[System.Web.Script.Services.ScriptMethod()]
public static void Provincia(string dato)
{
MessageBox.Show(dato);
DropDownList dropCanton = new DropDownList();
dropCanton = HttpContext.Current.Session["dropCanton"] as DropDownList;
if (dato != "Seleccione")
{
int idProvincia = int.Parse(dato);
_servicio2 = _servicio2 ?? new Servicio<Canton>();
dropCanton.DataTextField = "nom_Canton";
dropCanton.DataValueField = "cod_Canton";
dropCanton.DataSource = _servicio2.TraerTodoEspecificopor(new Query<Canton> { Where = x => x.cod_Provincia == idProvincia });
dropCanton.DataBind();
dropCanton.Items.Insert(0,"Seleccione");
dropCanton.Selectedindex = 0;
}
}
Page_load
protected void Page_Load(object sender,EventArgs e)
{
Session["dropCanton"] = this.droplistCanton;
}
解决方法
页面方法的一个常见误解:您不能从静态WebMethod / Page方法访问页面上的控件。您必须将数据返回到调用该方法的javascript,然后从javascript方法更新控件(下拉列表)。
由于该方法是静态的,因此在每个页面上都是相同的方法。如果您站点上的4个用户打开了页面,并且方法显示“更新此控件”,则应该更新哪个页面上的哪个下拉列表?这是不可能知道的。这就是为什么必须将数据发送回调用它的页面上的脚本的原因。