((Label)this.dl.FindControl("Label1")).Text = "....";

解决方案 »

  1.   

    我是这么写的,但是当我运行的时候,他出错,要求使用new创建对象实例,调用方法前检查对象是否为空,获取异常帮助
    ((Label)this.DataList1.FindControl("Label35")).Text = "abcdefg";
    请问我该怎么处理,谢谢
      

  2.   

    你的datalist中绑定数据了么?先帮定数据再看看~
      

  3.   

    代码如下:
    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 Admin_sel_jjz_jjfw : System.Web.UI.Page
    {
        public SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["SQLCONNECTIONSTRING"].ConnectionString);
        public string zmlm = null;    protected void Page_Load(object sender, EventArgs e)
        {
            string info = "";
            info += "function getData()";
            info += "{";
            info += "var province=document.getElementById('rq');";
            info += "var pindex = province.selectedIndex;";
            info += "var pValue = province.options[pindex].value;";
            info += "var pText  = province.options[pindex].text;";
            info += "document.getElementById('rq').innerText=pText;";
            info += "}";
            this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "LinkageDDList", info, true);
            if (!this.Page.ClientScript.IsClientScriptIncludeRegistered(this.GetType(), "Date"))
            {
                //动态注册 js
                this.Page.ClientScript.RegisterClientScriptInclude(this.GetType(), "Date", "scripts/Date.js");
            }
            this.TextBox5.Attributes.Add("onclick", "setday(this,document.getElementById('" + this.TextBox5.ClientID + "'))");
            this.TextBox6.Attributes.Add("onclick", "setday(this,document.getElementById('" + this.TextBox6.ClientID + "'))");        if (!IsPostBack)
            {
                DropDownList2.Items.Clear();
                //SqlConnection con = new SqlConnection("server=192.168.0.4;uid=jdc;pwd=jdc;database=jdc");
                con.Open();
                /*
                SqlDataAdapter da = new SqlDataAdapter("select * from kcjjzjjsjb", con);
                DataSet ds = new DataSet();
                da.Fill(ds, "kcjjzjjsjb");
                DataList1.DataSource = ds.Tables[0].DefaultView;
                DataList1.DataBind();
                 */
                //绑定集结站
                DataSet ds_jjz = new DataSet();
                SqlDataAdapter da_jjz = new SqlDataAdapter("select * from zmzd where czxz='编组站' or czxz='区段站' order by czxz", con);
                da_jjz.Fill(ds_jjz, "zmzd");
                DropDownList1.DataSource = ds_jjz.Tables["zmzd"].DefaultView;
                DropDownList1.DataTextField = "hzzm";
                DropDownList1.DataBind();            //选择集结范围
                string sql_jjfw = "select * from zmzd order by czxz";
                SqlCommand mycmd = new SqlCommand(sql_jjfw, con);
                SqlDataReader myreader = mycmd.ExecuteReader();
                while (myreader.Read())
                {
                    DropDownList2.Items.Add(myreader["hzzm"].ToString().Trim() + myreader["czxz"].ToString().Trim());
                }
                con.Close();
            }
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            //ListBox1.Items.Remove(ListBox1.SelectedIndex);
            if (ListBox1.SelectedIndex > -1)
            {
                ListBox1.Items.RemoveAt(ListBox1.SelectedIndex);
            }
        }
        protected void Button3_Click(object sender, EventArgs e)
        {
            Label229.Text="";
            if ((Equals(TextBox3.Text.Trim(),string.Empty))||(Equals(TextBox4.Text.Trim(),string.Empty)))
            {
                Label229.Text="运行时间和挂运次数不能为空!!!";
            }
            else
            {
                ListBox1.Items.Add(DropDownList2.Text.Trim() + " " + TextBox3.Text.Trim() + " " + TextBox4.Text.Trim());
                TextBox3.Text = "";
                TextBox4.Text = "";
            }
        }
        protected void Button4_Click(object sender, EventArgs e)
        {
            ListBox1.Items.Clear();
        }
        protected void Button2_Click(object sender, EventArgs e)
        {
            Label229.Text = "";
            if (Equals(TextBox5.Text.Trim(), string.Empty))
            {
                Label229.Text = "起始日期不能为空!!!";
            }
            else if (Equals(TextBox6.Text.Trim(), string.Empty))
            {
                Label229.Text = "结束日期不能为空!!!";
            }
            else if (Equals(TextBox1.Text.Trim(), string.Empty))
            {
                Label229.Text = "棚车百分比不能为空!!!";
            }
            else if (Equals(TextBox2.Text.Trim(), string.Empty))
            {
                Label229.Text = "平车百分比不能为空!!!";
            }
            else if (ListBox1.Items.Count<=0)
            {
                Label229.Text = "请选择集结范围!!!";
            }
            else
            {
                Label229.Text = "正在生成集结时间表,请稍候...";
                //this.csh_jjzjjfw();
                //this.get_zmlm();
                //this.ins_jjz();
                this.datalist_db();
                //((Label)this.dl.FindControl("Label1")).Text = "....";
                ((Label)this.DataList1.FindControl("Label35")).Text = "abcdefg";
            }
        }    //获取站名略码
        public void get_zmlm()
        {
            con.Open();
            string sql = "select zmlm from zmzd where hzzm='" + DropDownList1.Text.Trim() + "'";
            SqlCommand mycmd = new SqlCommand(sql, con);
            SqlDataReader myreader = mycmd.ExecuteReader();
            while (myreader.Read())
            {
                zmlm = myreader["zmlm"].ToString().Trim();
            }
            con.Close();
        }    //集结站入库
        public void ins_jjz()
        {
            con.Open();
            string sql = "insert into jjz (zmhz,zmlm) values ('" + DropDownList1.Text.Trim() + "','" + zmlm.Trim() + "')";
            SqlCommand mycom = new SqlCommand(sql, con);
            int i = mycom.ExecuteNonQuery();
            con.Close();
            Label229.Text = Convert.ToString(i) + "条集结站记录插入成功!!!";
        }    //生成集结站集结时间表初始化
        public void csh_jjzjjfw()
        {
            string sql_jjz = "delete from jjz";
            con.Open();
            SqlCommand jjz_com = new SqlCommand(sql_jjz, con);
            int i = jjz_com.ExecuteNonQuery();
            //con.Close();
            string sql_jjfw = "delete from jjfw";
            //con.Open();        SqlCommand jjfw_com = new SqlCommand(sql_jjfw, con);
            int ii=jjfw_com.ExecuteNonQuery();        string sql_kcjjzjjsjb = "delete from kcjjzjjsjb";
            SqlCommand kcjjzjjsjb_com = new SqlCommand(sql_kcjjzjjsjb, con);
            int iii = kcjjzjjsjb_com.ExecuteNonQuery();
            con.Close();
            Label229.Text="集结范围时间表初始化成功!!!";
        }    //datalist数据绑定
        public void datalist_db()
        {
            con.Open();
            SqlDataAdapter da = new SqlDataAdapter("select * from kcjjzjjsjb", con);
            DataSet ds = new DataSet();
            da.Fill(ds, "kcjjzjjsjb");
            DataList1.DataSource = ds.Tables[0].DefaultView;
            DataList1.DataBind();
            con.Close();
        }    protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
        {
            Label la35 = (Label)this.DataList1.Items[e.Item.ItemIndex].FindControl("label35");
            la35.Text = "abcdefg";
        }
    }