private void fy()
    {
        string connstring = ConfigurationManager.ConnectionStrings["citywalksConnectionString6"].ConnectionString;
        SqlConnection con = new SqlConnection(connstring);
        SqlDataAdapter sda = new SqlDataAdapter("SELECT [content] FROM [news]", con);
        DataSet ds = new DataSet();
        sda.Fill(ds, "ncontent2");
        text = ds.Tables["ncontent2"].Rows[0][0].ToString();
        textlen = text.Length;
        if ((textlen > 1000) &&( textlen < 2000)) 
        {
            prevlen = text.LastIndexOf("p>", 1000) + 2;
            latelen = textlen - prevlen;
            TextBox TextBox1 = (TextBox)DataList.FindControl("TextBox1");
            TextBox1.Text = text.Substring(0, prevlen);
        }
        else
        {
            TextBox TextBox1 = (TextBox)DataList.FindControl("TextBox1");
            TextBox1.Text = text.Substring(0,textlen);        }
线的位置错误

解决方案 »

  1.   

    TextBox1这个没找到吧,debug看看呢
      

  2.   

    (TextBox)DataList.FindControl("TextBox1"); 
    这是找DateList里的textbox,你要先找到是哪一行,然后再找那一行里的textbox
      

  3.   

    TextBox1.Text = text.Substring(0,textlen);       
    改为TextBox1.Text = text;
    试试
      

  4.   

    text的值可能为空,为空时会出错,最好先判断一下
      

  5.   

    text取出来是什么先看看,很可能没取到值
      

  6.   

    我觉得你Else里面的那两句话应该放在DataList的RowDataBound事件里需去处理
    那时就可以这样写:
    TextBox textbox1=e.Item.Findcontrol("textbox1") as TextBox;
    textbox1.text=text.Substring(0,textlen);       
      

  7.   

    TextBox TextBox1 = (TextBox)DataList.Items[i].FindControl("TextBox1"); //这里的i值根据你的实际要求获取
    应该这么写吧?
      

  8.   

    似乎是没找到, 放到里边确实是解决了,可还用个相似的怎么弄啊
    if (e.Item.ItemType == ListItemType.Footer)
            {            Button Button1 = (Button)e.Item.FindControl("Button1");
                Button1.Attributes.Add("onclick", "Button1_Click()");
                Button Button2 = (Button)e.Item.FindControl("Button2");
                Button2.Attributes.Add("onclick", "Button2_Click()");

    protected void Button1_Click(object sender, EventArgs e)
        {
            TextBox TextBox1 = (TextBox)FindControl("TextBox1");
            TextBox1.Text = text.Substring(prevlen, latelen);
        }    protected void Button2_Click(object sender, EventArgs e)
        {
            TextBox TextBox1 = (TextBox)FindControl("TextBox1");
            TextBox1.Text = text.Substring(0, prevlen);
        }

    事件也犯同样的毛病,但还不好放到DataList_ItemDataBound里啊
      

  9.   

    没有找到textbox 控件 ,如果textbox控件在datalist的话,你最好需要遍历一下datalist或是在datalist的row事件
    查找textbox控件。
      

  10.   

    先看text的值
    然后看看textlen是不是越界了
      

  11.   

      
    text = ds.Tables["ncontent2"].Rows[0][0].ToString(); 此句代码中text没有获取到值,初步判断为数据库中没有记录!可考虑用此方法试下:string connstring = ConfigurationManager.ConnectionString["citywalksConnectionString6"].ConnectionString; 
         SqlConnection con = new SqlConnection(connstring); 
        SqlCommand com=new SqlCommand("SELECT [content] FROM [news]",con);
        SqlDataReader sdr=com.ExecuteNonQuery();
        if(sdr.Read())
            {
                 text =sdr[字段名].ToString();
             }
         sdr.Colse;
         con.Colse;
      

  12.   

    (TextBox)DataList1.Item.FindControl("TextBox1")
      

  13.   

    TextBox TextBox1 = (TextBox)DataList.Items[i].FindControl("TextBox1");