【问题标题】:Asp.net databind to htmlselect with ajax使用 ajax 将 Asp.net 数据绑定到 htmlselect
【发布时间】:2017-01-07 20:02:31
【问题描述】:

您好,我知道 HTMLSELECT 元素具有数据源和数据绑定属性。那么你们能给我一个在 asp.net 中使用 Ajax POST 将数据绑定到 HTMLSELECT 的示例吗?

编辑

好的,伙计们,我会解释一下我想做什么。

场景如下:

是的,我正在使用 Web 表单和实体框架。我有一个 linq 查询,它给我一个列表。这个查询需要一个值,该值将来自 AJAX POST 到静态函数。

在代码后面的这个静态函数中,我使用 AJAX 从页面的 TEXTBOX 中获取了一个值,并且我能够做到这一点。

但问题是:

在静态方法中,我需要将 HTMLSELECT 元素的数据源绑定到 linq 查询。那我该怎么做呢?

【问题讨论】:

  • 我假设这是在引用 aspx 页面上的服务器控件。如果你是数据绑定,为什么需要ajax调用?

标签: asp.net ajax data-binding


【解决方案1】:

正如 Bindrid 从您的问题上下文中提到的,听起来您正在使用网络表单? aspx 页面?通常这涉及服务器控制。所以在典型的场景中,你会数据绑定你的服务器控件。如果您有兴趣在不刷新整个页面的情况下与您的服务器控件交互,您通常使用 asp.net 更新面板。这并不是说不能在 Web 表单页面中使用 ajax 调用来补充您的 html 选择(我经常使用 Web 方法或 Web api 调用来执行此操作),但这不是通常用于 Web 表单的方式。下面是一个数据绑定下拉服务器端控件的示例。

aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication3.WebForm1" EnableViewState="true" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:DropDownList ID="DropDownList1" runat="server"></asp:DropDownList>  
    </div>
    </form>
</body>
</html>

后面的代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication3
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            dropDownInfo dd1 = new dropDownInfo()
            {
                val = "1",
                txt = "one"
            };
            dropDownInfo dd2 = new dropDownInfo()
            {
                val = "2",
                txt = "two"
            };
            dropDownInfo dd3 = new dropDownInfo()
            {
                val = "3",
                txt = "three"
            };
            List<dropDownInfo> list = new List<dropDownInfo>();
            list.Add(dd1);
            list.Add(dd2);
            list.Add(dd3);

            DropDownList1.DataSource = list;
            DropDownList1.DataValueField = "val";
            DropDownList1.DataTextField = "txt";
            DropDownList1.DataBind();
        }


    }

    internal class dropDownInfo
    {
        public string val{ get; set; }
        public string txt { get; set; }

    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多