ASPNET,通过select下拉列表框使文本框显示数据库数据

默然simple 2016-05-10 10:20:08
        private void GetDropList()
{
HttpContext context = HttpContext.Current;
string sql = "select id,type from breakDownReason ";
StringBuilder sb = new StringBuilder();
SqlDataReader dr = DBHelper.GetReader(sql);
while (dr.Read())
{
sb.Append("<option value ='" + dr["id"] + "'>" + dr["type"] + "</option>");
}
context.Response.Write(sb.ToString());
}
#region 根据id加载数据
private void getReason(int id)
{
HttpContext context = HttpContext.Current;
string sql = "SELECT contents FROM breakDownReason where id='" + id + "'";
SqlDataReader dr = DBHelper.GetReader(sql);
string txtContent = "";
while (dr.Read())
{
txtContent = dr["contents"].ToString();
}
context.Response.Write("{\"Content\":\"" + txtContent + "\",\"id\":\"" + id + "\"}");
}
#endregion


    window.onload=function()
{
$.ajax({
type: "POST",
dataType: "text",//
url: 'handler.ashx',
data: { action: "getDrop" },
success: function (b) {
$("#dropList").append(b);

}
});

}

//根据ID加载数据
function getReason(id) {
$.ajax({
type: "POST",
dataType: "json",
url: 'handler.ashx',
data: { action: "getReason", Id: id },
success: function (b) {
$("#txtContent").val(b.Content);
$("#hidId").val(b.id);
}
});
}


想选择下拉列表框,然后下面的文本框显示对应的数据,哪里需要改
...全文
666 11 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
默然simple 2016-05-10
  • 打赏
  • 举报
回复
引用 7 楼 u013085997 的回复:
[quote=引用 6 楼 tinydyw 的回复:] function getReason(id)这个方法并没看到你调用啊...
怎么调用,能麻烦举个例吗[/quote] 谢谢您耐心解答,经过点拨发现错误已经完成功能,谢谢
 function getReason(value) {
        var value = document.getElementById("dropList").value;
        $.ajax({
            type: "POST",
            dataType: "json",
            url: 'handler.ashx',
            data: { action: "getReason", id: value },
            success: function (b) {
                $("#txtContent").val(b.Content);
                $("#hidId").val(b.id);
            }
        });
    }
默然simple 2016-05-10
  • 打赏
  • 举报
回复
引用 8 楼 tinydyw 的回复:
比如像5楼说的,在select的onchange里,获取select的selectedindex,然后 getReason(selectedindex).. 你select之所以更改了,是因为你函数写在onload里 所以页面加载完毕就调用了...文本框的文字更改你只写了函数 然而并没有在任何地方调用,所以并没有执行....
谢谢您耐心解答,经过点拨发现错误已经完成功能,谢谢
 function getReason(value) {
        var value = document.getElementById("dropList").value;
        $.ajax({
            type: "POST",
            dataType: "json",
            url: 'handler.ashx',
            data: { action: "getReason", id: value },
            success: function (b) {
                $("#txtContent").val(b.Content);
                $("#hidId").val(b.id);
            }
        });
    }
可以了
默然simple 2016-05-10
  • 打赏
  • 举报
回复
引用 5 楼 hanjun0612 的回复:
[quote=引用 4 楼 u013085997 的回复:] [quote=引用 3 楼 hanjun0612 的回复:] 你不是绑定了吗?
但是下拉列表框选择了后,下面的文本框没跳出文字,我也不知道哪里没写好[/quote] 那你用 jquery,给 select控件增加 change事件,事件里给 文本框赋值就好了[/quote] 谢谢您耐心解答,经过点拨发现错误已经完成功能,谢谢
 function getReason(value) {
        var value = document.getElementById("dropList").value;
        $.ajax({
            type: "POST",
            dataType: "json",
            url: 'handler.ashx',
            data: { action: "getReason", id: value },
            success: function (b) {
                $("#txtContent").val(b.Content);
                $("#hidId").val(b.id);
            }
        });
    }
tinydyw 2016-05-10
  • 打赏
  • 举报
回复
比如像5楼说的,在select的onchange里,获取select的selectedindex,然后 getReason(selectedindex).. 你select之所以更改了,是因为你函数写在onload里 所以页面加载完毕就调用了...文本框的文字更改你只写了函数 然而并没有在任何地方调用,所以并没有执行....
默然simple 2016-05-10
  • 打赏
  • 举报
回复
引用 6 楼 tinydyw 的回复:
function getReason(id)这个方法并没看到你调用啊...
怎么调用,能麻烦举个例吗
tinydyw 2016-05-10
  • 打赏
  • 举报
回复
function getReason(id)这个方法并没看到你调用啊...
正怒月神 版主 2016-05-10
  • 打赏
  • 举报
回复
你不是绑定了吗?
正怒月神 版主 2016-05-10
  • 打赏
  • 举报
回复
引用 4 楼 u013085997 的回复:
[quote=引用 3 楼 hanjun0612 的回复:] 你不是绑定了吗?
但是下拉列表框选择了后,下面的文本框没跳出文字,我也不知道哪里没写好[/quote] 那你用 jquery,给 select控件增加 change事件,事件里给 文本框赋值就好了
默然simple 2016-05-10
  • 打赏
  • 举报
回复
引用 3 楼 hanjun0612 的回复:
你不是绑定了吗?
但是下拉列表框选择了后,下面的文本框没跳出文字,我也不知道哪里没写好
默然simple 2016-05-10
  • 打赏
  • 举报
回复
引用 1 楼 hanjun0612 的回复:
先查看前台生成的html是什么样子


本来用dropdownlist控件比较简单,但页面会回发。所以用select有点不会,问问哈
正怒月神 版主 2016-05-10
  • 打赏
  • 举报
回复
先查看前台生成的html是什么样子

62,244

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