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); }
线的位置错误
{
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); }
线的位置错误
这是找DateList里的textbox,你要先找到是哪一行,然后再找那一行里的textbox
改为TextBox1.Text = text;
试试
那时就可以这样写:
TextBox textbox1=e.Item.Findcontrol("textbox1") as TextBox;
textbox1.text=text.Substring(0,textlen);
应该这么写吧?
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里啊
查找textbox控件。
然后看看textlen是不是越界了
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;