Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,027

HOME > .NET Framework > Forum > สอบถามต่อค่ะจากที่เคยถามว่า gridview โชว์ข้อมูลตามเงื่อนไข เมื่อโชว์แล้วอยากทำการบันทึกข้อมูลเมื่อมีการแก้ไขค่ะ



 

สอบถามต่อค่ะจากที่เคยถามว่า gridview โชว์ข้อมูลตามเงื่อนไข เมื่อโชว์แล้วอยากทำการบันทึกข้อมูลเมื่อมีการแก้ไขค่ะ

 



Topic : 103274



โพสกระทู้ ( 91 )
บทความ ( 0 )



สถานะออฟไลน์




ข้อมูลแสดงใน gridview ดังรูปค่ะ


meallist


การทำงานคือ เมื่อติ๊กที่ checkbox ที่ header จะทำการบันทึกค่าโดย


ติ๊ก Adult จะให้ save

AgeID = 33
MealPrice = txtMealPrice1.Text.ToDecimal();
ติ๊ก Teen จะให้ save
AgeID = 34
ml.MealPrice = txtMealPrice2.Text.ToDecimal();
ติ๊ก Child จะให้ save
AgeID = 36
ml.MealPrice = txtMealPrice3.Text.ToDecimal();

เขียนโค๊ดปุ้มอัพเดทไว้แบบนี้ ไม่แน่ใจว่าเงือนไขข้างบนเรื่อง checkbox จะเขียนยังไงค่ะ แนะนำด้วยนะคะ

แบบนี้จะถูกหรือป่าว

