求高手解决一个很灵异的问题~~急~~~
我用UpdatePanel空间写了一个绑定DropDownList的功能,要求绑定 市 区 商圈,绑定没问题 运行没问题,但是点保存按钮的时候,DropDownList的SelectedIndexChanged事件会重新运行一遍,最后取到的结果是请选择,高手帮我看看咋回事,下面是代码。
HTML代码
<%@ Page Language="C#" MasterPageFile="~/Main.Master" EnableEventValidation="false" ResponseEncoding="GB2312" AutoEventWireup="true" CodeFile="PurchaseEdit.aspx.cs" Inherits="Personal_PurchaseEdit" Title="发布求购信息"%>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<script type="text/javascript" src="../js/calendar.js"></script>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td class="t_link"><ul><li>
<asp:Label ID="Label10" runat="server" Text="所在位置"></asp:Label>:
<a href="../Default.aspx" ><asp:Label ID="Label11" runat="server" Text="首 页" ></asp:Label></a> >>
<asp:Label ID="Label9" runat="server" Text="发布求购信息" ></asp:Label></li></ul></td>
</tr>
</table>
<table border="0" cellpadding="0" cellspacing="0" class="hmain00" align="center">
<tr>
<td class="hmain01_1">
<ul class="ttop02"><li></li></ul>
<ul class="hmain04">
<li style="width:358px;">
<asp:Label ID="Label1" runat="server" Text="地 区:" CssClass="hmtext80"></asp:Label>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager><asp:UpdatePanel id="UpdatePanel1" runat="server"><contenttemplate>
<asp:DropDownList id="ddlCity" runat="server" Width="70px" OnSelectedIndexChanged="ddlCity_SelectedIndexChanged" AutoPostBack="True">
</asp:DropDownList> <asp:DropDownList id="ddlTown" runat="server" Width="70px" AutoPostBack="True" OnSelectedIndexChanged="ddlTown_SelectedIndexChanged">
</asp:DropDownList>
<asp:DropDownList ID="ddlArea" runat="server" Width="70px">
</asp:DropDownList>
<span style="color: #ff0000">*</span>
</contenttemplate>
</asp:UpdatePanel>
<li style="width:475px;">
<asp:Label ID="Label4" runat="server" Text="关键字:" CssClass="hmtext80"></asp:Label>
<asp:TextBox ID="KeyWord1" runat="server"></asp:TextBox>
<asp:TextBox ID="KeyWord2"
runat="server"></asp:TextBox>
<asp:TextBox ID="KeyWord3" runat="server"></asp:TextBox><span style="color: #ff0000">*</span></li></ul>
<ul class="hmain05" style=" text-align:center;">
<li><span class="sbbg01"><asp:Button ID="btnSubmit" runat="server" OnClick="btnSubmit_Click" Text="提交" ValidationGroup="Add" /> <%-- <input id="Reset1" type="reset" value="重置" />--%>
</span></li>
</td>
</tr>
</table>
</asp:Content>
cs代码
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class Personal_PurchaseEdit : PROPERTY.Common.BasePage
{
private operDB odb = new operDB();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindToCityandTown();
}
}
protected void BindToCityandTown()
{
string Citystr = "select CCity from City";
SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["SQLCon"]);
SqlDataAdapter da = new SqlDataAdapter(Citystr, con);
DataSet ds = new DataSet();
da.Fill(ds);
this.ddlCity.DataSource = ds;
this.ddlCity.DataTextField = "CCity";
this.ddlCity.DataValueField = "CCity";
this.ddlCity.DataBind();
this.ddlCity.Items.Insert(0, "请选择 ");
}
protected void ddlCity_SelectedIndexChanged(object sender, EventArgs e)
{
string Citystr = "SELECT Borough.BBorough FROM Borough left JOIN City ON City.CCity = '" + ddlCity.SelectedValue.ToString() + "' right OUTER JOIN City AS City_1 ON Borough.Cid = City.Cid GROUP BY Borough.BBorough";
SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["SQLCon"]);
SqlDataAdapter da = new SqlDataAdapter(Citystr, con);
DataSet ds = new DataSet();
da.Fill(ds);
this.ddlTown.DataSource = ds;
this.ddlTown.DataTextField = "BBorough";
this.ddlTown.DataValueField = "BBorough";
this.ddlTown.DataBind();
this.ddlTown.Items.Insert(0, "请选择 ");
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
string hirestr = "insert into BuyHouse values('北京','" + ddlCity.SelectedValue.ToString() + "','" + ddlTown.SelectedValue.ToString() + "','" + ddlArea.SelectedValue.ToString() + "','" + TextBox1.Text.Trim().ToString() + "','" + TextBox2.Text.Trim().ToString() + "','" + txtArea.Text.Trim().ToString() + "',Convert(money,'" + txtPrice.Text.Trim().ToString() + "'),'" + Convert.ToDateTime(tbTime.Text.Trim().ToString()) + "','" + KeyWord1.Text.Trim().ToString() + "','" + KeyWord2.Text.Trim().ToString() + "','" + KeyWord3.Text.Trim() + "','1')";
if (!odb.FillDataGrid(hirestr))
{
Response.Write("<script>alert('添加错误!')</script>");
return;
}
}
protected void ddlTown_SelectedIndexChanged(object sender, EventArgs e)
{
string Citystr = "SELECT Area.AArea FROM Area LEFT OUTER JOIN Borough ON Borough.BBorough = '" + ddlTown.SelectedValue.ToString() + "' RIGHT OUTER JOIN Borough AS Borough_1 ON Borough.Bid = Area.Aid GROUP BY Area.AArea";
SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["SQLCon"]);
SqlDataAdapter da = new SqlDataAdapter(Citystr, con);
DataSet ds = new DataSet();
da.Fill(ds);
this.ddlArea.DataSource = ds;
this.ddlArea.DataTextField = "AArea";
this.ddlArea.DataValueField = "AArea";
this.ddlArea.DataBind();
this.ddlArea.Items.Insert(0, "请选择 ");
}
}
[解决办法]
参考下这个http://www.cnblogs.com/Terrylee/archive/2006/11/01/ASPNET_AJAX_UpdatePanle_Part2.html
[解决办法]
ddlTown.SelectedValue.ToString()
注意在tosting以前请判断是否为null
还有!!!!
this.ddlArea.Items.Insert(0, "请选择 ");
是没有selectValue的及为null
[解决办法]
单步调试下看看~
[解决办法]
断点调试!
[解决办法]
友情UP