public partial class manage_editnews : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Session["username"] == null)
{
Response.Redirect("~/login.aspx");
}
if (!IsPostBack)
{
band();
bindgrid();
}
}
private void band()//---绑定dripdownlist控件数据,用于绑定新闻类型
{
SqlConnection con = conn.createconn();
con.Open();
string aa = "select typename from newstype";
SqlDataAdapter sda = new SqlDataAdapter();
sda.SelectCommand = new SqlCommand(aa, con);
DataSet ds = new DataSet();
sda.Fill(ds);
this.droptype.DataSource = ds;//绑定dripdownlist
this.droptype.DataTextField = "typename";
this.droptype.DataValueField = "typename";
this.droptype.DataBind();
}
private void bindgrid()
{
SqlConnection con = conn.createconn();//---绑定GridView1控件数据,用于绑定新闻类型
con.Open();
string aa = "select adid, adname, adsubject, adtype, adcontent, adhit, addata from news";
SqlDataAdapter sda = new SqlDataAdapter();
sda.SelectCommand = new SqlCommand(aa, con);
DataSet ds = new DataSet();
sda.Fill(ds);
this.GridView1.DataSource = ds;
this.GridView1.DataBind();
con.Close();
}
public string GetContent(string str)
{
int i = str.Length;
string substr =str.Substring(0,5)+"....";
//在这里出错:用户代码未处理ArgumentOutOfRangeException return substr ;
}
protected void btnedit_Click1(object sender, EventArgs e)
{
if(Convert.ToInt32(ViewState["adid"])==0)
{
Response.Write("<script>alert('你还没有选择要更新的数据!');</script>");
}
else
{
SqlConnection con = conn.createconn();
con.Open();
int aa = Convert.ToInt32(ViewState["adid"]);
string str = "select adid,adsubject,adtype,adcontent from news where adid=" + aa + " ";
SqlCommand cmd = new SqlCommand(str, con);
SqlDataReader rd = cmd.ExecuteReader();
if (rd.Read())
{
tsubject.Text = rd["adsubject"].ToString();
tcontent.Text = rd["adcontent"].ToString();
this.droptype.Text = rd["adtype"].ToString();
}
con.Close();
this.p1.Visible = true;
this.p2.Visible = false;
}
}
protected void btneditnew_Click(object sender, EventArgs e)
{
if (15 > Length(this.tcontent.Text))
{
Response.Write("<script>alert('新闻内容不能少于15个字符!');</script>");
}
else
{
SqlConnection con = conn.createconn();
con.Open();
int aa = Convert.ToInt32(ViewState["adid"]);
string str = "update news set adsubject = '" + this.tsubject.Text + "' ,adcontent= '" + tcontent.Text + "',adtype='" + this.droptype.SelectedValue + "',addata='" + DateTime.Now +"' where adid=" + aa + "";
SqlCommand cmd = new SqlCommand(str, con);
cmd.ExecuteNonQuery();
con.Close();
bindgrid();
Response.Write("<script>alert('更新成功!');</script>");
this.p2.Visible = true;
this.p1.Visible = false;
}
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
int aa = Convert.ToInt32(this.GridView1.SelectedRow.Cells[0].Text);
ViewState["adid"] = aa;
}
protected void btndelt_Click1(object sender, EventArgs e)
{
if (Convert.ToInt32(ViewState["adid"]) == 0)
{
Response.Write("<script>alert('你还没有选择要删除的数据!');</script>");
}
else
{
btndelt.Attributes.Add("onClick", "javascript:return confirm('确定删除吗?');");
//Response.write("<script>return confirm('是否要删除此记录')</script>");
SqlConnection con = conn.createconn();
con.Open();
// string str = "delete from news where adid=" + Convert.ToInt32(DataGrid1.SelectedItem.Cells[0].Text) + " ";
int aa = Convert.ToInt32(ViewState["adid"]);
string str = "delete from news where adid=" + aa + "";
SqlCommand cmd = new SqlCommand(str, con);
cmd.ExecuteNonQuery();
con.Close();
bindgrid();
}
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
this.GridView1.PageIndex = e.NewPageIndex;
bindgrid();
}
protected void btncannle_Click(object sender, EventArgs e)
{
this.p2.Visible = true;
this.p1.Visible = false;
}
public int Length(string str)
{
int length = str.Length;
//用户验证控件
Regex RegCHZN = new Regex("[\u4e00-\u9fa5]");
foreach (char c in str)
{
Match m = RegCHZN.Match(c.ToString());
if (m.Success)
{
length++;
}
}
return length;
}
}
7 个解决方案
#1
string substr =str.Substring(0,5)+"....";
//在这里出错:用户代码未处理ArgumentOutOfRangeException
"索引和长度必须引用该字符串内的位置。参数名: length“ 为什么出现这个错误,请教高手!
//在这里出错:用户代码未处理ArgumentOutOfRangeException
"索引和长度必须引用该字符串内的位置。参数名: length“ 为什么出现这个错误,请教高手!
#2
int i = str.Length;
if(i>=5) string substr =str.Substring(0,5)+"....";
if(i>=5) string substr =str.Substring(0,5)+"....";
#3
public string GetContent(string str)
{
string substr = string.Empty;
int i = str.Length;
if (!string.IsNullOrEmpty(str))
{
if (i > 5)
{
substr = str.Substring(0, 5) + "....";
}
else
{
substr = str;
}
}
return substr ;
}
#4
public string GetContent(string str)
{
int i = str.Length;
string substr = str;
if(i > 5)
{
string substr =str.Substring(0,5)+"....";
}
return substr;
}
{
int i = str.Length;
string substr = str;
if(i > 5)
{
string substr =str.Substring(0,5)+"....";
}
return substr;
}
#5
str的长度少于你的str.Substring(0,5),所以要报错.你的str长度必须>=5,Substring才不会报错
#6
。。。。 连 index 越界都搞不定么? 5 > Length 啦
#7
问题解决了,谢谢!
#1
string substr =str.Substring(0,5)+"....";
//在这里出错:用户代码未处理ArgumentOutOfRangeException
"索引和长度必须引用该字符串内的位置。参数名: length“ 为什么出现这个错误,请教高手!
//在这里出错:用户代码未处理ArgumentOutOfRangeException
"索引和长度必须引用该字符串内的位置。参数名: length“ 为什么出现这个错误,请教高手!
#2
int i = str.Length;
if(i>=5) string substr =str.Substring(0,5)+"....";
if(i>=5) string substr =str.Substring(0,5)+"....";
#3
public string GetContent(string str)
{
string substr = string.Empty;
int i = str.Length;
if (!string.IsNullOrEmpty(str))
{
if (i > 5)
{
substr = str.Substring(0, 5) + "....";
}
else
{
substr = str;
}
}
return substr ;
}
#4
public string GetContent(string str)
{
int i = str.Length;
string substr = str;
if(i > 5)
{
string substr =str.Substring(0,5)+"....";
}
return substr;
}
{
int i = str.Length;
string substr = str;
if(i > 5)
{
string substr =str.Substring(0,5)+"....";
}
return substr;
}
#5
str的长度少于你的str.Substring(0,5),所以要报错.你的str长度必须>=5,Substring才不会报错
#6
。。。。 连 index 越界都搞不定么? 5 > Length 啦
#7
问题解决了,谢谢!