PDA

View Full Version : سوال: نمایش پیغام



irrazor
شنبه 11 بهمن 1393, 11:44 صبح
سلام دوستان

من داخل گرید ویو از 3 تا دکمه استفاده میکنم(حذف.انتخاب.ویرایش)لاز م به ذکره هیچ کدوم پیشفرضخود asp نیست و خودم برنامه نوشتم(Templete) بجز حذف.

حال میخوام وقتی کاربر رو حذف کلیک میکنه ی پیغام بیاد("ایا از حذف اطلاعات مطمعآید") بعد شامل 2 تا دکمه لغو و بله باشه.گشتم این کد رو پیدا کردم ولی واس حذف کار نمیکنه(cancele میزنم باز حذف میکنه) ولی واسه سایر دکمه ها مثل ویرایش دقیق کار میکنه!

اینم کد:


this.GridView1.Attributes.Add("onclick", "javascript: return confirm('آیا از حذف اطلاعات مطمئن هستید ؟');");

این کد در گریدویو حذف:


protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e) {
string Cod1 = GridView1.Rows[e.RowIndex].Cells[3].Text.ToString();
delete1(Cod1);
}
void delete1(string Cod1)
{
Label1.Text = "";
Label2.Text = "";


try
{
SqlCommand sm1 = new SqlCommand("delete [LessonPC] where [کد درس]='" + Cod1 + "'", akoconnection);
akoconnection.Open();
sm1.ExecuteNonQuery();
Label1.Visible = true;
akoconnection.Close();
Label1.Text = "شما با موفقیت اطلاعات رو در جدول دروس کامپیوتر حذف کردید";
}
catch
{
Label2.Text = "حذف اطلاعات با مشکل مواجه شد";
}
inf();
}

اینم کد ویرایش در گریدویو:


protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) {
if (e.CommandName == "edt1")
{
GridViewRow row1;
int RowIndex;
row1 = (GridViewRow)(((Button)e.CommandSource).NamingCont ainer);
RowIndex = row1.RowIndex;
string Cod1 = GridView1.Rows[RowIndex].Cells[3].Text.ToString();
inf(Cod1);
}
}
void inf(string Cod1)
{
SqlDataAdapter da4 = new SqlDataAdapter("select * from [LessonPC] where [کد درس]='" + Cod1 + "'", akoconnection);
DataTable dt24 = new DataTable();
da4.Fill(dt24);
txtcod.Text = dt24.Rows[0][0].ToString();
txtname.Text = dt24.Rows[0][1].ToString();
txtunit.Text = dt24.Rows[0][2].ToString();
txtsemester.Text = dt24.Rows[0][3].ToString();
txtfield.Text = dt24.Rows[0][4].ToString();
txtday.Text = dt24.Rows[0][5].ToString();
txttime.Text = dt24.Rows[0][6].ToString();
txtm.Text = dt24.Rows[0][8].ToString();
txtm1.Text = dt24.Rows[0][9].ToString();
}

ی چیز دیگه که من میخوام فقط وقتی رو حذف کلیک میشه این پیغام بیاد اما با این کد که پیدا کردم نمیشه محدود کرد فقط میشه واس عملگر(دکمه یا گریدویو...) تعریف کرد.

keiv@n
شنبه 11 بهمن 1393, 15:21 عصر
<asp:GridView ID="GridView1" runat="server" OnRowDeleting="GridView1_RowDeleting">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="deleteButton" runat="server" CommandName="Delete" Text="Delete"
OnClientClick="return confirm('آیا مطمعن به حذف رکورد هستید؟?');" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>



protected void GridView1_RowDeleting(object sender, System.Web.UI.WebControls.GridViewDeleteEventArgs e)
{

DBhelper.delete(int.Parse(GridView1.Rows[e.RowIndex].Cells[1].Text));
DataTable dt = DBhelper.select();
GridView1.DataSource = dt;
GridView1.DataBind();
}

irrazor
شنبه 11 بهمن 1393, 20:42 عصر
دمت گرم عالی بود.

فقط ی سوال این DBhelper چیه که تعریف کردید؟

دوستان مشکلم حل شد

باتشکر

irrazor
یک شنبه 12 بهمن 1393, 10:21 صبح
اقا مشکل قبلی که گفتم حل شد(یعنی وقتی از چندتا دکمه در گریدویو استفاده میکردم وفقط میخواستم واسه دکمه حذف پیغام بده).

اما الان میخوام فقط با استفاده از دکمه حذف پیشفرض گریدویو(commandfield)استفاده کنم.وقتی از این کد استفاده میکنم کار نمیکنه.و از روش خودم که گشتم پیدا کردم استفاده میکنم وقتی پیغام رو cancele میکنی باز اجرا میشه!

keiv@n
یک شنبه 12 بهمن 1393, 14:48 عصر
دمت گرم عالی بود.

فقط ی سوال این DBhelper چیه که تعریف کردید؟

دوستان مشکلم حل شد

باتشکر


DBhelper یه کلاس کمکی که عملیات (....,CRUD(Insert,Update,Delete داخلش تعریف شده

irrazor
یک شنبه 12 بهمن 1393, 22:15 عصر
DBhelper یه کلاس کمکی که عملیات (....,CRUD(Insert,Update,Delete داخلش تعریف شده


اگه میشه مشکل دوم که مطرح کردم رو راه حل پیشنهاد کنید.

اگه میشه ی راحل دیگه بجز تعریف کلاس بگید که کار کنه روش این پیغام.

keiv@n
دوشنبه 13 بهمن 1393, 20:42 عصر
اگه میشه مشکل دوم که مطرح کردم رو راه حل پیشنهاد کنید.

اگه میشه ی راحل دیگه بجز تعریف کلاس بگید که کار کنه روش این پیغام.

بالاخره هر راه حلی که مد نظرتون باشه باید کوئری delete در اون موجود باشه کلاس dbhelper فقط کوری delete رو فراخوانی می کنه ! اگر این کلاس نباشه باید کوئری delete داخل رخداد delete کنترل مورد نظر نوشته بشه

irrazor
سه شنبه 14 بهمن 1393, 10:12 صبح
اگه امکانش هست کد dbhellper هم بزارید کامل من متوجه بشم.

keiv@n
سه شنبه 14 بهمن 1393, 13:18 عصر
اگه امکانش هست کد dbhellper هم بزارید کامل من متوجه بشم.


public static void delete(int id)
{
try
{
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrin gs["mydbConnectionString"].ConnectionString))
{
using (SqlCommand cmd = con.CreateCommand())
{
cmd.CommandType = CommandType.Text;
cmd.CommandText = "delete from map where id = @id";
cmd.Parameters.Add(new SqlParameter("@id", id));
con.Open();
cmd.ExecuteNonQuery();
}
}
}
catch (Exception ex)
{
}
}

keiv@n
سه شنبه 14 بهمن 1393, 13:18 عصر
public static void delete(int id)
{
try
{
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrin gs["mydbConnectionString"].ConnectionString))
{
using (SqlCommand cmd = con.CreateCommand())
{
cmd.CommandType = CommandType.Text;
cmd.CommandText = "delete from map where id = @id";
cmd.Parameters.Add(new SqlParameter("@id", id));
con.Open();
cmd.ExecuteNonQuery();
}
}
}
catch (Exception ex)
{
}
}