如何解决如何使用内部代码中的静态方法在内部DropboxList中显示数据来填充dropdownlist
想法是用数据库中的信息,已调试的代码以及信息(如果存储在下拉列表中)填充下拉列表,问题是它看不到任何建议。
我不会使用更新面板。
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个用户打开了页面,并且方法显示“更新此控件”,则应该更新哪个页面上的哪个下拉列表?这是不可能知道的。这就是为什么必须将数据发送回调用它的页面上的脚本的原因。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。