各位大侠:
    最近遇到一个问题,Gridview中的模板列Button点击后不触发Button_Click事件,出现网页无法显示错误,前台代码如下:
<%@ Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="波形图数据管理.aspx.cs" Inherits="波形图数据管理" Title="无标题页" %><asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<table>
<tr>
<td align="center">    <asp:Label ID="Label2" runat="server" Text="波形图数据管理"></asp:Label>
&nbsp;</td>
</tr>
<tr>
<td>
    <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True">
    </asp:DropDownList>
    <asp:Label ID="Label3" runat="server" Text="年"></asp:Label>
    <asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="True">
    </asp:DropDownList>
    <asp:Label ID="Label4" runat="server" Text="月"></asp:Label>
    </td>
</tr>
<tr>
<td>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
        onrowcommand="GridView1_RowCommand" >
    <Columns>
    <asp:BoundField DataField="DetectId" HeaderText="Id" SortExpression="DetectId"/>
     <asp:BoundField DataField="线名" HeaderText="线名" SortExpression="线名"/>
     <asp:BoundField DataField="行别" HeaderText="行别" SortExpression="行别"/>
    <asp:BoundField DataField="检测日期" HeaderText="检测日期" SortExpression="检测日期"/>
     <asp:BoundField DataField="检测车号" HeaderText="检测车号" SortExpression="检测车号"/>
     <asp:BoundField DataField="检测车型" HeaderText="检测车型" SortExpression="检测车型"/>
        <asp:HyperLinkField HeaderText="波形图数据" DataTextField="波形数据" SortExpression="波形数据" />
        <asp:TemplateField HeaderText="工务段选择">
        <ItemTemplate>
     <asp:RadioButtonList ID="RadioButtonList1" runat="server">
    </asp:RadioButtonList>
        </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="波形上传">
        <ItemTemplate>
        <input id="FileUpload1" type="file"  name = "FileUpload1" runat="server" />
        </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="">
         <ItemTemplate>
         <asp:Button runat="server" Text="上传"  ID="Upload" OnClick="uploadWavefiles" CommandName="Upload" CommandArgument='<%#Eval("DetectId") %>'/>
         </ItemTemplate>
        </asp:TemplateField>
        
    </Columns>
    </asp:GridView>
</td>
</tr>
    
</table>
    
    
</asp:Content>后台代码:
using System;
using System.IO;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;public partial class 波形图数据管理 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        DateTime Mindate = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM") + "-01");
        DateTime Maxdate = Convert.ToDateTime(DateTime.Now.AddMonths(1).AddDays(-1).ToString("yyyy-MM-dd"));
        if (!IsPostBack)
        {
            Gridview1_databind(Maxdate, Mindate);
        }
    }
    /// <summary>
    /// 显示已经完成检测的检测任务,将数据绑定到Gridview1中,同时在选择工务段列显示
    /// 相应的工务段
    /// </summary>
    public void Gridview1_databind(DateTime MaxDate,DateTime MinDate)
    {
        string strcom = "select * from DetectPlan where (检测状态='已检测' And 检测日期 >= '" + MinDate + "' And 检测日期 <= '" + MaxDate + "') Order by DetectId";
        SQL_ConnectionAllDatabase mysqlcon = new SQL_ConnectionAllDatabase(strcom, "DetectPlan");
        DataSet myds = mysqlcon.query();
        GridView1.DataSource = myds;
        GridView1.DataKeyNames = new string[] { "DetectId" };//主键
        GridView1.DataBind();
        DataSet gwdDataset = new DataSet();
        for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
        {   
            gwdDataset = searchgongwd(GridView1.Rows[i].Cells[1].Text.Trim());
            for (int j = 0; j < gwdDataset.Tables[0].Rows.Count; j++)
            {
                ((RadioButtonList)(GridView1.Rows[i].Cells[7].FindControl("RadioButtonList1"))).Items.Add(gwdDataset.Tables[0].Rows[j][0].ToString().Trim());
            }
            if(GridView1.Rows.Count != 0)
            ((RadioButtonList)(GridView1.Rows[i].Cells[7].FindControl("RadioButtonList1"))).Items[0].Selected = true;
        }
    }
    public DataSet searchgongwd(string LineName)
    {
        string strcom = "select 工务段 from 线名工务段信息 where 线名='" + LineName + "'";
        SQL_Connection mysqlcon = new SQL_Connection(strcom);
        DataSet myds = mysqlcon.query();
        return myds;
    }
    protected void uploadWavefiles(object sender, EventArgs e)
    {
       
    }
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Upload")
        {
            string filename="";
            string gongwd="";
           
            int Id = Convert.ToInt32(e.CommandArgument);
            for (int i = 0; i < GridView1.Rows.Count; i++)
            {
                if (Convert.ToInt32(GridView1.Rows[i].Cells[0].Text.Trim()) == Id)
                {
                    string[] fileNamestring = ((HtmlInputFile)(GridView1.Rows[i].Cells[8].FindControl("FileUpload1"))).Value.Split('\\');
                    filename = fileNamestring[fileNamestring.Length - 1].Trim();
                    gongwd = ((RadioButtonList)(GridView1.Rows[i].Cells[7].FindControl("RadioButtonList1"))).SelectedValue.ToString().Trim();
                }
            }
             
            string fileinfo = "E:\\Uploadfiles\\DetectData\\Wave\\" + filename;
            if (filename != "")
            {
                try
                {
                    if (File.Exists(fileinfo))
                        File.Delete(fileinfo);
                    ((FileUpload)(GridView1.Rows[Id].Cells[8].FindControl("FileUpload1"))).PostedFile.SaveAs(fileinfo);
                }
                catch (Exception error)
                {
                    Response.Write("保存上传文件错误:" + error.Message);
                }
            }
            //将路径保存在数据库中
            string strcom = "Update DetectPlan set 波形数据='" + fileinfo + "' where DetectId =" + Id;
            SQL_ConnectionAllDatabase mysqlcon = new SQL_ConnectionAllDatabase(strcom, "DetectPlan");
            mysqlcon.Insert();
        }
    }
}
运行情况是:如果fileUpload1不选择上传文件,点击“上传”按钮uploadWavefiles()方法和GridView1_RowCommand()能运行,但如果fileUpload1选择上传文件,点击“上传”按钮直接出“网页无法显示”错误,uploadWavefiles()方法和GridView1_RowCommand()方法根据不执行,不晓得是什么原因,拜托大家帮我看看是什么原因,谢谢

