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,028

HOME > .NET Framework > Forum > Gridview Redirect Session TT ช่วยด้วยคร้าบ ตอนนี้ผมได้ทำการ ออกแบบ หน้า Web โดยในหน้าจะประกอบไปด้วย Form Edit,Search



 

Gridview Redirect Session TT ช่วยด้วยคร้าบ ตอนนี้ผมได้ทำการ ออกแบบ หน้า Web โดยในหน้าจะประกอบไปด้วย Form Edit,Search

 



Topic : 035258



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



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




ตอนนี้ผมได้ทำการ ออกแบบ หน้า Web โดยในหน้าจะประกอบไปด้วยForm Edit,Search
โดยใน Search นั้นจะมี
Gridview และใช้ CheckBox สำหรับเลือกข้อมูลที่จะลบ

โดยการทำงานนะครับ คือ เมือมีผู้เข้าใช้ จะแสดง Form Search และเมือทำการค้นหาข้อมูลเงื่อนไขในการค้นหา จะถูกเก็บไว้ใน Session
และก็จะแสดง Gridview ขึ้นมาปกติ สามารถทำการลบ โดยเลือกจาก CheckBox ได้ โดยไม่มีปัญหา

แต่พอเราต้องการจะเปลี่ยนหน้า โดยการใช้วิธี Redirect Page index.aspx?mode=Edit จะมีการแสดง Form Edit ให้แสดง พอเราแก้ไขเสร็จ
ก็จะ Redirect กลับไปหน้าเดิม และแสดง Gridview โดยนำ เงื่อนไขในการค้นหาใน Session มาใช้ในการ สร้าง Gridview และสามารถสร้างได้
แต่พอเราไปเลือก CheckBox ใน Gridview กลับไม่แสดงค่า อะไรเลย พอมีทางแก้หรือ ทราบปัญหาที่เกิดขึ้นข้อคำแนะนำหน่อยนะครับ ขอบคุณครับ



Tag : - - - -







Move To Hilight (Stock) 
Send To Friend.Bookmark.
Date : 2009-12-03 16:57:38 By : ksillapapan View : 2585 Reply : 8
 

 

No. 1



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



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


ตอนนี้ได้ข้อมูลมาอีกหน่อยครับ

ผมสงสัยว่า จะมอง CheckBox ไม่เห็น อ่ะครับ

ผมได้ทดสอบ ดู โดยใช้
Code (C#)
foreach (GridViewRow grv in GridView1.Rows)
        {
            
            Label lbl_questionId = (Label)grv.FindControl("lbl_question_id");
            RegisterClientScriptBlock("Onload", "<script>alert('" + lbl_questionId.Text + "');</script>");
            
            }
        }


ไม่ว่ายังไงค่าจะแสดงออกมา พอปลี่ยนหน้า โดยการใช้วิธี Redirect Page index.aspx?mode=Edit จะมีการแสดง Form Edit ให้แสดง พอเราแก้ไขเสร็จ
ก็จะ Redirect กลับไปหน้าเดิม และแสดง Gridview โดยนำ เงื่อนไขในการค้นหาใน Session มาใช้ในการ สร้าง Gridview ผมลอง Test ดูค่ามันก็จะแสดงค่า popup ค่าของ lbl_questionId.Text ที่อยู่ใน Gridview

