【问题标题】:Adding Checkboxes to a GridView in Visual Studio在 Visual Studio 中将复选框添加到 GridView
【发布时间】:2018-07-23 02:44:38
【问题描述】:

我正在尝试在我的网格视图中添加一列复选框,以便我只能将选定的行导出到 Excel(当前它导出整个网格)。我正在使用 ASP.Net 和 C#,而且我对这两种语言都很陌生。当我添加一个复选框列时,它会在每一行中显示一个复选框。它一直有效,直到我将母版页链接到 aspx 页面,然后该列出现,但只出现一个复选框。该复选框也不会出现在任何行中,而是出现在页面的中间位置。有谁知道我如何解决这个问题或另一种方法来从数据网格中选择多行进行导出(复选框方法更可取)?非常感谢任何帮助。

以下是我的 ASP.Net 代码:

<%@ Page Language="C#" EnableEventValidation="false"      AutoEventWireup="true" CodeBehind="Shipment.aspx.cs"    Inherits="CertificateApplication.Shipment" MasterPageFile="~/Site.Master"%>



<asp:Content ID="BodyContent" ContentPlaceHolderID="BodyContent" runat="server">
<h1 style="color:white;">Shipment Details</h1>
<br/>
<br/>
<div class="container-fluid" ">
  <div class="row">
    <div class="col-12">
        <fieldset style="margin-right:20px;" >
            <!--Panel containing a grid view of data from the database. Not all data is selected, only the data required for the excel document-->
                <asp:Panel ID="DataGrid" runat="server" ScrollBars="Horizontal" style="z-index:-1; margin-right: 248px;" Height="800px" Width="1206px">           
                <asp:SqlDataSource ID="SqlDataSourceCertificate" runat="server" ConnectionString="<%$ ConnectionStrings:CertificateDBConnectionString1 %>" SelectCommand="SELECT * FROM [ProductDetails]"></asp:SqlDataSource>
                <asp:GridView ID="GridView1" runat="server" AllowSorting="True" AutoGenerateColumns="False" CellPadding="3" DataKeyNames="CertificateNumber" DataSourceID="SqlDataSourceCertificate" ForeColor="#333333" GridLines="None" Height="320px" Width="1327px" Font-Size="Small" >
                <AlternatingRowStyle BackColor="White" />
                <Columns>
                    <asp:BoundField DataField="CertificateNumber" HeaderText=" Certificate No." ReadOnly="True" SortExpression="CertificateNumber" />
                    <asp:BoundField DataField="ProductDescription" HeaderText="Name of Product   " SortExpression="ProductDescription" />
                    <asp:BoundField DataField="NetWeight" HeaderText="Weight" SortExpression="NetWeight" />
                    <asp:BoundField DataField="NoPackages" HeaderText= Number of  packages" SortExpression="NoPackages" />
                    <asp:TemplateField HeaderText="Select">
                        <ItemTemplate>
                            <asp:CheckBox ID="CheckBox1" runat="server" />
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns
            </asp:GridView>
             </asp:Panel>
            <!--Buttons to export the data to excel -->
            <asp:Button ID="btnExport" runat="server" Text="Export to Excel" style=" left: 201px; top: 807px; position: absolute; width: 261px; height: 38px;" OnClick="btnExport_Click"/>
            <asp:Button ID="btnExportSelected" runat="server" Text="Export Selected Record to Excel" style=" left: 496px; top: 808px; position: absolute; width: 494px; height: 38px;" OnClick="btnExportSelected_Click"/>
        </fieldset>
      </div>
    </div>
    </div>
<!-- /.container-fluid-->
<!-- /.content-wrapper-->
<!-- Scroll to Top Button-->
<a class="scroll-to-top rounded" href="#page-top">
  <i class="fa fa-angle-up"></i>
</a>

以下是我的 C#:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
using System.IO;

namespace CertificateApplication
{
public partial class Shipment : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }


    public override void VerifyRenderingInServerForm(Control control)
    {
        /* Confirms that an HtmlForm control is rendered for the specified ASP.NET
           server control at run time. */
    }

   protected void btnExport_Click(object sender, EventArgs e)
    {
        Response.ClearContent();
        Response.Buffer = true;
        Response.AddHeader("content-disposition",       string.Format("attachment;filename={0}", "Certificates.xls"));
        Response.ContentType = "application/ms-excel";
        StringWriter sw = new StringWriter();
        HtmlTextWriter hw = new HtmlTextWriter(sw);
        GridView1.AllowPaging = false;
        GridView1.RenderControl(hw);
        Response.Write(sw.ToString());
        Response.End();

    }

}   

}

如果需要,我可以提供我的主密码!谢谢

【问题讨论】:

  • 如果没有母版页但没有母版页显示正常,那是 CSS 问题!在 chrom-> 开发工具中尝试一下
  • 我在 css 文件中找不到导致它的原因。我认为我的编码知识不足以发现导致复选框更改的原因。你知道我可能在找什么吗,或者如果在这里发布 css 代码,你是否能够识别影响复选框的部分?

标签: c# asp.net gridview checkbox


【解决方案1】:

我没有足够的分数来提出一个澄清问题,所以我会提供一个可能的答案。检查主文件并确保没有像“CheckBox1”这样的冲突名称。母版页上是否有任何复选框?我也会怀疑您看到的是哪个复选框 - 它真的来自“发货”页面吗?右键单击该复选框上的检查元素,或查看页面源以更好地了解实际呈现的内容。您可能需要尝试clientIDMode="static" 来帮助了解发生了什么。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-04-10
    • 1970-01-01
    • 1970-01-01
    • 2013-07-30
    • 2011-03-05
    • 1970-01-01
    • 1970-01-01
    • 2016-12-17
    相关资源
    最近更新 更多