C# 如何使每个按钮仅响应该行
我有一个中继器,它为具有相同字段名的多行创建一个表:C# 如何使每个按钮仅响应该行,c#,asp.net,sql,C#,Asp.net,Sql,我有一个中继器,它为具有相同字段名的多行创建一个表: <asp:Repeater runat="server" ID="rptContent"> <HeaderTemplate> <table border="0" style="width: 95%;"> <tr> <td style="width:
<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>
如果有多个条目,每个按钮将通过
[name]
查询最后四个ssn的不同之处,我如何才能做到这一点?首先,只需点头使用参数
无论如何,有很多方法可以做到这一点,但其中之一可以是向按钮添加commandargument值(然后在代码中对其求值)
然后,您需要确保SELECT语句中有一个字段,例如RIGHT(SSN,4)作为L4\u SSN
最后,您需要修改
btngeneratedf\u单击
sub以计算e.CommandArgument
。我发送两个参数,因为名称可以相同,或者最后四个参数可以相同。因此,我会根据名称和最后四个SSN进行查询,因此只有一个匹配项,但当我运行它时,由于某种原因,查询为空。
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");
}
<asp:Button
ID="btnGeneratePDF"
runat="server"
Text="Generate PDF For"
CommandArgument = '<%# Eval("L4_SSN") %>'
onclick="btnGeneratePDF_Click" />