1

我有一个现有的 SQL 数据库和一个 ASP.NET 应用程序。我的应用程序有两个现有GridViews和登录功能。我还有一个现有的 Crystal Report,旨在自动从我的 SQL 数据库创建收据。这是通过用户填写 3 个特定参数来完成的,其余数据(与这些参数并行)将自动填写水晶报表。

我想在我的创建一个打印按钮GridView来自动填写水晶报表中的3个参数。这是使我的应用程序更加用户友好的尝试。简而言之,用户只需在 中的新列中按下打印按钮GridView,这 3 个参数就会被自动拾取并填充到 Crystal Report 中。

我的参数是:“EmpID”、“KeyControl”和“ControlNumber”。我的水晶报表标签是“x.rpt”

这是我的GridView标记:

<asp:GridView ID="gridKeyAndBuildingInformation" runat="server"  CssClass="style3" 
              AllowSorting ="True" 
              AutoGenerateColumns ="False" 
              AllowPaging="True"
              DataKeyNames="KeyRefId"
              OnRowCancelingEdit="gridKeyAndBuildingInformation_RowCancelingEdit" 
              onPageIndexChanging="gridKeyAndBuildingInformation_PageIndexChanging"
              OnRowDataBound="gridKeyAndBuildingInformation_RowDataBound"
              OnRowEditing="gridKeyAndBuildingInformation_RowEditing" 
              OnRowUpdating="gridKeyAndBuildingInformation_RowUpdating" 
              OnRowCommand="gridKeyAndBuildingInformation_RowCommand" 
              ShowFooter="True"
              OnRowDeleting="gridKeyAndBuildingInformation_RowDeleting"                                    
              AlternatingRowStyle-BackColor="#EFEFEF" 
              EditRowStyle-VerticalAlign="Top" 
              HeaderStyle-BackColor="#77b218"
              OnSorting="gridKeyAndBuildingInformation_Sorting" 
              BackColor="#CCCCCC" 
              BorderColor="#999999" 
              BorderStyle="Solid" 
              BorderWidth="3px" 
              CellPadding="4" 
              EnableModelValidation="True" 
              ForeColor="Black" 
              CellSpacing="2">
<Columns>

<asp:TemplateField HeaderText ="EmpID" HeaderStyle-CssClass="HeaderText" sortexpression="EmpID">
    <ItemTemplate>                     
    <asp:Label ID="lblEmpID" runat="server" Text='<%# Eval("EmpID") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate> 
    <asp:TextBox ID="txtEmpID" runat="server" Text='<%# Eval("EmpID") %>' Width="50px"></asp:TextBox> 
</EditItemTemplate> 
<FooterTemplate> 
    <asp:TextBox ID="txtNewEmpID" runat="server"  Width="50px"></asp:TextBox> 
</FooterTemplate> 
<HeaderStyle CssClass="HeaderText" />
</asp:TemplateField>

<asp:TemplateField HeaderText ="ControlNumber" HeaderStyle-CssClass="HeaderText" sortexpression="ControlNumber">
<ItemTemplate>                     
    <asp:Label ID="lblControlNumber" runat="server" Text='<%# Eval("ControlNumber") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate> 
    <asp:TextBox ID="txtControlNumber" runat="server" Text='<%# Eval("ControlNumber") %>' Width="50px"></asp:TextBox> 
</EditItemTemplate> 
<FooterTemplate> 
    <asp:TextBox ID="txtNewControlNumber" runat="server"  Width="50px"></asp:TextBox> 
</FooterTemplate> 
<HeaderStyle CssClass="HeaderText" />
</asp:TemplateField> 

<asp:TemplateField HeaderText ="KeyNumber" HeaderStyle-CssClass="HeaderText" sortexpression="KeyNumber">
<ItemTemplate>                     
    <asp:Label ID="lblKeyNumber" runat="server" Text='<%# Eval("KeyNumber") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate> 
    <asp:TextBox ID="txtKeyNumber" runat="server" Text='<%# Eval("KeyNumber") %>' Width="50px"></asp:TextBox> 
</EditItemTemplate> 
<FooterTemplate> 
    <asp:TextBox ID="txtNewKeyNumber" runat="server"  Width="50px"></asp:TextBox> 
</FooterTemplate> 
<HeaderStyle CssClass="HeaderText" />
</asp:TemplateField> 

<asp:TemplateField HeaderText="Edit" ShowHeader="False">
<EditItemTemplate>
    <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True" CommandName="Update" Text="Update"></asp:LinkButton> 
    <asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel"></asp:LinkButton> 
</EditItemTemplate> 
<FooterTemplate>
    <asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="AddNew" Text="Add New"></asp:LinkButton> 
</FooterTemplate> 
<ItemTemplate> 
    <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Edit" Text="Edit"></asp:LinkButton> 
</ItemTemplate> 
</asp:TemplateField> 

<asp:CommandField HeaderText="Delete" ShowDeleteButton="True" ShowHeader="True" /> 
<asp:ButtonField HeaderText="Print" ShowHeader="True" Text="Print" />

</Columns>

<EditRowStyle VerticalAlign="Top"></EditRowStyle>
<FooterStyle BackColor="#CCCCCC" />
<HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White"></HeaderStyle>
<PagerStyle BackColor="#CCCCCC" ForeColor="Black" HorizontalAlign="Left" />
<RowStyle BackColor="White" />
<SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
</asp:GridView>
4

1 回答 1

1

您可以处理该RowCommand事件,该事件会在您单击ButtonField列中的删除按钮时触发。将此添加到GridView声明标记的末尾(在“ CellSpacing="2"”之后但在“”之前>):

OnRowCommand="gridKeyAndBuildingInformation_RowCommand"

然后,在你后面的代码中,你需要这样的东西(这是 C#,如果你需要 VB.NET,请告诉我——这个问题没有用服务器端语言标记):

protected void gridKeyAndBuildingInformation_RowCommand(Object sender, GridViewCommandEventArgs e)
{
    // Get your ID for the row you're on
    int ID = Convert.ToInt32(e.CommandArgument);

    // Get the row the button was clicked in
    GridViewRow row = gridKeyAndBuildingInformation.Rows[ID];

    // Get the values you need from that row
    int EmpID = row.Cells[0];
    int ControlNumber = row.Cells[1];
    int KeyNumber = row.Cells[2]; 

    // Use those numbers to make your call to the Crystal Report
    // I don't know what this part would look like.
}

您可以在 MSDN 上阅读有关 RowCommand 事件的更多信息:GridView.RowCommand 事件

于 2012-08-15T13:09:45.857 回答