bbc_sh
شنبه 29 تیر 1387, 20:34 عصر
من از یک دیتالیست برای ویرایش موجودیت ها استفاده میکنم.توی edititemtemplate یه کنترل آپلود هم هست.حالا مشکل من اینه که ممکنه تو قسمت ویرایش کاربر نخواد عکس جدید وارد کنه,و فقط بخواد یه نام رو تغییر بده در این صورت اگه قسمت آپلود خالی بمونه خطا میگیره .باید چیکار کنم؟
protected void datalist_editcommand(object source, DataListCommandEventArgs e)
{
DataList1.EditItemIndex = e.Item.ItemIndex;
DataList1.DataBind();
}
protected void datalist_cancelcommand(object source, DataListCommandEventArgs e)
{
DataList1.EditItemIndex = -1;
DataList1.DataBind();
}
//updating datalist
protected void datalist_updatecommand(object source, DataListCommandEventArgs e)
{
int stid = Convert.ToInt32(DataList1.DataKeys[e.Item.ItemIndex]);
TextBox firstname = (TextBox)e.Item.FindControl("txtfirst");
TextBox lastname = (TextBox)e.Item.FindControl("txtlast");
FileUpload picupload = (FileUpload)e.Item.FindControl("FileUpload1");
string filename=Path.GetFileName(picupload.FileName);
picupload.SaveAs(Server.MapPath("~/pic/")+filename);
SqlConnection con=new SqlConnection(connectionstring);
string sqlup = "update stt set stname=@stname,stlastname=@stlastname,picture=@pic ture where stid=@stid";
SqlCommand com = new SqlCommand(sqlup, con);
com.Parameters.AddWithValue("@stid" , stid);
com.Parameters.AddWithValue("@stname" , firstname.Text);
com.Parameters.AddWithValue("@stlastname" , lastname.Text);
com.Parameters.AddWithValue("@picture", "~/pic/"+filename);
con.Open();
com.ExecuteNonQuery();
con.Close();
DataList1.EditItemIndex = -1;
DataList1.DataBind();
}
//deleting a record of datalist
protected void datalist_deletecommand(object source, DataListCommandEventArgs e)
{
int stid = Convert.ToInt32(DataList1.DataKeys[e.Item.ItemIndex]);
SqlConnection con=new SqlConnection(connectionstring);
string sqldel = "delete from stt where stid=@stid";
SqlCommand com = new SqlCommand(sqldel, con);
com.Parameters.AddWithValue("@stid", stid);
con.Open();
com.ExecuteNonQuery();
con.Close();
DataList1.DataBind();
}
protected void datalist_editcommand(object source, DataListCommandEventArgs e)
{
DataList1.EditItemIndex = e.Item.ItemIndex;
DataList1.DataBind();
}
protected void datalist_cancelcommand(object source, DataListCommandEventArgs e)
{
DataList1.EditItemIndex = -1;
DataList1.DataBind();
}
//updating datalist
protected void datalist_updatecommand(object source, DataListCommandEventArgs e)
{
int stid = Convert.ToInt32(DataList1.DataKeys[e.Item.ItemIndex]);
TextBox firstname = (TextBox)e.Item.FindControl("txtfirst");
TextBox lastname = (TextBox)e.Item.FindControl("txtlast");
FileUpload picupload = (FileUpload)e.Item.FindControl("FileUpload1");
string filename=Path.GetFileName(picupload.FileName);
picupload.SaveAs(Server.MapPath("~/pic/")+filename);
SqlConnection con=new SqlConnection(connectionstring);
string sqlup = "update stt set stname=@stname,stlastname=@stlastname,picture=@pic ture where stid=@stid";
SqlCommand com = new SqlCommand(sqlup, con);
com.Parameters.AddWithValue("@stid" , stid);
com.Parameters.AddWithValue("@stname" , firstname.Text);
com.Parameters.AddWithValue("@stlastname" , lastname.Text);
com.Parameters.AddWithValue("@picture", "~/pic/"+filename);
con.Open();
com.ExecuteNonQuery();
con.Close();
DataList1.EditItemIndex = -1;
DataList1.DataBind();
}
//deleting a record of datalist
protected void datalist_deletecommand(object source, DataListCommandEventArgs e)
{
int stid = Convert.ToInt32(DataList1.DataKeys[e.Item.ItemIndex]);
SqlConnection con=new SqlConnection(connectionstring);
string sqldel = "delete from stt where stid=@stid";
SqlCommand com = new SqlCommand(sqldel, con);
com.Parameters.AddWithValue("@stid", stid);
con.Open();
com.ExecuteNonQuery();
con.Close();
DataList1.DataBind();
}