通过替换链接按钮添加Checkboxmark

时间:2013-10-08 05:18:16

标签: c# asp.net pdf checkbox linkbutton

此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

而不是像这样我希望输出为     enter image description here

所以当我点击CheckboxMark然后我按下Download按钮时,它应该像以前的输出一样好。这可能吗?

1 个答案:

答案 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