using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Data.OleDb;
using System.IO;namespace WebApplication2
{
/// <summary>
/// WebForm1 的摘要说明。
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid DataGrid1;
protected System.Web.UI.WebControls.TextBox TextBox1;
protected System.Web.UI.WebControls.Button Button1;
SqlConnection myCon;
protected System.Web.UI.WebControls.DropDownList DropDownList1;
protected System.Web.UI.WebControls.DropDownList DropDownList2;
int aa;
private void dis()
{
myCon=new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["con"]);
myCon.Open();//打开数据库
////DataGrid1数据绑定
string sql1="select zhanghao_id as 游戏帐号,zhanghao_mima as 密码 from table_zhanghao";
System.Data.SqlClient.SqlDataAdapter myDa=new SqlDataAdapter(sql1,myCon);
DataSet ds=new DataSet();
myDa.Fill(ds,"table_zhanghao");
this.DataGrid1.DataSource=ds.Tables["table_zhanghao"];
//this.DataGrid1.CurrentPageIndex=0;
this.DataGrid1.DataBind();
myCon.Close();
}
private void dis1()
{
myCon=new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["con"]);
myCon.Open();//打开数据库
string sql1="select zhanghao_id from table_zhanghao";
System.Data.SqlClient.SqlCommand mycom=new SqlCommand (sql1,myCon);
System.Data.SqlClient.SqlDataReader mydr=mycom.ExecuteReader();
this.DropDownList1.Items.Clear();
while(mydr.Read())
{
this.DropDownList1.Items.Add(mydr.GetString(0));
}
myCon.Close();
}
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
//if(!this.IsPostBack)
//{
dis();
dis1(); //}
} #region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{    
this.DataGrid1.ItemCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_ItemCommand);
this.DataGrid1.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.DataGrid1_PageIndexChanged);
this.DropDownList1.SelectedIndexChanged += new System.EventHandler(this.DropDownList1_SelectedIndexChanged);
this.Load += new System.EventHandler(this.Page_Load); }
#endregion private void DataGrid1_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
this.DataGrid1.CurrentPageIndex=e.NewPageIndex; this.DataGrid1.DataBind();
//dis();

} private void DataGrid1_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
if(e.CommandName=="Select")
{
object oo=e.Item.Cells[1].Text.ToString().Trim();
this.TextBox1.Text=oo.ToString().Trim();
}
} private void DropDownList1_SelectedIndexChanged(object sender, System.EventArgs e)
{
this.TextBox1.Text=this.DropDownList1.SelectedValue;
myCon=new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["con"]);
myCon.Open();//打开数据库
////DataGrid1数据绑定
string sql1="select zhanghao_mima  from table_zhanghao where zhanghao_id='"+this.DropDownList1.SelectedValue+"'";
System.Data.SqlClient.SqlCommand mycom=new SqlCommand (sql1,myCon);
System.Data.SqlClient.SqlDataReader mydr=mycom.ExecuteReader();
this.DropDownList2.Items.Clear();
while(mydr.Read())
{
this.DropDownList2.Items.Add(mydr.GetString(0));
}

myCon.Close();

}
}
}大家看上面的程序,有个问题,就是选择了DropDownList1后,为什么DropDownList1又多了一次加载,如果把DropDownList1,加载的时候CLEAR,但又不能选择起了,这是为什么????????

解决方案 »

  1.   

    private void Page_Load(object sender, System.EventArgs e)
    {
    // 在此处放置用户代码以初始化页面
    if(!this.IsPostBack)
    {
    dis();
    dis1(); }
    }
      

  2.   

    Page_Load
    {
    if(!IsPostBack)
    {
    看的我眼花!
    }
    }
      

  3.   

    if(!this.IsPostBack)
    {
    dis();
             dis1();}服务器控件要回发的哦!~
      

  4.   

    第一次加载一定要写在
    if(!IsPostBack)
    里面,切记切记
      

  5.   

    哈哈哈哈...我从来不看的,都是通过大家的回答,了解他想问的问题的,再去看关键的部分,哈哈哈哈....
    应该是IsPostBack的问题
      

  6.   

    if(!this.IsPostBack)
    ???你为什么要去掉呢?加上就可以了