【问题标题】:Hit Enter on Button Give Diffrent Result在按钮上按 Enter 给出不同的结果
【发布时间】:2013-05-24 09:36:57
【问题描述】:

我有从 datagridview 导出到 Excel 的功能,我通过按 Tab 键来尝试点击界面上的每个按钮来进行测试,以将指示器定位到每个按钮。但是在导出按钮上时。它给了我一个错误

DataGrid with id '' could not automatically generate any columns from the selected data source.

但是当我用指针/鼠标点击它时,它工作正常。

这是我导出到 excel 的代码:

Protected Sub btnExport_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnExport.Click

        Dim dt As New DataTable
        If CInquiry.SearchInquiry(txtAccount.Text, txtCustName.Text, txtAmount.Text, dropResponse.SelectedValue.ToString, txtInquiryDate.Text) Then
            dt = CInquiry.DT
        Else
            eMessage(CInquiry.eMsg)
        End If
        Dim DataGrd As New DataGrid()
        DataGrd.DataSource = dt.DefaultView
        DataGrd.DataBind()

        Dim attachment As String
        attachment = "attachment; filename=Inquiry_Report" & Format(Now, "ddMMMyyyy") & ".xls"
        Response.Buffer = True
        Response.ClearContent()
        Response.ClearHeaders()
        Response.AddHeader("content-disposition", attachment)
        Response.ContentType = "application/ms-excel"
        Dim sw As New StringWriter()
        Dim htw As New HtmlTextWriter(sw)
        DataGrd.RenderControl(htw)
        Response.Write(sw.ToString())
        Response.End()
    End Sub

错误是当数据网格尝试绑定数据时,只有当我按照上面所说的操作时才会出现错误。怎么了?它是 onclick 是指仅通过指针/鼠标单击吗?

【问题讨论】:

    标签: asp.net vb.net button datagrid onclick


    【解决方案1】:

    您好,实际上我现在没有 VB 代码,但我可以发布可能对您有帮助的 c# 代码

     protected void lnkfiledownload_Click(object sender, EventArgs e)
        {
    
            string attachment = "attachment; filename=notification.xls";
    
            Response.ClearContent();
    
            Response.AddHeader("content-disposition", attachment);
    
            Response.ContentType = "application/ms-excel";
    
            StringWriter sw = new StringWriter();
    
            HtmlTextWriter htw = new HtmlTextWriter(sw);
    
            // Create a form to contain the grid
    
            HtmlForm frm = new HtmlForm();
    
            gvBranchNotification.Parent.Controls.Add(frm);
    
            frm.Attributes["runat"] = "server";
    
            frm.Controls.Add(gvBranchNotification);
    
            frm.RenderControl(htw);
    
    
    
            Response.Write(sw.ToString());
    
            Response.End();
    
        }
    

    【讨论】:

    • 它解决了错误,但它创建了一个奇怪的 excel,它的复制与页面显示完全一样
    猜你喜欢
    • 1970-01-01
    • 2023-04-04
    • 2017-01-04
    • 1970-01-01
    • 2018-03-08
    • 2019-03-07
    • 1970-01-01
    • 2018-11-19
    • 1970-01-01
    相关资源
    最近更新 更多