如何使每个按钮仅响应该行

时间:2014-05-29 21:24:10

标签: c# asp.net sql

我有一个Repeater,它为多行创建一个具有相同字段名称的表:

<asp:Repeater runat="server" ID="rptContent">
            <HeaderTemplate>
                <table border="0" style="width: 95%;">
                    <tr>
                        <td style="width: 25%;">Name</td>
                        <td style="width: 25%;">Last Four SSN #</td>
                        <td style="width: 25%;">PDF Generator</td>
                    </tr>
            </HeaderTemplate>
            <ItemTemplate>
                    <tr>
                        <td><%# Eval("name").ToString() %></td>
                        <td><%# Eval("ssn3").ToString() %></td>
                        <td><asp:Button ID="btnGeneratePDF" runat="server" Text="Generate PDF For" onclick="btnGeneratePDF_Click" /></td>
                    </tr>
            </ItemTemplate>
            <FooterTemplate>
                </table>
            </FooterTemplate>
        </asp:Repeater>

我的代码背后是这样的:

public void writeData()
    {
        Conn = new SqlConnection(cString);
        Conn.Open();

        nameE = txtName.Text;

        var pdfPath = Path.Combine(Server.MapPath("~/PDFTemplates/9.pdf"));

        // Get the form fields for this PDF and fill them in!
        var formFieldMap = PDFHelper.GetFormFieldNames(pdfPath);
        formFieldMap["topmostSubform[0].Page1[0].f1_01_0_[0]"] = txtName.Text;

        sqlCode = "SELECT * FROM [db].[dbo].[TablePDFTest] WHERE [name] = '" + nameE + "'";

        using (SqlCommand command = new SqlCommand(sqlCode, Conn))
        {
            command.CommandType = CommandType.Text;

            using (reader = command.ExecuteReader())
            {
                if (reader.HasRows)
                {
                    if (reader.Read())
                    {
                        formFieldMap["topmostSubform[0].Page1[0].f1_02_0_[0]"] = reader.GetValue(1).ToString();
                        formFieldMap["topmostSubform[0].Page1[0].f1_04_0_[0]"] = reader.GetValue(2).ToString();
                        formFieldMap["topmostSubform[0].Page1[0].f1_05_0_[0]"] = reader.GetValue(3).ToString();
                        formFieldMap["topmostSubform[0].Page1[0].f1_07_0_[0]"] = reader.GetValue(4).ToString();
                        formFieldMap["topmostSubform[0].Page1[0].social[0].TextField1[0]"] = reader.GetValue(5).ToString();
                        formFieldMap["topmostSubform[0].Page1[0].social[0].TextField2[0]"] = reader.GetValue(6).ToString();
                        formFieldMap["topmostSubform[0].Page1[0].social[0].TextField2[1]"] = reader.GetValue(7).ToString();
                        formFieldMap["topmostSubform[0].Page1[0].social[0].TextField2[2]"] = reader.GetValue(8).ToString();
                        formFieldMap["topmostSubform[0].Page1[0].social[0].TextField2[3]"] = reader.GetValue(9).ToString();
                    }
                }
            }
        }

        // Requester's name and address (hard-coded)
        formFieldMap["topmostSubform[0].Page1[0].f1_06_0_[0]"] = "Medical Group\n2700 Wr Ave\nPurchase, NY 10232";

        var pdfContents = PDFHelper.GeneratePDF(pdfPath, formFieldMap);

        PDFHelper.ReturnPDF(pdfContents, "Compl.pdf");
    }

如果有多个条目,每个按钮将按[name]查询哪个最后四个ssn不同,我怎么能这样做呢?

1 个答案:

答案 0 :(得分:1)

首先,只是点头使用参数...

无论如何,有很多方法可以做到这一点,但可以在你的按钮上添加一个命令值(然后在后面的代码中对它进行评估)。

<asp:Button 
   ID="btnGeneratePDF" 
   runat="server" 
   Text="Generate PDF For" 
   CommandArgument = '<%# Eval("L4_SSN") %>' 
   onclick="btnGeneratePDF_Click" />

然后你需要确保SELECT语句中有一个字段,例如RIGHT(SSN,4) as L4_SSN

最后,您修改了btnGeneratePDF_Click子,以评估e.CommandArgument ...