当前位置:编程学习 > C#/ASP.NET >>

dropdownlist总是读取的是第一个值

 protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.Cookies["c"] == null)
        {
            Response.Redirect("login.aspx");
        }
        if (!Page.IsPostBack)
        {
            Bindddlpro();
            Bindddlcity(ddlpro.SelectedValue);
            string sql = "select * from com_info where username='" + Session["username"].ToString() 

+ "'";
            cls cls = new cls();
            SqlDataReader sdr = cls.ExcuteDataReader(sql);
            if (sdr.Read())
            {
                Bindddlpro();
                Bindddlcity(ddlpro.SelectedValue);
                ListItem item = null;
                item = ddlcity.Items.FindByValue(sdr["city"].ToString());
                if (item != null)
                {
                    item.Selected = true;
                }
                string name = sdr["city"].ToString();
                Bindddlhangye();
                ListItem item1 = null;
                item1 = ddlhangye.Items.FindByValue(sdr["hangye"].ToString());
                if (item1 != null)
                {
                    item1.Selected = true;
                }
            }
            cls cls1 = new cls();
            SqlDataReader sdr1 = cls1.ExcuteDataReader(sql);
            if (sdr1.Read())
            {
                txtcom_name.Text = sdr1["com_name"].ToString();
                txtwangzhi.Text = sdr1["wangzhi"].ToString();
                ddlhangye.SelectedValue = sdr1["hangye"].ToString();
                ddlpro.SelectedValue = sdr1["pro"].ToString();
                ddlcity.SelectedValue = sdr1["city"].ToString();
                txtjianjie.Text = HTMLEncode1(sdr1["jianjie"].ToString());
                txtjyxm.Text = sdr1["jyxm"].ToString();
                txtlianxiren.Text = sdr["lianxiren"].ToString();
                txttel.Text = sdr["tel"].ToString();
                txtchuanzhen.Text = sdr["chuanzhen"].ToString();
                txtEmail.Text = sdr["Email"].ToString();
                txtaddress.Text = sdr1["address"].ToString();
            }
            if (txtcom_name.Text == "")
            {
                Response.Write("<script>alert('您还没有填写企业信

息!');window.location='index.aspx';</script>");
            }
        }
    }
    //绑定省
    private void Bindddlpro()
    {
        string sql = "select * from pro";
        cls cls = new cls();
        DataTable dt = cls.ExcuteDataTable(sql);
        ddlpro.DataSource = dt;
        ddlpro.DataTextField = "name";
        ddlpro.DataValueField = "d_id";
        ddlpro.DataBind();
        Bindddlcity("0");
    }
    //绑定城市
    private void Bindddlcity(string id)
    {
        string sql = "select * from city where d_id='" + id + "'";
        cls cls = new cls();
        DataTable dt = cls.ExcuteDataTable(sql);
        ddlcity.DataSource = dt;
        ddlcity.DataTextField = "city_name";
        ddlcity.DataValueField = "id";
        ddlcity.DataBind();
    }
    //绑定行业
    private void Bindddlhangye()
    {
        string sql = "select * from hangye";
        cls cls = new cls();
        DataTable dt = cls.ExcuteDataTable(sql);
        ddlhangye.DataSource = dt;
        ddlhangye.DataTextField = "hy_name";
        ddlhangye.DataValueField = "id";
        ddlhangye.DataBind();
    }
}



市读取的是第一个值,如何能读取到数据库里的值
--------------------编程问答-------------------- if (!Page.IsPostBack) 
        { 
            Bindddlpro(); 
            Bindddlcity(ddlpro.SelectedItem.Value.ToString());   
            string sql = "select * from com_info where username='" + Session["username"].ToString() 

+ "'"; 



还有ddlpro这个控件AutoPostBack="True"了吗
--------------------编程问答-------------------- 没给定 SELECTEDVALUE 或selectedindex 当然就是第一个了 --------------------编程问答-------------------- up --------------------编程问答-------------------- AutoPostBack="True"了
补充:.NET技术 ,  C#
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,