what is the solution to get "CODE" value directly from Link Button not from the query string?

Peshang M. Husain 0 Reputation points
2023-03-09T19:29:13.16+00:00

Hello Guys;

The below codes work very well and I did not have any problems. but I just want to avoid page postback when the user clicked on LinkButton Product_Item.

what is the solution to get "CODE" value directly from LinkButton, not from the query string?

I appreciate your support.

thanks

using ClosedXML.Excel;
using MB.AwFrosh;
using System;
using System.Configuration;
using System.Data;
using System.Data.OleDb;
using System.IO;
using System.Net.Mail;
using System.Net.Mime;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;

public partial class Market : System.Web.UI.Page
{
    //Start Int to display records
    public int intStart;
    DataTable Basket_DataTable = null;
    protected void Page_Load(object sender, EventArgs e)
    {
        //Display PageStting
        string page = Request.Url.Segments[Request.Url.Segments.Length - 1];

        //Add Keywords Meta Tag.
        HtmlMeta URl = new HtmlMeta();
        URl.Name = "Url";
        URl.Attributes.Add("property", "og:url");
        URl.Content = HttpContext.Current.Request.Url.AbsoluteUri;
        this.Page.Header.Controls.Add(URl);

        HtmlMeta article = new HtmlMeta();
        article.Name = "Type";
        article.Attributes.Add("property", "og:type");
        article.Content = "ئاو فرۆش : بۆ گه‌یاندنی ئاو بۆ به‌رده‌م ماڵ و شوێنه‌کانتان‌";
        this.Page.Header.Controls.Add(article);

        HtmlMeta Name = new HtmlMeta();
        Name.Name = "Naw";
        Name.Attributes.Add("property", "og:title");
        Name.Content = "کلیک لێره‌ بکه‌ بۆ داواکردنی ئاو بۆ هه‌ر ناونیشانێک";
        this.Page.Header.Controls.Add(Name);

        HtmlMeta Fbtxt = new HtmlMeta();
        Fbtxt.Name = "Detail";
        Fbtxt.Attributes.Add("property", "og:description");
        Fbtxt.Content = "توانست و خێرای له‌ گه‌یاندنی ئاوی خواردنه‌وه‌";
        this.Page.Header.Controls.Add(Fbtxt);

        HtmlMeta Wena = new HtmlMeta();
        Wena.Name = "Wena";
        Wena.Attributes.Add("property", "og:image");
        Wena.Content = "http://www.awfrosh.com/App_Othere/Upload/logo/Dawa260X260.png";
        this.Page.Header.Controls.Add(Wena);

        HtmlMeta alt = new HtmlMeta();
        alt.Name = "alt";
        alt.Attributes.Add("property", "og:image:alt");
        alt.Content = "لۆگۆی ئاو فرۆش";
        this.Page.Header.Controls.Add(alt);

        HtmlMeta appid = new HtmlMeta();
        appid.Name = "appid";
        appid.Attributes.Add("property", "fb:app_id");
        appid.Content = "701097394841727";
        this.Page.Header.Controls.Add(appid);

        HtmlMeta locale = new HtmlMeta();
        locale.Name = "appid";
        locale.Attributes.Add("property", "og:locale");
        locale.Content = "ar-iq";
        this.Page.Header.Controls.Add(locale);

        if (!Page.IsPostBack)
        {

            if (Session["basket"] != null)
                ///read Basket_DataTable from session if exist
                Basket_DataTable = (DataTable)Session["basket"];
            else
            {
                //create an empty DataTable and Add some columns to it
                Basket_DataTable = new DataTable();
                Basket_DataTable.Columns.Add("CODE");
                Basket_DataTable.Columns.Add("Product_Names");
                Basket_DataTable.Columns.Add("Product_Price");
                Basket_DataTable.Columns.Add("Product_Images");
                Basket_DataTable.Columns.Add("Basket_Quantities");
                Basket_DataTable.Columns.Add("Product_Dis");
            }

            /////////////////////////
            if (Request["DelID"] != null)
            {
                for (int i = 0; i < Basket_DataTable.Rows.Count; i++)
                    if (Basket_DataTable.Rows[i][0].ToString() == Request["DelID"].ToString())
                        Basket_DataTable.Rows.Remove(Basket_DataTable.Rows[i]);

            }

            if (Request["CODE"] != null)
            {
                //search item in DataTable
                bool Found = false;
                for (int i = 0; i < Basket_DataTable.Rows.Count; i++)
                    if (Basket_DataTable.Rows[i][0].ToString() == Request["CODE"].ToString())
                        Found = true;

                //add to basket
                if (Found == false)
                {      
                    DataAcess data = new DataAcess();
                    string sql = "SELECT * FROM Dawabka_Cat_Com_Pak_Siz_Products where CODE='" + Request["CODE"] + "'";
                    DataTable ret = data.Exe_select(sql);
                    if (ret != null && ret.Rows.Count == 1)
                    {
                        Basket_DataTable.Rows.Add(new object[] { Request["CODE"]
                        ,ret.Rows[0]["Product_Names"].ToString()
                        ,ret.Rows[0]["Product_Price"].ToString()
                        ,ret.Rows[0]["Product_Images"].ToString()
                        ,""
                        ,ret.Rows[0]["Product_Dis"].ToString()});
                    }
                }
            }

            if (IsPostBack == false)
            {
                Temp_Basket.DataSource = Basket_DataTable;
                Temp_Basket.DataBind();
            }

            Session["basket"] = Basket_DataTable;

            //Call BindList Contate page
            ViewState["Start"] = 0;
            BindList1();

        }
    }
    protected void Chainge_Location_Click_Home(object sender, EventArgs e)
    {
        string url = Request.Url.AbsolutePath;
        HttpContext.Current.Session.Remove("basket");
        HttpContext.Current.Session.Abandon();

        Response.Redirect("~/Default.aspx");
    }
    protected void Chainge_Location_Click(object sender, EventArgs e)
    {
        string url = Request.Url.AbsolutePath;
        HttpContext.Current.Session.Remove("basket");
        HttpContext.Current.Session.Abandon();

        Response.Redirect("~/Dawa/Default.aspx");
    }
    protected void Plwsla_Location_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/Dawa/Aw_Dawa.aspx?CRID=" + Request.QueryString["CRID"] + "&#bottom");
    }
    protected void Product_Item1_Click(object sender, EventArgs e)
    {
        foreach (RepeaterItem item in Products.Items)
        {

            if (item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem)

            {
                LinkButton Product_Item = (LinkButton)sender;
                var box = (Label)Product_Item.NamingContainer.FindControl("NOCODE");
                Response.Redirect("Aw_Dawa.aspx?CODE=" + box.Text + "&CRID=" + Request.QueryString["CRID"]);
            }
        }
    }
    protected void Products_Repeater_Items(object sender, RepeaterItemEventArgs e)
    {
        ScriptManager scriptMan = ScriptManager.GetCurrent(this);
        LinkButton btn = e.Item.FindControl("Product_Item") as LinkButton;
        if (btn != null)
        {
            btn.Click += Product_Item1_Click;
            scriptMan.RegisterAsyncPostBackControl(btn);
        }
    }
    //Bindlist Datasource
    public void BindList1()
    {
        //no data in QueryString
        if (Request.QueryString["CRID"] == null)
        {

            Products.Visible = false;

        }
        //+Insurance data + Region data + Cat data
        else if (Request.QueryString["CRID"] != null)
        {

            Products.Visible = true;
        }

        //Configuration Manager to db
        string strConnection = ConfigurationManager.ConnectionStrings["AccessDADB"].ConnectionString;

        //Select Tabel in db
        string strSQL = "SELECT * FROM Dawabka_Cat_Com_Pak_Siz_Products WHERE CRID =" + Request["CRID"] + " ORDER BY CODE";
        //Call Open database - connect to the database
        OleDbConnection objConnection = new OleDbConnection(strConnection);
        //
        OleDbCommand objCommand = new OleDbCommand(strSQL, objConnection);
        //
        OleDbDataAdapter PageAdapter = new OleDbDataAdapter(objCommand);
        //Fill data
        DataSet dts = new DataSet();

        //TOtal Record apear in this section
        intStart = (int)ViewState["Start"];
        ViewState["pageSize"] = 36;

        PageAdapter.Fill(dts, intStart, (int)ViewState["pageSize"], "CRID");

        //Display Data on repeter
        Products.DataSource = dts;
        Products.DataBind();

    }
    protected void Temp_Basket_DataBound(object sender, System.EventArgs e)
    {
        Label3.Text = "&nbsp;" + Temp_Basket.Rows.Count.ToString() + "&nbsp;جۆری | نوع";
    }
    protected void Save_Click(object sender, EventArgs e)
    {
        var value = this.hdnField.Value;

        int G = Convert.ToInt32(value);

        int A = 9000;

        if (A < G)
        {
            DataAcess data = new DataAcess();
            try
            {
                string strSQL = null;

                for (int i = 0; i < Temp_Basket.Rows.Count; i++)
                {
                    TextBox CODE = (TextBox)Temp_Basket.Rows[i].FindControl("CODE");//8
                    TextBox SAdress = (TextBox)Temp_Basket.FooterRow.FindControl("Nawnewshan");//8
                    TextBox Basket_Quantities = (TextBox)Temp_Basket.Rows[i].FindControl("Basket_Quantities");//8
                    String SCRID = Request.QueryString["CRID"];
                    String SCustomerType = "Dawa";
                    DropDownList DropDownListKat1 = (DropDownList)Temp_Basket.FooterRow.FindControl("DropDownListKat1");
                    DropDownList DropDownListKat = (DropDownList)Temp_Basket.FooterRow.FindControl("DropDownListKat");

                    string ip = GetIPAddress();
                    //Response.Write("IP Address: " + GetIPAddress());
                    // Response.Write("IP Address: " + GetIPAddress2()); 

                    TextBox Phone_No = (TextBox)Temp_Basket.FooterRow.FindControl("Phone_No");//8
                    var info = TimeZoneInfo.FindSystemTimeZoneById("Arabic Standard Time");
                    DateTimeOffset localServerTime = DateTimeOffset.Now;
                    DateTimeOffset localTime = TimeZoneInfo.ConvertTime(localServerTime, info);
                    String ZmarayWasl = localTime.ToString("yyHHmmss") + Phone_No.Text.Substring(2);
                    String SBasketDate = localTime.ToString("dd/MM/yyyy HH:mm:ss");

                    string strConnection = ConfigurationManager.ConnectionStrings["AccessDADB"].ConnectionString;
                    strSQL = "INSERT INTO Dawabka_Basket (" +
                    "CODE, " +
                    "Customer_Type, " +
                    "Adress, " +
                    "Basket_Date, " +
                    "Basket_Quantities, " +
                    "Phone_No, " +
                    "CRID, " +
                    "DKat1, " +
                    "DKat, " +
                    "Invoice_No, " +
                    "UserName)"
                    + " VALUES('"
                    + CODE.Text + "', '"
                    + SCustomerType + "', '"
                    + SAdress.Text + "', '"
                    + SBasketDate + "', '"
                    + Basket_Quantities.Text + "', '"
                    + Phone_No.Text + "', '"
                    + SCRID + "', '"
                    + DropDownListKat1.SelectedValue.ToString() + "', '"
                    + DropDownListKat.SelectedValue.ToString() + "', '"
                    + ZmarayWasl + "', '"
                    + ip + "')";

                    //Call Open database - connect to the database
                    OleDbConnection objConnection = new OleDbConnection(strConnection);
                    OleDbCommand objCommand = new OleDbCommand(strSQL, objConnection);
                    objConnection.Open();
                    objCommand.ExecuteNonQuery();
                    objConnection.Close();
                }

                //Get the GridView Data from database.
                DataTable dt = GetData();
                //Set DataTable Name which will be the name of Excel Sheet.
                dt.TableName = "GridView_Data";
                //Create a New Workbook.
                XLWorkbook wb = new XLWorkbook();
                //Add the DataTable as Excel Worksheet.
                wb.Worksheets.Add(dt);
                MemoryStream memoryStream = new MemoryStream();
                //Save the Excel Workbook to MemoryStream.
                wb.SaveAs(memoryStream);
                //Convert MemoryStream to Byte array.
                byte[] bytes = memoryStream.ToArray();
                memoryStream.Close();

                TextBox Phone_No1 = (TextBox)Temp_Basket.FooterRow.FindControl("Phone_No");//8
                var info1 = TimeZoneInfo.FindSystemTimeZoneById("Arabic Standard Time");
                DateTimeOffset localServerTime1 = DateTimeOffset.Now;
                DateTimeOffset localTime1 = TimeZoneInfo.ConvertTime(localServerTime1, info1);
                String ZmarayWasl1 = localTime1.ToString("yyHHmmss") + Phone_No1.Text.Substring(2);

                //Send Email with Excel attachment.
                MailMessage msg = new MailMessage();
                //string Email = "Email";
                //DataTable dtMeta = this.GetData(Email);

                //Add Byte array as Attachment.
                msg.Attachments.Add(new Attachment(new MemoryStream(bytes), "Tak '" + ZmarayWasl1 + "'' .xlsx"));
                msg.IsBodyHtml = false;
                msg.BodyEncoding = Encoding.GetEncoding("ISO-8859-1");
                msg.From = new MailAddress("peshangm@gmail.com", "ئاو فرۆش");
                msg.To.Add(new MailAddress(Globals.Settings.ContactForm.MailTo));
                if (!string.IsNullOrEmpty(Globals.Settings.ContactForm.MailCC))
                    msg.CC.Add(new MailAddress(Globals.Settings.ContactForm.MailCC));
                msg.Priority = MailPriority.High;
                msg.Subject = "‌ئۆرده ری تاک فرۆش‌ : " + ZmarayWasl1;
                msg.Body = "<!DOCTYPE HTML>";
                msg.Body += "<HTML 'direction=rtl' lang='ar-iq'><HEAD><meta http-equiv='Content-Type'  content='text/html' charset='UTF-8'></HEAD><BODY><DIV direction='rtl' style='text-align:right;'><b>";
                msg.Body += "‌ئۆرده ری تاک فرۆش‌ : " + ZmarayWasl1;
                msg.Body += "<br><br><br>ئاو فرۆش: بۆ فرۆشتنی ئاو<br>07704401080<br>www.Awfrosh.com</b></DIV></BODY></HTML>";

                ContentType mimeType = new ContentType("text/html");
                // Add the alternate body to the message.
                AlternateView alternate = AlternateView.CreateAlternateViewFromString(msg.Body, mimeType);
                alternate.ContentType.CharSet = Encoding.UTF8.WebName;
                msg.AlternateViews.Add(alternate);
                SmtpClient client = new SmtpClient();
                // Credentials are necessary if the server requires the client  
                // to authenticate before it will send e-mail on the client's behalf.
                client.UseDefaultCredentials = true;
                client.Port = 25;
                client.EnableSsl = false;
                client.Send(msg);

            }
            finally
            {
                HttpContext.Current.Session.Remove("basket");
                HttpContext.Current.Session.Abandon();
                Response.Redirect("Confirm.aspx");
            }
        }
        else
        {
            BindList1();

            Label Condition1 = (Label)Temp_Basket.FooterRow.FindControl("co1");//8
            Label Condition2 = (Label)Temp_Basket.FooterRow.FindControl("co2");//8

            Condition1.Visible = true;
            Condition2.Visible = true;

        }
    }
    private DataTable GetData()
    {

        TextBox Phone_No = (TextBox)Temp_Basket.FooterRow.FindControl("Phone_No");//8
        var info = TimeZoneInfo.FindSystemTimeZoneById("Arabic Standard Time");
        DateTimeOffset localServerTime = DateTimeOffset.Now;
        DateTimeOffset localTime = TimeZoneInfo.ConvertTime(localServerTime, info);
        String ZmarayWasl = localTime.ToString("yyHHmmss") + Phone_No.Text.Substring(2);

        string constr = ConfigurationManager.ConnectionStrings["AccessDADB"].ConnectionString;
        using (OleDbConnection con = new OleDbConnection(constr))
        {
            using (OleDbCommand cmd = new OleDbCommand("SELECT Customer_Type, Dawabka_Basket.Basket_Date, Dawabka_Basket.Phone_No, Dawabka_Basket.Invoice_No, Dawabka.Region_Name_KU, Dawabka_Basket.Adress, Dawabka_Basket.DKat1, Dawabka_Basket.DKat, Dawabka_Cat_Com.Sharekakan_Names, Dawabka_Cat_Com_Pak_Siz_Products.Product_Dis, Dawabka_Basket.Basket_Quantities, Dawabka_Cat_Com_Pak_Siz_Products.Product_Price, Dawabka_Basket.Delivery, ((Dawabka_Cat_Com_Pak_Siz_Products.Product_Price*Dawabka_Basket.Basket_Quantities) + Dawabka_Basket.Delivery )AS Amount FROM Dawabka INNER JOIN (Dawabka_Cat_Com INNER JOIN ((((Dawabka_Basket INNER JOIN Dawabka_Cat_Com_Pak_Siz_Products ON Dawabka_Basket.CODE = Dawabka_Cat_Com_Pak_Siz_Products.CODE) INNER JOIN Dawabka_Cat ON Dawabka_Cat_Com_Pak_Siz_Products.CATID = Dawabka_Cat.CATID) INNER JOIN Dawabka_Cat_Com_Pak ON Dawabka_Cat_Com_Pak_Siz_Products.CATCOPAID = Dawabka_Cat_Com_Pak.CATCOPAID) INNER JOIN Dawabka_Cat_Com_Pak_Siz ON Dawabka_Cat_Com_Pak_Siz_Products.CATCOPASIID = Dawabka_Cat_Com_Pak_Siz.CATCOPASIID) ON Dawabka_Cat_Com.CATCOID = Dawabka_Cat_Com_Pak_Siz_Products.CATCOID) ON Dawabka.CRID = Dawabka_Basket.CRID WHERE Dawabka_Basket.Invoice_No = '" + ZmarayWasl + "'"))
            {
                using (OleDbDataAdapter sda = new OleDbDataAdapter())
                {
                    cmd.Connection = con;
                    sda.SelectCommand = cmd;
                    using (DataTable dt = new DataTable())
                    {
                        sda.Fill(dt);
                        return dt;
                    }
                }
            }
        }
    }
    protected string GetIPAddress()
    {
        HttpRequest request = HttpContext.Current.Request;
        return request.UserHostAddress;
    }
}
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,234 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,164 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Bruce (SqlWork.com) 54,621 Reputation points
    2023-03-09T21:50:12.93+00:00

    the only alternative is an ajax call to the server. but it looks changing code cause an update of the html, so not sure there is any savings.

    0 comments No comments