解决方案 »

  1.   

    还没有人来么,又出现了新情况,我把input的runat = "server" 去掉了,GridView1_RowComm事件执行了,但是在后台缺找不到input控件了,导致string[] fileNamestring = ((HtmlInputFile)(GridView1.Rows[i].Cells[8].FindControl("FileUpload1"))).Value.Split('\\')语句过不去了,请问各位高手该怎么办,为什么不能把input控件设置成服务器控件呢?
      

  2.   

    ((HtmlInputFile)(GridView1.Rows[i].Cells[8].FindControl("FileUpload1"))).Value?
    你得到这个属性有什么意义?客户端的完整路径是无法得到的,现在的浏览器都可以设置不上传路径的。你要上传文件,可以参考
    http://dotnet.aspx.cc/file/Add-Delete-Update-Edit-Data-With-Paging-in-GridView.aspx
      

  3.   

    在你的gridview外边放一个“上传文件”按钮就完了。假设上传10个文件,用户是先选择10个文件,然后点一下这个按钮就上传了。干么在每一行放一个上传按钮?
      

  4.   


    什么话?去掉了反而变成服务器控件了?不去掉反而不是服务器控件?去掉了就不是服务器控件了,当然无法找到。另外,就算你写
        GridView1.Rows[i].Cells[3].FindControl("FileUpload1")
    也一样可以找到FileUpload1的,所以这里比较懂使用FindControl的正确写法是写
        GridView1.Rows[i].FindControl("FileUpload1")
    根本用不着从什么Cell去开始查找。
      

  5.   

    现在的问题不是路径的问题,而是HtmlInputFile不知为什么必需Runat="Sever",我肯定得执行findControl("FileUpload1"),至少得知道要上传的文件名,不需要完整路径,我看了你的http://dotnet.aspx.cc/file/Add-Delete-Update-Edit-Data-With-Paging-in-GridView.aspx,一样要用到findControl("FileUpload1")
      

  6.   

    现在的问题是必需要把Input控件去掉runat = "server"才能运行,不知道是什么原因,另外,不能在Gridview外面放上传按钮,因为上传按钮的多少得根据Gridview内容定,每一个上传的文件对应数据库中的每一行
      

  7.   

    本帖最后由 net_lover 于 2012-02-12 14:32:07 编辑