asp.net gridview的checkbox选中问题

greyrainbow2010 2015-04-21 07:04:00
刚接触,没搞过B/S,请教如下问题:
环境:vs2008 + C# + asp.net,学做一个小系统
没有用ajax, 所有控件都是服务器端的

我有一个gridview, 命名为MyGrid
它的第一列绑定了一个模板列,用的是CheckBox, 其他列的数据从数据库后台代码绑定
我在页面上明明选中了好几列,但是后台代码里都取不到选中的情况

protected void Btn_Download_Click(object sender, EventArgs e)
{
int j = 0;
for (int i = 0; i < MyGrid.Rows.Count; i++)
{
GridViewRow row = MyGrid.Rows[i];
if (row.RowType == DataControlRowType.DataRow)
{
CheckBox cb = (CheckBox)row.Cells[0].FindControl("cb_Choose");
if (cb.Checked)
{
j++;
//文件下载路径
string strPath = row.Cells[3].Text.ToString();
//下载文件
DownLoadFile(strPath);
}
}
}

用以上后台代码去获取选中的行的数据,但调试发现没有一行是选中的。
请教是什么原因??是按钮按下时,刷新了页面导致后台无法取得选中行????
...全文
279 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
Justin-Liu 2015-04-22
  • 打赏
  • 举报
回复
创建一个单独的页面用于下载,下载时打开新窗体,有几个开几个
cowmax 2015-04-22
  • 打赏
  • 举报
回复
好像真没见过可以一次下载多个文件的情况,因为一个文件传输完成之后服务器的数据流就终止了。或者通过弹出多个页面的方式(每一个页面自动发起一个下载请求)—— 但是这样的用户体验比较差,而且有些浏览器的安全设置可能也不允许这样做。
greyrainbow2010 2015-04-22
  • 打赏
  • 举报
回复
引用 5 楼 FoxDave 的回复:
多个文件打包压缩后再下载
这不是我想要的功能 其实我要的是在gridview中选中两行或者多行 可以弹出多个下载的对话框 这样子也做不到???
Justin-Liu 2015-04-22
  • 打赏
  • 举报
回复
多个文件打包压缩后再下载
greyrainbow2010 2015-04-22
  • 打赏
  • 举报
回复
这个问题真的这么难解决??
nbhx2010 2015-04-22
  • 打赏
  • 举报
回复
引用 2 楼 greyrainbow2010 的回复:
上述问题已经解决 添加 if (!Page.IsPostBack) { } 就好了。不过又出现新的问题,我明明选中了两行下载,但永远只下载一个文件,这是什么原理?

   protected void Btn_Download_Click(object sender, EventArgs e)
    {
        int j = 0;
        for (int i = 0; i < MyGrid.Rows.Count; i++)
        {
            GridViewRow row = MyGrid.Rows[i];
            if (row.RowType == DataControlRowType.DataRow)
            {
                CheckBox cb = (CheckBox)row.Cells[0].FindControl("cb_Choose");
                if (cb.Checked)
                {
                    j++;   
                    //文件下载路径
                    string strPath = row.Cells[3].Text.ToString();
                    //文件名
                   string strFileName = row.Cell[2].Text.ToString();
                    //下载文件
                    DownLoadFile(strPath, strFileName);
                }
            }
        }

       /// <summary>
    /// 文件下载方法
    /// </summary>
    /// <param name="strPath">文件名</param>
    private void DownLoadFile(string strPath, string strFileName)
    {
        if (!File.Exists(strPath))
        {
            Response.Write("<script>alert('服务器上已经没有此文件!');</script>");
            return;
        }

        try
        {
            string filePath = strPath;
            //以字符流的形式下载文件
            FileStream fs = new FileStream(filePath, FileMode.Open);
            byte[] bytes = new byte[(int)fs.Length];
            fs.Read(bytes, 0, bytes.Length);
            fs.Close();
            Response.ContentType = "application/octet-stream";
            //通知浏览器下载文件而不是打开
            Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(strFileName, System.Text.Encoding.UTF8));
            Response.BinaryWrite(bytes);
            Response.Flush();
            Response.End();
        }
        catch
        {
            Response.Write("<script>alert('查无此资料或已被删除');</script>");
            return;
        }
    }

我即使全部选中, 也只是下载一个文件。调试发现, Response.End();运行完后就跳转到catch里去了 请教,是不是只能下载一个文件?? 如果这样的话,我用radiobutton就行了,都不用checkbox了
这个需求我6年前做过,没实现这个功能。后来改用radiobutton,就每次单个文件选中,然后单个下载 多个文件下载一般采用打包文件,下载到本地后再解压开的方法 不知道高手们有没有实现过楼主需要的这个功能
z22708387 2015-04-22
  • 打赏
  • 举报
回复
打包下载.或者创建多个下载页面. 打包下载是首选.
greyrainbow2010 2015-04-21
  • 打赏
  • 举报
回复
上述问题已经解决 添加 if (!Page.IsPostBack) { } 就好了。不过又出现新的问题,我明明选中了两行下载,但永远只下载一个文件,这是什么原理?

   protected void Btn_Download_Click(object sender, EventArgs e)
    {
        int j = 0;
        for (int i = 0; i < MyGrid.Rows.Count; i++)
        {
            GridViewRow row = MyGrid.Rows[i];
            if (row.RowType == DataControlRowType.DataRow)
            {
                CheckBox cb = (CheckBox)row.Cells[0].FindControl("cb_Choose");
                if (cb.Checked)
                {
                    j++;   
                    //文件下载路径
                    string strPath = row.Cells[3].Text.ToString();
                    //文件名
                   string strFileName = row.Cell[2].Text.ToString();
                    //下载文件
                    DownLoadFile(strPath, strFileName);
                }
            }
        }

       /// <summary>
    /// 文件下载方法
    /// </summary>
    /// <param name="strPath">文件名</param>
    private void DownLoadFile(string strPath, string strFileName)
    {
        if (!File.Exists(strPath))
        {
            Response.Write("<script>alert('服务器上已经没有此文件!');</script>");
            return;
        }

        try
        {
            string filePath = strPath;
            //以字符流的形式下载文件
            FileStream fs = new FileStream(filePath, FileMode.Open);
            byte[] bytes = new byte[(int)fs.Length];
            fs.Read(bytes, 0, bytes.Length);
            fs.Close();
            Response.ContentType = "application/octet-stream";
            //通知浏览器下载文件而不是打开
            Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(strFileName, System.Text.Encoding.UTF8));
            Response.BinaryWrite(bytes);
            Response.Flush();
            Response.End();
        }
        catch
        {
            Response.Write("<script>alert('查无此资料或已被删除');</script>");
            return;
        }
    }

我即使全部选中, 也只是下载一个文件。调试发现, Response.End();运行完后就跳转到catch里去了 请教,是不是只能下载一个文件?? 如果这样的话,我用radiobutton就行了,都不用checkbox了
greyrainbow2010 2015-04-21
  • 打赏
  • 举报
回复

                <asp:GridView ID="MyGrid" runat="server" AutoGenerateColumns="False"
                    Width="700px"
                    border="0" cellpadding="0" cellspacing="1" class="tableborder" >
                    <Columns>
                       <asp:TemplateField HeaderText="选择">
                         <ItemTemplate>
                           <asp:CheckBox ID="cb_Choose" runat="server" Width="40px"/>
                         </ItemTemplate>
                        </asp:TemplateField>
                        <asp:BoundField DataField="Doc_Name" HeaderText="文档名称">
                            <ItemStyle HorizontalAlign="Left" Width="300px" />
                        </asp:BoundField>
                        <asp:BoundField DataField="Doc_Type_Name" HeaderText="文档类型">
                            <ItemStyle HorizontalAlign="Center" Width="80px" />
                        </asp:BoundField>
                        <asp:BoundField DataField="SavePath" HeaderText="保存路径">
                            <ItemStyle HorizontalAlign="Left" Width="300px" />
                        </asp:BoundField>
                        <asp:BoundField DataField="Upload_Operator" HeaderText="上传者">
                            <ItemStyle HorizontalAlign="Center" Width="60px" />
                        </asp:BoundField>
                        <asp:BoundField DataField="Upload_Time" HeaderText="上传时间">
                            <ItemStyle HorizontalAlign="Center" Width="150px" />
                        </asp:BoundField>
        
                    </Columns>
                </asp:GridView>

这是前台gridview的代码

62,074

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