我的datagrid里面有个按钮列:
<asp:DataGrid id="dginfoquery" runat="server" AutoGenerateColumns="False" AllowPaging="True" OnPageIndexChanged="dginfoquery_PageIndexChanged" OnItemCommand="detail">
<Columns>
<asp:TemplateColumn HeaderText="">
<ItemTemplate>
<asp:Button id="detail" Text="详细" runat="server" CommandName="detail"></asp:Button>
</ItemTemplate>
</asp:TemplateColumn>               
<</Columns>
</asp:DataGrid>使用了datagrid的自动分页功能,分页代码如下:
private void dginfoquery_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
  {
    dginfoquery.CurrentPageIndex=e.NewPageIndex;
    Binddginfoquery();
   }当点击“详细”按钮时就将该列的其他信息显示在一个弹出式窗口内,下面是事件代码:
private void detail(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
  {
   if(((Button)e.CommandSource).CommandName=="detail")
     Response.Write("<script>window.showModalDialog('detail.aspx',window,'dialogWidth=950px','dialogHeight=950px')<"+"/"+"script>");
  }问题是当我点击datagrid下面的分页数字 “1”、“2”翻页时,就会出错:
“/”应用程序中的服务器错误。
--------------------------------------------------------------------------
指定的转换无效。 
异常详细信息: System.InvalidCastException: 指定的转换无效。
源错误: 
行 124: private void detail(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
行 125: {
行 126:  if(((Button)e.CommandSource).CommandName=="detail")
行 127:   Response.Write("<script>window.showModalDialog('detail.aspx',window,'dialogWidth=950px','dialogHeight=950px')<"+"/"+"script>");
源文件: d:\web\xxxt\infoquery.aspx    行: 126 据我自己的分析,是因为datagrid中的分页数字也是控件,点击会触发OnItemCommand事件,因而会去执行detail()事件处理程序,而在该程序中if(((Button)e.CommandSource).CommandName=="detail")这句话里面因为使用了强制类型转换,而分页数字并非Button类型的,因而导致出错。各位遇到过这种问题没有?请问该如何解决?

解决方案 »

  1.   

    问题楼主已知道,detail()事件注册的地方改改。
      

  2.   

    代码如下:
    <%@Import Namespace="System.Data" %>
    <%@Import Namespace="System.Data.SqlClient" %>
    <HTML>
    <HEAD>
    <title>查询信息情况</title>
    <meta name="GENERATOR" Content="Microsoft FrontPage 5.0">
    <meta name="CODE_LANGUAGE" Content="C#">
    </HEAD>
    <body MS_POSITIONING="GridLayout" bgcolor="#FFF9E1" vlink="#0000FF" style="font-size: 10pt">
      <form id="Forminfoquery" method="post" runat="server">
       <asp:DataGrid id="dginfoquery" style="Z-INDEX: 101; LEFT: 10px; POSITION: absolute; TOP: 140px" runat="server" AutoGenerateColumns="False" Width="630px" Height="" AllowPaging="True" PageStyle-HorizontalAlign="Right" OnPageIndexChanged="dginfoquery_PageIndexChanged" BorderColor="SlateGray" Font-Size="10pt" OnItemCommand="detail">
       <HeaderStyle HorizontalAlign="Center" backcolor="#ffcc99"></HeaderStyle >
       <Columns>
       <asp:BoundColumn HeaderText="编号" DataField="keynum" ItemStyle-HorizontalAlign="center" itemstyle-width="30px"></asp:BoundColumn>
       <asp:TemplateColumn HeaderText=""  ItemStyle-HorizontalAlign="center" itemstyle-width="60px">
         <ItemTemplate>
           <asp:Button id="detail" Text="详细" runat="server" CommandName="detail"></asp:Button>
        </ItemTemplate>
       </asp:TemplateColumn>               
       </Columns>
       </asp:DataGrid>
       <asp:Label id="Label3" style="Z-INDEX: 122; LEFT: 390px; POSITION: absolute; TOP: 100px" runat="server">每页显示行数</asp:Label>
       <asp:textbox id="pagerows" style="Z-INDEX: 102; LEFT: 480px; POSITION: absolute; TOP: 100px" runat="server" Width="30px"></asp:textbox>
       <asp:button id="fresh" style="Z-INDEX: 103; LEFT: 510px; POSITION: absolute; TOP: 100px" runat="server" Text="查询" OnClick="fresh_Click"></asp:button>
       <div id="outError" runat="server"></div>
    </form><script language="C#" runat="server">
       private void Page_Load(object sender, EventArgs e)
         {
           if(!this.IsPostBack)
             {
               pagerows.Text="5";
               dginfoquery.CurrentPageIndex=0; 
               Binddginfoquery();
              }
              }

        private void dginfoquery_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
             {
              dginfoquery.CurrentPageIndex=e.NewPageIndex;
              Binddginfoquery();
              }

         private void detail(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
           {
    if(((Button)e.CommandSource).CommandName=="detail")
               Response.Write("<script>window.showModalDialog('detail.aspx',window,'dialogWidth=950px','dialogHeight=950px')<"+"/"+"script>");
           }

       void Binddginfoquery()
          {
           string strConnect = "Data Source=(local);uid=syf;pwd=syf;Initial Catalog=zkgl";
           string strSelect = "SELECT * FROM casekey";
           DataSet objDataSet = new DataSet();
           try
    {
               SqlConnection objConnect = new SqlConnection(strConnect);
      SqlDataAdapter objDataAdapter = new SqlDataAdapter(strSelect, objConnect);    objDataAdapter.Fill(objDataSet, "casekey");
       DataTable casekey=objDataSet.Tables["casekey"];
    }
            catch (Exception objError)
    {
    outError.InnerHtml = "<b>* 数据库访问失败</b>.<br />"
      + objError.Message + "<br />" + objError.Source;
    return;
    }

             DataView objDataView = new DataView(objDataSet.Tables["casekey"]);
    dginfoquery.DataSource = objDataView;
    objDataView.Sort="subtime DESC";
    dginfoquery.DataBind();  
    }

        private void fresh_Click(object sender, System.EventArgs e)
           {
     Binddginfoquery();
            }
    <script>
    </body>
    </HTML>
      

  3.   

    protected void detail(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
        {
            if (e.CommandSource is Button)
            {
                if (((Button)e.CommandSource).CommandName == "detail")
                    Response.Write("<script>window.showModalDialog('detail.aspx',window,'dialogWidth=950px','dialogHeight=950px')<" + "/" + "script>");
            }
        }