原來需求只是將單筆資料進行跨行跨列顯示,後來演變到前方增加checkbox,勾選後按button再取得被勾選的欄位內容進行顯示。
 
其中取得地址值資料,是依原Gridview中的排列位置取得即可,例如原來地址排在Cells[5],不用管它是否被移到下一列顯示,直接還是取Cells[5]即可。
 
將GridView1.DataBind()放在Page_InitComplete,而不是放在Page_Load,因為放在Page_Load會發生如果前5個checkbox有勾選,按取值button的postback後,這5個勾選會被自動取消勾選例如勾1、2、6、7,postback後會剩下6、7被勾選,以致button按下後無法判定此5個是否有勾選,所以將GridView1.DataBind()放在Page_InitComplete就可以了。
 
另外,當在頁次非1的頁面中按下取值button後,postback後,Gridview的頁面資料會停在原所在頁次,但下方的數字頁次會顯示成本頁為第1頁,故又在Button1_Click最末加入GridView1.DataBind()
 
 

test1.aspx

 

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

 

 

<!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></title>

</head>

<body>

    <form id="form1" runat="server">

<asp:Label ID="Label98" runat="server" Text=""></asp:Label><br>

<asp:GridView id="GridView1" runat="server" DataSourceID="SqlDataSource1" PageSize="10" OnRowDataBound="GridView1_RowDataBound"  OnPreRender="GridView1_PreRender" EmptyDataText="沒有相應的記錄" DataKeyNames="CustomerID" AutoGenerateColumns="False" AllowPaging="True">

        <Columns>

<asp:TemplateField HeaderText="">

<ItemTemplate>

<asp:CheckBox ID="ckbSelect" runat="server" Checked='<%#Eval("CustomerID").ToString()=="1"?true:false%>' Text=''></asp:CheckBox>

</ItemTemplate>

</asp:TemplateField>

            <asp:BoundField DataField="CustomerID" HeaderText="ID" ReadOnly="True" SortExpression="CustomerID">

                <ItemStyle HorizontalAlign="Center" />

                <HeaderStyle HorizontalAlign="Center" />

            </asp:BoundField>

<asp:BoundField DataField="CompanyName" HeaderText="公司名" ReadOnly="True" SortExpression="CompanyName">

                <ItemStyle HorizontalAlign="Center" />

                <HeaderStyle HorizontalAlign="Center" />

            </asp:BoundField>

            <asp:BoundField DataField="ContactName" HeaderText="聯絡人" SortExpression="ContactName">

                <HeaderStyle HorizontalAlign="Center" />

                <ItemStyle HorizontalAlign="Center" />

            </asp:BoundField> 

<asp:BoundField DataField="ContactTitle" HeaderText="職稱" SortExpression="ContactTitle">

                <HeaderStyle HorizontalAlign="Center" />

                <ItemStyle HorizontalAlign="Center" />

            </asp:BoundField> 

<asp:BoundField DataField="Address" HeaderText="地址" SortExpression="Address">

                <HeaderStyle HorizontalAlign="Center" />

                <ItemStyle HorizontalAlign="Center" />

            </asp:BoundField> 

<asp:BoundField DataField="Country" HeaderText="國別" SortExpression="Country">

                <HeaderStyle HorizontalAlign="Center" />

                <ItemStyle HorizontalAlign="Center" />

            </asp:BoundField> 

</Columns>

    </asp:GridView>

<asp:SqlDataSource id="SqlDataSource1" runat="server" SelectCommand="" ConnectionString="<%$ ConnectionStrings:chtNorthwind %>" >

    </asp:SqlDataSource><br /> 

<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="取值" PostBack="True"/><br>

<asp:Label ID="Label99" runat="server" Text=""></asp:Label><br>

    </form>

</body>

</html>

 

 

test1.aspx.cs

 

using System;

using System.Data;

using System.Configuration;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

using System.Text;

 

public partial class test1 : System.Web.UI.Page

{

private void Page_InitComplete(object sender, System.EventArgs e)

{

this.SqlDataSource1.SelectCommand = "select CustomerID, CompanyName, ContactName, ContactTitle, Address, Country from Customers";

                  this.SqlDataSource1.DataBind();

GridView1.DataBind();

}

 

protected void Page_Load(object sender, EventArgs e)

    {

    }

 

protected void GridView1_PreRender(object sender, EventArgs e)

    {

//隱藏地址資料欄表頭

GridView1.HeaderRow.Cells[5].Visible = false;

 

//隱藏地址資料欄資料

for (Int32 i = 0; i <= GridView1.Rows.Count - 1; i++) {

GridView1.Rows[i].Cells[5].Visible = false;

}

 

foreach (GridViewRow wkItem in GridView1.Rows)  //將第一行資料做rowspan=2

{

wkItem.Cells[0].RowSpan = 2;

}

    }

 

protected void GridView1_RowDataBound(Object sender, GridViewRowEventArgs e)

{

SetMultiRow(e);

}

 

 

private void SetMultiRow(GridViewRowEventArgs e)

{

if (e.Row.RowType == DataControlRowType.DataRow) {

GridViewRow row;

                           row = new GridViewRow(-1, -1, DataControlRowType.DataRow, DataControlRowState.Normal);

                           TableCell cell_1 = new TableCell();

                           cell_1.ColumnSpan = 1;

                           cell_1.Text = "地址---";

cell_1.BackColor = System.Drawing.Color.FromArgb(233,239,255);

                           row.Cells.Add(cell_1);

 

TableCell cell_2 = new TableCell();

                           cell_2.ColumnSpan = 4;

                           cell_2.Text = DataBinder.Eval(e.Row.DataItem, "Address").ToString().Replace("\r\n", "<br>");

cell_2.BackColor = System.Drawing.Color.FromArgb(255,206,128);

                           row.Cells.Add(cell_2);

 

                           e.Row.Parent.Controls.Add(row);

}

}

 

protected void Button1_Click(object sender, EventArgs e)

    {

StringBuilder strBuilder = new StringBuilder();

//foreach (GridViewRow row in GridView1.Rows)

//{

//        strBuilder.AppendFormat("國別:{0},地址:{1}<br />", row.Cells[6].Text, row.Cells[5].Text);

//}

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

{

GridViewRow gvr = GridView1.Rows[i];

CheckBox ckb = (CheckBox)gvr.FindControl("ckbSelect");

if (ckb.Checked==true)

{

//Label99.Text += GridView1.Rows[i].Cells[6].Text + GridView1.Rows[i].Cells[5].Text + "<br>";

strBuilder.AppendFormat("國別:{0},地址:{1}<br />", GridView1.Rows[i].Cells[6].Text, GridView1.Rows[i].Cells[5].Text);

}

}

Label99.Text = strBuilder.ToString();

GridView1.DataBind();  //加入此DataBind後,下方顯示目前停留的頁次才會正確

}

 

}

arrow
arrow
    全站熱搜

    adamschen9921 發表在 痞客邦 留言(0) 人氣()