此aspx代码将显示gridview
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataSourceID="SqlDataSource1" RowStyle-HorizontalAlign="Center"
Width="294px"
>
<Columns>
<asp:BoundField DataField="FID" HeaderText="FID" SortExpression="FID" />
<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
<asp:TemplateField ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:LinkButton ID="lnkDownload" runat="server" OnClick = "DownloadFile"
CommandArgument='<%#Eval("FID")%>' Text="Download" CommandName="DownloadFile" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<EmptyDataTemplate><h3 align="center" style="font-weight: bold; font-size: large">NO Records Found</h3></EmptyDataTemplate>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ProjectConnectionString %>"
SelectCommand="SELECT [FID], [Name] FROM [Personal_det]">
</asp:SqlDataSource>
c#
中的代码背后的代码 private DataTable GetData(string query)
{
string conString = ConfigurationManager.ConnectionStrings["ProjectConnectionString"].ConnectionString;
SqlCommand cmd = new SqlCommand(query);
using (SqlConnection cn = new SqlConnection(conString))
{
using (SqlDataAdapter da = new SqlDataAdapter())
{
cmd.Connection = cn;
da.SelectCommand = cmd;
using (DataTable dt = new DataTable())
{
da.Fill(dt);
return dt;
}
}
}
}
protected void DownloadFile(object sender, EventArgs e)
{
string FID = Convert.ToString(((LinkButton)sender).CommandArgument);
var dt = GetData("select * from Personal_det where FID = '"+ FID +"'");
..
..
..
}
输出是这样的
FID Name
M001 Faculty1 download
M002 Faculty2 download
M003 Faculty3 download
M003 Faculty4 download
M004 Faculty5 download
M006 Faculty6 download
M007 Faculty6 download
而不是像这样我希望输出为
所以当我点击CheckboxMark
然后我按下Download
按钮时,它应该像以前的输出一样好。这可能吗?
答案 0 :(得分:0)
在aspx中
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataSourceID="SqlDataSource1"
Width="294px"
>
<Columns>
<asp:BoundField DataField="FID" HeaderText="FID" SortExpression="FID" />
<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
<asp:TemplateField ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<EmptyDataTemplate><h3 align="center" style="font-weight: bold; font-size: large">NO Records Found</h3></EmptyDataTemplate>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ProjectConnectionString %>"
SelectCommand="SELECT [FID], [Name] FROM [Personal_det]">
</asp:SqlDataSource>
在c#
中 private DataTable GetData(string query)
{
string conString = ConfigurationManager.ConnectionStrings["ProjectConnectionString"].ConnectionString;
SqlCommand cmd = new SqlCommand(query);
using (SqlConnection cn = new SqlConnection(conString))
{
using (SqlDataAdapter da = new SqlDataAdapter())
{
cmd.Connection = cn;
da.SelectCommand = cmd;
using (DataTable dt = new DataTable())
{
da.Fill(dt);
return dt;
}
}
}
}
protected void Button1_Click(object sender, EventArgs e)
{
for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
{
string FID = GridView1.Rows[i].Cells[0].Text;
GridViewRow row = GridView1.Rows[i];
CheckBox Ckbox = (CheckBox)row.FindControl("CheckBox1");
var dt = GetData("select * from Personal_det where FID = '" + FID + "'");
if (Ckbox.Checked == true)
{
..
..
..
}
我已经尝试过这一点但是当我为多个值标记checkbox
然后按下download
按钮时,只下载了一个document