ฉันจะGridViewควบคุมการแสดงผล<thead> <tbody>แท็กได้อย่างไร ฉันรู้ว่า.UseAccessibleHeadersทำให้มันถูกใส่<th>แทน<td>แต่ฉันไม่<thead>สามารถปรากฏให้เห็นได้
ฉันจะGridViewควบคุมการแสดงผล<thead> <tbody>แท็กได้อย่างไร ฉันรู้ว่า.UseAccessibleHeadersทำให้มันถูกใส่<th>แทน<td>แต่ฉันไม่<thead>สามารถปรากฏให้เห็นได้
คำตอบ:
สิ่งนี้ควรทำ:
gv.HeaderRow.TableSection = TableRowSection.TableHeader;
HeaderRowคุณสมบัติจะnullจนGridViewได้รับข้อมูลที่ถูกผูกเพื่อให้แน่ใจว่าจะรอจนกว่า databinding ได้เกิดขึ้นก่อนที่จะใช้เส้นข้างต้นของรหัส
theadคือใช้ใน jQuery อย่างไรก็ตามหลังจากแสดงส่วนหัวแล้วtbodyดูเหมือนว่าจะไม่สามารถใช้งานได้ สิ่งที่อาจขาดหายไปในกรณีของฉัน?
ฉันใช้สิ่งนี้ในOnRowDataBoundเหตุการณ์:
protected void GridViewResults_OnRowDataBound(object sender, GridViewRowEventArgs e) {
if (e.Row.RowType == DataControlRowType.Header) {
e.Row.TableSection = TableRowSection.TableHeader;
}
}
GridViewอยู่ภายในUpdatePanelและ async-postback เกิดจากการควบคุมอื่น ๆOnRowDataBoundเหตุการณ์จะไม่ถูกยกขึ้นดังนั้นโค้ดในคำตอบนี้จะไม่ถูกดำเนินการส่งผลให้GridViewเปลี่ยนกลับเป็นการแสดงผลโดยไม่มี<thead>แท็ก ... ถอนหายใจ . หากต้องการกำหนดเป้าหมายกรณีนี้ให้ใส่รหัสจากคำตอบที่ยอมรับลงในPreRenderตัวจัดการเหตุการณ์ของ gridView (เหมือนกับคำตอบของ ASalvo ที่แนะนำ)
รหัสในคำตอบที่ต้องการที่จะไปหรือPage_Load GridView_PreRenderฉันวางไว้ในวิธีการที่เรียกว่าหลังจากนั้นPage_Loadและได้รับไฟล์NullReferenceException.
DataBoundเหตุการณ์ grid.DataBound += (s, e) => { grid.HeaderRow.TableSection = TableRowSection.TableHeader; };
ฉันใช้รหัสต่อไปนี้เพื่อทำสิ่งนี้:
ifงบผมเพิ่มมีความสำคัญ
มิฉะนั้น (ขึ้นอยู่กับว่าคุณแสดงกริดของคุณอย่างไร) คุณจะมีข้อยกเว้นเช่น:
ตารางต้องมีส่วนของแถวตามลำดับส่วนหัวเนื้อหาและส่วนท้าย
protected override void OnPreRender(EventArgs e)
{
if ( (this.ShowHeader == true && this.Rows.Count > 0)
|| (this.ShowHeaderWhenEmpty == true))
{
//Force GridView to use <thead> instead of <tbody> - 11/03/2013 - MCR.
this.HeaderRow.TableSection = TableRowSection.TableHeader;
}
if (this.ShowFooter == true && this.Rows.Count > 0)
{
//Force GridView to use <tfoot> instead of <tbody> - 11/03/2013 - MCR.
this.FooterRow.TableSection = TableRowSection.TableFooter;
}
base.OnPreRender(e);
}
thisวัตถุ GridView ของฉัน
จริงๆแล้วฉันได้ลบล้าง Asp.net GridView เพื่อทำการควบคุมแบบกำหนดเอง แต่คุณสามารถวางสิ่งนี้ลงในaspx.csหน้าและอ้างอิง GridView ตามชื่อแทนการใช้วิธีการกำหนดตารางมุมมองที่กำหนดเอง
FYI: ฉันยังไม่ได้ทดสอบตรรกะส่วนท้าย แต่ฉันรู้ว่ามันใช้ได้กับส่วนหัว
สิ่งนี้ใช้ได้กับฉัน:
protected void GrdPagosRowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.TableSection = TableRowSection.TableBody;
}
else if (e.Row.RowType == DataControlRowType.Header)
{
e.Row.TableSection = TableRowSection.TableHeader;
}
else if (e.Row.RowType == DataControlRowType.Footer)
{
e.Row.TableSection = TableRowSection.TableFooter;
}
}
สิ่งนี้ได้รับการทดลองใน VS2010
ฉันรู้ว่ามันเก่า แต่นี่คือการตีความคำตอบของ MikeTeeVee สำหรับมุมมองมาตรฐาน:
หน้า aspx:
<asp:GridView ID="GridView1" runat="server"
OnPreRender="GridView_PreRender">
aspx.cs:
protected void GridView_PreRender(object sender, EventArgs e)
{
GridView gv = (GridView)sender;
if ((gv.ShowHeader == true && gv.Rows.Count > 0)
|| (gv.ShowHeaderWhenEmpty == true))
{
//Force GridView to use <thead> instead of <tbody> - 11/03/2013 - MCR.
gv.HeaderRow.TableSection = TableRowSection.TableHeader;
}
if (gv.ShowFooter == true && gv.Rows.Count > 0)
{
//Force GridView to use <tfoot> instead of <tbody> - 11/03/2013 - MCR.
gv.FooterRow.TableSection = TableRowSection.TableFooter;
}
}
สร้างฟังก์ชันและใช้ฟังก์ชันนั้นในPageLoadกิจกรรมของคุณดังนี้:
ฟังก์ชั่นคือ:
private void MakeGridViewPrinterFriendly(GridView gridView) {
if (gridView.Rows.Count > 0) {
gridView.UseAccessibleHeader = true;
gridView.HeaderRow.TableSection = TableRowSection.TableHeader;
}
}
PageLoadเหตุการณ์:
protected void Page_Load(object sender, EventArgs e) {
if (!IsPostBack)
{
MakeGridViewPrinterFriendly(grddata);
}
}
คุณยังสามารถใช้ jQuery เพื่อเพิ่มได้ สิ่งนี้หลีกเลี่ยงปัญหาเกี่ยวกับ TableRowSection.TableHeader ที่ถูกทิ้งบน PostBack
$('#myTableId').prepend($("<thead></thead>").append($(this).find("#myTableId tr:first")));