Code (C#)
   if (chkAdult.Checked == true)
                        {
                            ml.AgeID = 33;
                            ml.MealPrice = txtMealPrice1.Text.ToDecimal();
                        }
                        if (chkTeen.Checked == true)
                        {
                            ml.AgeID = 34;
                            ml.MealPrice = txtMealPrice2.Text.ToDecimal();
                        }
                        if (chkChild.Checked == true)
                        {
                            ml.AgeID = 36;
                            ml.MealPrice = txtMealPrice3.Text.ToDecimal();
                        }



Code (C#)
  protected void cmdUpdate_Click(object sender, EventArgs e)
    {
       
        eContractDBDataContext db = new eContractDBDataContext();

        int icount = 0;

        for (int i = 0; i < grdSearch.Rows.Count; i++)
        {

            CheckBox chkUse = new CheckBox();
            chkUse = grdSearch.Rows[i].FindControl("chkUse") as CheckBox;
            chkUse.Enabled = false;

            if (chkUse.Checked == true)
            {
             CheckBox chkAdult = new CheckBox();
            chkAdult = grdSearch.Rows[i].FindControl("chkAdult") as CheckBox;

            CheckBox chkTeen = new CheckBox();
            chkTeen = grdSearch.Rows[i].FindControl("chkTeen") as CheckBox;

            CheckBox chkChild = new CheckBox();
            chkChild = grdSearch.Rows[i].FindControl("chkChild") as CheckBox;

                
                TextBox txtMealPrice1 = new TextBox();
                txtMealPrice1 = grdSearch.Rows[i].FindControl("txtMealPrice1") as TextBox;
                if (txtMealPrice1.Text == "")
                {
                    txtMealPrice1.Text = "0";
                }
                TextBox txtMealPrice2 = new TextBox();
                txtMealPrice2 = grdSearch.Rows[i].FindControl("txtMealPrice2") as TextBox;
                if (txtMealPrice2.Text == "")
                {
                    txtMealPrice2.Text = "0";
                }
                TextBox txtMealPrice3 = new TextBox();
                txtMealPrice3 = grdSearch.Rows[i].FindControl("txtMealPrice3") as TextBox;
                if (txtMealPrice3.Text == "")
                {
                    txtMealPrice3.Text = "0";
                }
                if (Request.QueryString["mode"] != null)
                {

                    if (txtMealPrice1.ReadOnly == false)
                    {

                        Mealz ml = new Mealz();

                        ml.Compulsory = "O";
                        ml.CreateBy = User.Identity.Name;
                        ml.CurrencyID = Request.QueryString["cur"].ToString().ToDecimal();
                        ml.MealTypeID = grdSearch.DataKeys[i].Value.ToString().ToDecimal();
                        ml.PropID = Request.QueryString["HotelID"].ToString().ToDecimal();
                        ml.UpdateBy = "";
                        ml.UpdateTime = DateTime.Now;
                        ml.ValidYear = "";
                        ml.ValidID = Request.QueryString["V"].ToString().ToDecimal();


                        if (chkAdult.Checked == true)
                        {
                            ml.AgeID = 33;
                            ml.MealPrice = txtMealPrice1.Text.ToDecimal();
                        }
                        if (chkTeen.Checked == true)
                        {
                            ml.AgeID = 34;
                            ml.MealPrice = txtMealPrice2.Text.ToDecimal();
                        }
                        if (chkChild.Checked == true)
                        {
                            ml.AgeID = 36;
                            ml.MealPrice = txtMealPrice3.Text.ToDecimal();
                        }

                        ml.UpdateBy = User.Identity.Name;
                        ml.UpdateTime = DateTime.Now;


                        db.Mealzs.InsertOnSubmit(ml);
                        db.SubmitChanges();

                        chkUse.Enabled = false;


                    }
                    else
                    {
                       var editMeal = (from ml in db.Mealzs
                                        where ml.PropID == Request.QueryString["HotelID"].ToString().ToDecimal()
                                        where ml.ValidID == Request.QueryString["V"].ToString().ToDecimal()
                                        where ml.CurrencyID == Request.QueryString["cur"].ToString().ToDecimal()
                                        where ml.MealTypeID == grdSearch.DataKeys[i].Value.ToString().ToDecimal()
                                        select ml).First();
                       
                        if(chkAdult.Checked == true)
                        {
                        editMeal.AgeID = 33 ;
                        editMeal.MealPrice = txtMealPrice1.Text.ToDecimal();
                        }
                        else
                        {
                        }
                        if(chkTeen.Checked == true)
                        {
                        editMeal.AgeID = 34 ;
                        editMeal.MealPrice = txtMealPrice2.Text.ToDecimal();
                        }
                        else
                        {
                        }
                        if(chkChild.Checked == true)
                        {
                         editMeal.AgeID = 36 ;
                        editMeal.MealPrice = txtMealPrice3.Text.ToDecimal();
                        }
                        else
                        {
                        }
                        
                        editMeal.UpdateBy = User.Identity.Name;
                        editMeal.UpdateTime = DateTime.Now;

                        db.SubmitChanges();

                    }

                }
                else
                {

                    var editMeal = (from ml in db.Mealzs
                                    where ml.PropID == Request.QueryString["HotelID"].ToString().ToDecimal()
                                    where ml.ValidID == Request.QueryString["V"].ToString().ToDecimal()
                                    where ml.CurrencyID == Request.QueryString["cur"].ToString().ToDecimal()
                                    where ml.MealTypeID == grdSearch.DataKeys[i].Value.ToString().ToDecimal()
                                    select ml).First();


                    if (chkAdult.Checked == true)
                    {
                        editMeal.AgeID = 33;
                        editMeal.MealPrice = txtMealPrice1.Text.ToDecimal();
                    }
                    else
                    {
                    }
                    if (chkTeen.Checked == true)
                    {
                        editMeal.AgeID = 34;
                        editMeal.MealPrice = txtMealPrice2.Text.ToDecimal();
                    }
                    else
                    {
                    }
                    if (chkChild.Checked == true)
                    {
                        editMeal.AgeID = 36;
                        editMeal.MealPrice = txtMealPrice3.Text.ToDecimal();
                    }
                    else
                    { 
                    }
                    editMeal.UpdateBy = User.Identity.Name;
                    editMeal.UpdateTime = DateTime.Now;

                    db.SubmitChanges();

                }

                icount = icount + 1;

            }

        }

        if (icount == 0)
        {

            Alert("er", "Please Choose Meal Type", true);
            return;


        }
        else
        {
            Alert(MsgType.FinishUpdate);
            return;
        }


    }





Tag : ASP.NET Ms SQL Server 2008, C#







Move To Hilight (Stock) 
Send To Friend.Bookmark.
Date : 2013-12-03 14:32:11 By : หมูแดง View : 790 Reply : 2
 

 

No. 1



โพสกระทู้ ( 74,058 )
บทความ ( 838 )

สมาชิกที่ใส่เสื้อไทยครีเอท

สถานะออฟไลน์
Twitter Facebook

ใช้หลักการ FindControl ครับ ส่วนจะทำอะไรก็แทรกใน Loop ได้เลยครับ

Code (C#)
    void btnDelete_OnClick(object sender,EventArgs e)
	{
        CheckBox chkDel;
        Label lblID;
        int i;
        for( i = 0; i <= myGridView.Items.Count - 1; i++)
		{
            chkDel = (CheckBox)myGridView.Items[i].FindControl("chkDel");
            lblID = (Label)myGridView.Items[i].FindControl("lblCustomerID");
            if(chkDel.Checked)
			{
				strSQL = "DELETE FROM customer WHERE CustomerID = '"+ lblID.Text +"' ";
				objCmd = new SqlCommand(strSQL, objConn);
				objCmd.ExecuteNonQuery();
            }
        }
		BindData();
    }







แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2013-12-04 09:25:20 By : mr.win
 


 

No. 2



โพสกระทู้ ( 91 )
บทความ ( 0 )



สถานะออฟไลน์


เขียนมาเป็นแบบนี้แต่มันไม่เซฟให้ทุกค่าต้องแก้ตรงไหนคะ

Code (C#)
  protected void cmdSave_Click(object sender, EventArgs e)
    {

        eContractDBDataContext db = new eContractDBDataContext();

            int icount = 0;

            for (int i = 0; i < grdSearch.Rows.Count; i++)
            {
               CheckBox chkUse = new CheckBox();
               chkUse = grdSearch.Rows[i].FindControl("chkUse") as CheckBox;

               chkUse.Enabled = false;

               if (chkUse.Checked == true)
               {
                   CheckBox chkAdult = new CheckBox();
                   chkAdult = grdSearch.HeaderRow.FindControl("chkAdult") as CheckBox;

                   CheckBox chkTeen = new CheckBox();
                   chkTeen = grdSearch.HeaderRow.FindControl("chkTeen") as CheckBox;

                   CheckBox chkChild = new CheckBox();
                   chkChild = grdSearch.HeaderRow.FindControl("chkChild") as CheckBox;


                   TextBox txtMealPrice1 = new TextBox();
                   txtMealPrice1 = grdSearch.Rows[i].FindControl("txtMealPrice1") as TextBox;
                   if (txtMealPrice1.Text == "")
                   {
                       txtMealPrice1.Text = "0";
                   }

                   TextBox txtMealPrice2 = new TextBox();
                   txtMealPrice2 = grdSearch.Rows[i].FindControl("txtMealPrice2") as TextBox;
                   if (txtMealPrice2.Text == "")
                   {
                       txtMealPrice2.Text = "0";
                   }

                   TextBox txtMealPrice3 = new TextBox();
                   txtMealPrice3 = grdSearch.Rows[i].FindControl("txtMealPrice3") as TextBox;
                   if (txtMealPrice3.Text == "")
                   {
                       txtMealPrice3.Text = "0";
                   }

                   Mealz ml = new Mealz();

                   ml.Compulsory = "O";
                   ml.CreateBy = User.Identity.Name;
                   ml.CurrencyID = Request.QueryString["cur"].ToString().ToDecimal();
                   ml.MealTypeID = grdSearch.DataKeys[i].Value.ToString().ToDecimal();
                   ml.PropID = Request.QueryString["HotelID"].ToString().ToDecimal();
                   ml.UpdateBy = "";
                   ml.UpdateTime = DateTime.Now;
                   ml.ValidYear = "";
                   ml.ValidID = Request.QueryString["V"].ToString().ToDecimal();


                   if (chkAdult.Checked == true)
                   {
                       ml.AgeID = 33;
                       ml.MealPrice = txtMealPrice1.Text.ToDecimal();
                   }
                   if(chkTeen.Checked == true)
                   {
                       ml.AgeID = 34;
                       ml.MealPrice = txtMealPrice2.Text.ToDecimal();
                   }
                   if (chkChild.Checked == true)
                   {
                       ml.AgeID = 36;
                       ml.MealPrice = txtMealPrice3.Text.ToDecimal();
                   }


                   db.Mealzs.InsertOnSubmit(ml);
                   db.SubmitChanges();

                   icount = icount + 1;

                 } 
           }

           if (icount == 0)
           {
               Alert("er", "Please Choose Meal Type", true);
               return;
           }
           else
           {
               cmdSave.Visible = false;
               cmdUpdate.Visible = true;

               Alert(MsgType.FinishSave);
               return;
           }
       
 }

แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2013-12-04 10:13:02 By : หมูแดง
 

   

ค้นหาข้อมูล


   
 

แสดงความคิดเห็น
Re : สอบถามต่อค่ะจากที่เคยถามว่า gridview โชว์ข้อมูลตามเงื่อนไข เมื่อโชว์แล้วอยากทำการบันทึกข้อมูลเมื่อมีการแก้ไขค่ะ
 
 
รายละเอียด
 
ตัวหนา ตัวเอียง ตัวขีดเส้นใต้ ตัวมีขีดกลาง| ตัวเรืองแสง ตัวมีเงา ตัวอักษรวิ่ง| จัดย่อหน้าอิสระ จัดย่อหน้าชิดซ้าย จัดย่อหน้ากึ่งกลาง จัดย่อหน้าชิดขวา| เส้นขวาง| ขนาดตัวอักษร แบบตัวอักษร
ใส่แฟลช ใส่รูป ใส่ไฮเปอร์ลิ้งค์ ใส่อีเมล์ ใส่ลิ้งค์ FTP| ใส่แถวของตาราง ใส่คอลัมน์ตาราง| ตัวยก ตัวห้อย ตัวพิมพ์ดีด| ใส่โค้ด ใส่การอ้างถึงคำพูด| ใส่ลีสต์
smiley for :lol: smiley for :ken: smiley for :D smiley for :) smiley for ;) smiley for :eek: smiley for :geek: smiley for :roll: smiley for :erm: smiley for :cool: smiley for :blank: smiley for :idea: smiley for :ehh: smiley for :aargh: smiley for :evil:
Insert PHP Code
Insert ASP Code
Insert VB.NET Code Insert C#.NET Code Insert JavaScript Code Insert C#.NET Code
Insert Java Code
Insert Android Code
Insert Objective-C Code
Insert XML Code
Insert SQL Code
Insert Code
เพื่อความเรียบร้อยของข้อความ ควรจัดรูปแบบให้พอดีกับขนาดของหน้าจอ เพื่อง่ายต่อการอ่านและสบายตา และตรวจสอบภาษาไทยให้ถูกต้อง

อัพโหลดแทรกรูปภาพ

Notice

เพื่อความปลอดภัยของเว็บบอร์ด ไม่อนุญาติให้แทรก แท็ก [img]....[/img] โดยการอัพโหลดไฟล์รูปจากที่อื่น เช่นเว็บไซต์ ฟรีอัพโหลดต่าง ๆ
อัพโหลดแทรกรูปภาพ ให้ใช้บริการอัพโหลดไฟล์ของไทยครีเอท และตัดรูปภาพให้พอดีกับสกรีน เพื่อความโหลดเร็วและไฟล์ไม่ถูกลบทิ้ง

   
  เพื่อความปลอดภัยและการตรวจสอบ กระทู้ที่แทรกไฟล์อัพโหลดไฟล์จากที่อื่น อาจจะถูกลบทิ้ง
 
โดย
อีเมล์
บวกค่าให้ถูก
<= ตัวเลขฮินดูอารบิก เช่น 123 (หรือล็อกอินเข้าระบบสมาชิกเพื่อไม่ต้องกรอก)







Exchange: นำเข้าสินค้าจากจีน, Taobao, เฟอร์นิเจอร์, ของพรีเมี่ยม, ร่ม, ปากกา, power bank, แฟลชไดร์ฟ, กระบอกน้ำ

Load balance : Server 05
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่