แต่ถ้าเรา ทำแบบด้านล่าง โดย ตรวจสอบดูด้วยว่า ให้แสดงเฉพาะที่ CheckBox ถูกเลือก
Code (C#)
foreach (GridViewRow grv in GridView1.Rows)
        {
            CheckBox chk = (CheckBox)grv.FindControl("chk_question");
            Label lbl_questionId = (Label)grv.FindControl("lbl_question_id");
            
            if (chk != null)
            {
                if (chk.Checked)
                {
                    RegisterClientScriptBlock("Onload", "<script>alert('" + lbl_questionId.Text + "');</script>");
                    
                }
            }
        }


ค่าจะออก เฉพาะ การค้นหาข้อมูลแล้วสร้างครั้งแรก แล้วพอเอาเงื่อนไขที่เก็บไว้ใน Session มาใช้สร้าง Grid
มันก็จะไม่แสดงค่าครับ เหมือนมันไม่มอง CheckBox

ยังไงช่วยแนะนำหน่อยนะครับ ทุกคนๆ ช่วยด้วยคร้าบๆๆ






Date : 2009-12-03 17:21:33 By : ksillapapan
 


 

No. 2



โพสกระทู้ ( 3,144 )
บทความ ( 1 )

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

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


ทำความเข้าใจกันหน่อยนะ

1. TextBox ไว้ Search
2. Search แล้วแสดงไว้ใน GridView ซึ่งมี CheckBox ไว้เลือกลบได้ และสามารถทำงานได้ปกติ
3. แต่เวลาที่กด Edit จะ Redirect ไปที่หน้า Edit และเก็บค่า KeyWord ไว้ใน Session
4. เมื่อ Edit เรียบร้อยให้ Redirect กลับมาหน้าเดิมโดยเอา KeyWord ใน Session มาใช้ Search อีกครั้งเพื่อแสดงข้อมูลใน GridView
5. จากนั้น CheckBox ที่ใช้ลบข้อมูลก็ใช้ไม่ได้ ใช่ไหมครับ
Date : 2009-12-05 15:57:08 By : tungman
 

 

No. 3



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



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


ใช่ครับ ^^
Date : 2009-12-06 16:54:26 By : ksillapapan
 


 

No. 4



โพสกระทู้ ( 3,144 )
บทความ ( 1 )

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

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


ขอโทษด้วยครับ ออกต่างจังหวัดหลายวัน เพิ่ฝจะเข้ามา

ลองใช้เป็น Request.QueryString แทน Session นะครับ

Test.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Test.aspx.cs" Inherits="Test" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <table>
            <tr>
                <td>
                    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="*" ControlToValidate="TextBox1"></asp:RequiredFieldValidator>
                    <asp:Button ID="Button1" runat="server" Text="Search" OnClick="Button1_Click" />
                </td>
            </tr>
            <tr>
                <td>
                    <asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" 
                        GridLines="None">
                        <RowStyle BackColor="#EFF3FB" />
                        <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                        <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
                        <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
                        <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                        <EditRowStyle BackColor="#2461BF" />
                        <AlternatingRowStyle BackColor="White" />
                    </asp:GridView>
                </td>
            </tr>
        </table>
    </div>
    </form>
</body>
</html>


Test.aspx.cs
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.Web.Configuration;
using System.Data.SqlClient;

public partial class Test : System.Web.UI.Page
{
    private SqlConnection sqlConnection;
    private DataTable CustomerData = new DataTable();

    protected void Page_Load(object sender, EventArgs e)
    {
        string SqlConnectionString = WebConfigurationManager.ConnectionStrings["Sql"].ToString();
        sqlConnection = new SqlConnection(SqlConnectionString);

        BoundField Index = new BoundField();
        Index.HeaderText = "#";
        Index.HeaderStyle.Width = 30;
        Index.ItemStyle.HorizontalAlign = HorizontalAlign.Center;

        HyperLinkField CustomerCode = new HyperLinkField();
        CustomerCode.HeaderText = "รหัสลูกค้า";
        CustomerCode.HeaderStyle.Width = 100;
        CustomerCode.ItemStyle.HorizontalAlign = HorizontalAlign.Left;

        BoundField CustomerName = new BoundField();
        CustomerName.HeaderText = "ลูกค้า";
        CustomerName.HeaderStyle.Width = 400;
        CustomerName.ItemStyle.HorizontalAlign = HorizontalAlign.Left;

        CommandField DeleteButtom = new CommandField();
        DeleteButtom.HeaderText = "ลบ";
        DeleteButtom.HeaderStyle.Width = 30;
        DeleteButtom.ButtonType = ButtonType.Image;
        DeleteButtom.ShowEditButton = false;
        DeleteButtom.ShowDeleteButton = true;
        DeleteButtom.DeleteImageUrl = "~/images/delete-16x16.png";
        DeleteButtom.ItemStyle.HorizontalAlign = HorizontalAlign.Center;

        GridView1.DataKeyNames = new String[] { "CustomerCode" };
        GridView1.RowDataBound += new GridViewRowEventHandler(GridView1_RowDataBound);
        GridView1.RowDeleting += new GridViewDeleteEventHandler(this.GridView1_RowDeleting);
        GridView1.AutoGenerateColumns = false;

        if (!IsPostBack)
        {
            GridView1.Columns.Add(Index);
            GridView1.Columns.Add(CustomerCode);
            GridView1.Columns.Add(CustomerName);
            GridView1.Columns.Add(DeleteButtom);

            if (Request.QueryString["keyword"] != null)
            {
                TextBox1.Text = Request.QueryString["keyword"].ToString();

                CustomerData = SearchCustomer(TextBox1.Text);

                GridView1.DataSource = CustomerData;
                GridView1.DataBind();
            }
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        CustomerData = SearchCustomer(TextBox1.Text);

        GridView1.DataSource = CustomerData;
        GridView1.DataBind();
    }

    protected DataTable SearchCustomer(string KeyWord)
    {
        DataTable Dt = new DataTable();

        string sqlCommandString = "Select [CustomerCode], [CustomerThaiName] From [Customer] Where [CustomerThaiName] Like @KeyWord";
        SqlCommand sqlCommand = new SqlCommand(sqlCommandString, sqlConnection);
        sqlCommand.Parameters.Add("@KeyWord", SqlDbType.NVarChar);
        sqlCommand.Parameters["@KeyWord"].Value = "%" + @KeyWord + "%";

        try
        {
            SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(sqlCommand);
            sqlDataAdapter.Fill(Dt);
        }
        catch (Exception ex)
        {
            MessageBox.Show("Error: " + ex.Message.ToString());
            return null;
        }

        return Dt;
    }

    protected void DeleteCustomer(string CustomerCode)
    {
        string sqlCommandString = "Delete [Customer] Where [CustomerCode] = @CustomerCode";
        SqlCommand sqlCommand = new SqlCommand(sqlCommandString, sqlConnection);
        sqlCommand.Parameters.Add("@CustomerCode", SqlDbType.NVarChar);
        sqlCommand.Parameters["@CustomerCode"].Value = CustomerCode;

        sqlConnection.Open();
        sqlCommand.ExecuteNonQuery();
        sqlConnection.Close();
    }

    protected void GridView1_RowDeleting(Object sender, GridViewDeleteEventArgs e)
    {
        DeleteCustomer(GridView1.DataKeys[e.RowIndex].Value.ToString());

        CustomerData = SearchCustomer(TextBox1.Text);

        GridView1.DataSource = CustomerData;
        GridView1.DataBind();
    }

    protected void GridView1_RowDataBound(Object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            int count = e.Row.RowIndex + 1;
            HyperLink Link = e.Row.Cells[1].Controls[0] as HyperLink;
            ImageButton CommandButton = e.Row.Cells[3].Controls[0] as ImageButton;

            e.Row.Cells[0].Text = count.ToString();
            Link.Text = CustomerData.Rows[e.Row.RowIndex]["CustomerCode"].ToString();
            Link.NavigateUrl = "~/EditTest.aspx?keyword=" + TextBox1.Text + "&id=" + CustomerData.Rows[e.Row.RowIndex]["CustomerCode"].ToString();
            e.Row.Cells[2].Text = CustomerData.Rows[e.Row.RowIndex]["CustomerThaiName"].ToString();
            CommandButton.Attributes.Add("OnClick", "return confirm('คุณต้องการลบข้อมูลนี้ใช่หรือไม่');");
        }
    }
}

Date : 2009-12-08 13:53:35 By : tungman
 


 

No. 5



โพสกระทู้ ( 3,144 )
บทความ ( 1 )

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

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


อันนี้หน้าที่ใช้ edit

EditTest.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="EditTest.aspx.cs" Inherits="EditTest" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:TextBox ID="TextBox1" Width="400" runat="server"></asp:TextBox>
        <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="*" ControlToValidate="TextBox1"></asp:RequiredFieldValidator>
        <br />
        <br />
        <asp:Button ID="SaveButton" runat="server" Text=" Save " OnClick="SaveButton_Click" />
        <asp:Button ID="CancelButton" runat="server" Text=" Cancel " CausesValidation="false" OnClick="CancelButton_Click" />
    </div>
    </form>
</body>
</html>


EditTest.aspx.cs
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.Web.Configuration;
using System.Data.SqlClient;

public partial class EditTest : System.Web.UI.Page
{
    private SqlConnection sqlConnection;

    protected void Page_Load(object sender, EventArgs e)
    {
        string SqlConnectionString = WebConfigurationManager.ConnectionStrings["Sql"].ToString();
        sqlConnection = new SqlConnection(SqlConnectionString);

        TextBox1.Text = CustomerData(Request.QueryString["id"].ToString());
    }

    protected void SaveButton_Click(object sender, EventArgs e)
    {
        EditCustomer(Request.QueryString["id"].ToString(), TextBox1.Text);

        Response.Redirect("Test.aspx?keyword=" + Request.QueryString["keyword"].ToString());
    }

    protected void CancelButton_Click(object sender, EventArgs e)
    {
        Response.Redirect("Test.aspx?keyword=" + Request.QueryString["keyword"].ToString());
    }

    protected string CustomerData(string CustomerCode)
    {
        DataTable Dt = new DataTable(); 
        
        string sqlCommandString = "Select [CustomerThaiName] From [Customer] Where [CustomerCode] = @CustomerCode";
        SqlCommand sqlCommand = new SqlCommand(sqlCommandString, sqlConnection);
        sqlCommand.Parameters.Add("@CustomerCode", SqlDbType.NVarChar);
        sqlCommand.Parameters["@CustomerCode"].Value = CustomerCode;

        try
        {
            SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(sqlCommand);
            sqlDataAdapter.Fill(Dt);
        }
        catch (Exception ex)
        {
            MessageBox.Show("Error: " + ex.Message.ToString());
            return "error";
        }

        return Dt.Rows[0]["CustomerThaiName"].ToString();
    }

    protected void EditCustomer(string CustomerCode, string Editdata)
    {
        string sqlCommandString = "Update [Customer] Set [CustomerThaiName]=@EditData Where [CustomerCode] = @CustomerCode";
        SqlCommand sqlCommand = new SqlCommand(sqlCommandString, sqlConnection);
        sqlCommand.Parameters.Add("@EditData", SqlDbType.NVarChar);
        sqlCommand.Parameters["@EditData"].Value = Editdata;
        sqlCommand.Parameters.Add("@CustomerCode", SqlDbType.NVarChar);
        sqlCommand.Parameters["@CustomerCode"].Value = CustomerCode;

        sqlConnection.Open();
        sqlCommand.ExecuteNonQuery();
        sqlConnection.Close();
    }
}

Date : 2009-12-08 13:55:00 By : tungman
 


 

No. 6



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



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


ขอบคุณมากๆๆ ครับ คุณ tungman
^^
Date : 2009-12-08 14:30:41 By : ksillapapan
 


 

No. 7



โพสกระทู้ ( 3,144 )
บทความ ( 1 )

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

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


มาอ่านดูอีกที รู้สึกว่าผมจะลืม checkbox ไปนะ
Date : 2009-12-08 14:54:23 By : tungman
 


 

No. 8



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



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


ผมชอบวิธี เขียนโค้ด Gridview แบบคุณ tungman มากเลยครับ ผมไม่เคยใช้ BoundField
กำลังจะศึกษา วิธีใช้อยู่ครับ ส่วนมาก ผมใช้ แต่ TemplateFiled อย่างเดี้ยวเลย T T
พอเห็น วิธีที่คุณ Tungman ใช้ แล้ว ถูกใจมากๆๆ ขอบคุณมากครับ ^^
Date : 2009-12-08 15:59:30 By : ksillapapan
 

   

ค้นหาข้อมูล


   
 

แสดงความคิดเห็น
Re : Gridview Redirect Session TT ช่วยด้วยคร้าบ ตอนนี้ผมได้ทำการ ออกแบบ หน้า Web โดยในหน้าจะประกอบไปด้วย Form Edit,Search
 
 
รายละเอียด
 
ตัวหนา ตัวเอียง ตัวขีดเส้นใต้ ตัวมีขีดกลาง| ตัวเรืองแสง ตัวมีเงา ตัวอักษรวิ่ง| จัดย่อหน้าอิสระ จัดย่อหน้าชิดซ้าย จัดย่อหน้ากึ่งกลาง จัดย่อหน้าชิดขวา| เส้นขวาง| ขนาดตัวอักษร แบบตัวอักษร
ใส่แฟลช ใส่รูป ใส่ไฮเปอร์ลิ้งค์ ใส่อีเมล์ ใส่ลิ้งค์ 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 04
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 อัตราราคา คลิกที่นี่