using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

public partial class rl_admins_pg_NewsEdit : System.Web.UI.Page
{
    //********************************
    SqlConnection con;
    SqlTransaction tr;
    SqlCommand cmd = new SqlCommand();
    //********************************
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            C_myClass.MenueItemsCreate(User, panelMenues);
            Label lblTitle = (Label)this.Master.FindControl("lblTitle");
            lblTitle.Text = "ویرایش اخبار";
        }
        catch
        {
        }
        finally
        {
        }
    }

    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        try
        {
            if (e.CommandName == "ShowInList")
            {
                int newsId = Int32.Parse(e.CommandArgument.ToString());
                bool show = false, showInList = false;
                string conString = RamanConnectionString.LocalConnectionString();
                con = new SqlConnection(conString);
                cmd.Connection = con;
                con.Open();
                tr = con.BeginTransaction();
                cmd.Transaction = tr;
                try
                {
                    cmd.CommandText = "select showInList from t_News Where newsId = " + newsId;
                    show = (cmd.ExecuteScalar() != DBNull.Value ? (bool)cmd.ExecuteScalar() : false);
                    showInList = !show;
                    int showBit = (showInList ? 1 : 0);
                    cmd.CommandText = "update t_News SET showInList = " + showBit + " WHERE newsId = " + newsId;
                    cmd.ExecuteNonQuery();
                    tr.Commit();
                    GridView1.DataBind();
                }
                catch (Exception ex)
                {
                    try
                    {
                        tr.Rollback();
                    }
                    catch
                    {
                    }
                }
                finally
                {
                    if (con.State != ConnectionState.Closed)
                        con.Close();
                }
            }
            else if (e.CommandName == "DeletePress")
            {
                int newsId = Int32.Parse(e.CommandArgument.ToString());
                string conString = RamanConnectionString.LocalConnectionString();
                con = new SqlConnection(conString);
                SqlCommand cmd = con.CreateCommand();
                try
                {
                    con.Open();
                    cmd.CommandText = "delete from t_News Where newsId = " + newsId;
                    cmd.ExecuteNonQuery();
                    GridView1.DataBind();
                }
                catch
                {
                }
                finally
                {
                    if (con.State != ConnectionState.Closed)
                        con.Close();
                }
            }
        }
        catch
        {
        }
    }

    protected string GetNewsDetailsEditUrl(object NewsId)
    {
        int newsId = Int32.Parse(NewsId.ToString());
        string url = "pg_NewsDetailsEdit.aspx?newsId=" + newsId.ToString() + "&returnUrl=~/rl_admins/pg_NewsEdit.aspx";
        return url;
    }

    protected string GetNewsDetailsShowUrl(object NewsId)
    {
        int newsId = Int32.Parse(NewsId.ToString());
        string url = "~/pg_NewsDetailsShow.aspx?newsId=" + newsId.ToString() + "&returnUrl=pg_NewsList.aspx";
        return url;
    }

    protected string GetNewsContext(string NewsContext)
    {
        string context = NewsContext;
        if (context.Length > 500)
            context = context.Substring(0, 500);
        context += " ...";
        return context;
    }

    protected void btnOk_Click(object sender, EventArgs e)
    {
        try
        {
            Response.Redirect("pg_NewsCreate.aspx?returnUrl=pg_NewsList.aspx");
        }
        catch
        {
        }
    }

    protected string FindCssClass(object ShowInList)
    {
        string css = "";
        bool showInList = (bool)ShowInList;
        if (showInList)
        {
            css = "gridsmallbuttonstrue";
        }
        else
        {
            css = "gridsmallbuttonsfalse";
        }
        return css;
    }

    protected string FindShowButtonText(object ShowInList)
    {
        string txt = "";
        bool showInList = (bool)ShowInList;
        if (showInList)
        {
            txt = "عدم نمایش";
        }
        else
        {
            txt = "نمایش";
        }
        return txt;
    }
}
