Asp.net ajax 1.0 绑定drowdownlist时取值问题

今天同事使用了很久远的一个东西。。asp.net ajax 1.0,然后使用了 updatepanel来实现drowdownlist的联动,结果出了问题,有个ddl取值selectitem.text总是默认值,于是乎让我帮忙,本想说让同事用其它类似jquery的库,但是同事在编程方面属于刚入门,但是活儿却要急着出来,唉,用了就用了吧,可是咱一过去帮忙调试,调试了1个半点,才把问题找出来。。。。期间google了问题,但是答案都没有比较明确的解释。。可能是这个东西(asp.net ajax)真的该被抛弃了吧,但是对于新手要出活儿来说确实还是有点实用的。。。所以在此记录下来吧

在updatepanel中的drowdownlist是使用数据源绑定过去的,但是不管怎么设置,类似autopostback,ispostback啊什么之类的updatepanel的postmodel啊检查了没问题啊,但是就是不管事儿。调试时断点内的ddl的selectitem.text的值总是默认值。。。。郁闷啊。。。,这时想着用其它方式解决吧。。。。于是乎使用了

Request["clientId"].ToString()

来取值,或者在页面内放置一个Hidden,然后用javascript来更新hidden的值,在后台取这个hidden的值,这个时候问题能解决,但是本着找出问题的原因的精神,我们来探索原来哪里写的有问题,没道理的啊。

经过了一段时间的排查,发现了同事绑定drowdownlist控件时,在绑定完时再

drowdownlist.item.Insert(0,new ListItem("全部","0"));

,而且只指定了 DataTextField而没有指定 DataValueField;

然后我们就把DataValueField加上,这个时候终于找到原因了,对,就是drowdownlistlist在使用updatepanel来异步操作时,value值必须是唯一的,否则text值会有问题,其实这个问题不管是不是使用updatepanel都应当值的注意,因为 drowdownlist生成的select页面控件的value就是一个唯一值,你绑定时若不指定,它所有的value应该都是空,这个时侯是取不到selectitem的,包括selectindex都取不到。

由于有园友提出了想知道具体环境,这里给出具体代码

Aspx部分

View Code
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" EnableEventValidation="false"
Inherits
="TestNet2._0._Default"%>

<%@ Register Assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
Namespace
="System.Web.UI" TagPrefix="asp"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>test</title>
<script type="text/javascript">
window.onload
= function () {
alert(
"已回发");
}
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="up1" runat="server" UpdateMode="Always">
<ContentTemplate>
<asp:DropDownList ID="ddl1" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddl1_SelectChanged">
</asp:DropDownList>
<asp:Label ID="lbl1" runat="server"></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
</form>
</body>
</html>

CS部分

View Code
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace TestNet2._0
{
publicpartialclass _Default : System.Web.UI.Page
{
protectedvoid Page_Load(object sender, EventArgs e)
{
if (!IsPostBack) {
BindList();
}
}

privatevoid BindList()
{
DataTable dt
=new DataTable();
dt.Columns.Add(
"Name");
for (int i =0; i <10; i++) {
DataRow dr
= dt.NewRow();
dr[
"Name"] ="测试"+ i.ToString();
dt.Rows.Add(dr);
}

this.ddl1.DataSource = dt;
this.ddl1.DataTextField ="Name";
this.ddl1.DataBind();

this.ddl1.Items.Insert(0, new ListItem("全部", "0"));
}

protectedvoid ddl1_SelectChanged(object sender,EventArgs e)
{
this.lbl1.Text = ddl1.SelectedItem.Text +":"+ ddl1.SelectedIndex.ToString();
}
}
}

web.config部分

View Code
<?xml version="1.0"?>
<configuration>
<appSettings/>
<connectionStrings/>
<system.web>
<httpHandlers>
<remove verb="*" path="*.asmx"/>
<add verb="*" path="*.asmx" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
<add verb="*" path="*_AppService.axd" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
<add verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" validate="false"/>
</httpHandlers>
<httpModules>
<add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
</httpModules>
<compilation debug="true">
<assemblies>
<add assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
</assemblies>
</compilation>
<authentication mode="Windows"/>
<globalization requestEncoding="GB2312" responseEncoding="GB2312" fileEncoding="GB2312" culture="zh-CN" uiCulture="zh-CN"/>
</system.web>
</configuration>

最后在自己测试过程中发现出现这个问题的真正原因:

由于在web.config中指定了页面编码"gb2312",而 asp.net ajax1.0使用的是utf-8的编码,回传数据时如果数据带有中文字符,则会出现乱码,乱码情况可以通过调试使用request[“clientid”]发现,因此则不能识别到item的值,[因此只要将编码设置为utf-8即可,只设置DataTextField也是可以取得值的]。

posted @ 2011-04-18 21:18  Bodil  阅读(356)  评论(2编辑  收藏  举报