2016-08-05 68 views
-3

我想定義在asp.net上的C#中的GridView的嘗試捕獲,我已經使用了幾種方法,如在標籤或Java腳本顯示,但他們didn 「T工作, 感謝幫我 這裏是我的代碼嘗試捕獲不起作用在ASP.net中的GridView C#

protected void AddNewSP(object sender, EventArgs e) 
     { 
      try 
      { 
       string Id = ((TextBox)GridView1.FooterRow.FindControl("txtId")).Text; 
       string PartName = ((TextBox)GridView1.FooterRow.FindControl("txtPartName")).Text; 
       string Coverage = ((TextBox)GridView1.FooterRow.FindControl("txtCoverage")).Text; 
       string SupplierName = ((TextBox)GridView1.FooterRow.FindControl("txtSupplierName")).Text; 
       string Status = ((TextBox)GridView1.FooterRow.FindControl("txtStatus")).Text;       
        SqlConnection con = new SqlConnection(strConnString); 
        SqlCommand cmd = new SqlCommand(); 
        cmd.CommandType = CommandType.Text; 
        cmd.CommandText = "insert into SupplySituation(Id, PartName, Coverage, SupplierName, Status) " + 
        "values(@Id, @PartName, @Coverage, @SupplierName, @Status);" + 
        "select Id, PartName, Coverage, SupplierName, Status from SupplySituation ORDER BY Id DESC"; 
        cmd.Parameters.Add("@Id", SqlDbType.VarChar).Value = Id; 
        cmd.Parameters.Add("@PartName", SqlDbType.VarChar).Value = PartName; 
        cmd.Parameters.Add("@Coverage", SqlDbType.VarChar).Value = Coverage; 
        cmd.Parameters.Add("@SupplierName", SqlDbType.VarChar).Value = SupplierName; 
        cmd.Parameters.Add("@Status", SqlDbType.VarChar).Value = Status; 
        GridView1.DataSource = GetData(cmd); 
        GridView1.DataBind(); 
       } 
       catch (Exception ex) 
       { 
        ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(), Guid.NewGuid().ToString(), "alert('" + ex.Message.ToString() + "');", true); 
       }   
     } 

private DataTable GetData(SqlCommand cmd) 
     { 

      DataTable dt = new DataTable(); 
      SqlConnection con = new SqlConnection(strConnString); 
      SqlDataAdapter sda = new SqlDataAdapter(); 
      cmd.CommandType = CommandType.Text; 
      cmd.Connection = con; 
      con.Open(); 
      sda.SelectCommand = cmd; 
      sda.Fill(dt); 
      return dt;   
     } 

其實是沒有任何錯誤或任何行動,當我點擊‘添加’有錯誤的數據沒有任何按鈕發生了,我除了顯示錯誤但沒有任何反應

我的前端代碼

<asp:GridView ID="GridView1" runat="server" Width="750px" 
           AutoGenerateColumns="False" Font-Names="Arial" 
           Font-Size="11pt" AlternatingRowStyle-BackColor="#C2D69B" 
           HeaderStyle-BackColor="green" AllowPaging="True" ShowFooter="True" 
           OnPageIndexChanging="OnPagingSP" OnRowEditing="EditSP" 
           OnRowUpdating="UpdateSP" OnRowCancelingEdit="CancelEditSP"> 

           <Columns> 

            <asp:TemplateField HeaderText="Id"> 
             <ItemTemplate> 

              <asp:Label ID="lblId" runat="server" 
               Text='<%# Eval("Id")%>'></asp:Label> 

             </ItemTemplate> 

             <FooterTemplate> 

              <asp:TextBox ID="txtId" Width="20px" 
               MaxLength="5" runat="server"></asp:TextBox> 

             </FooterTemplate> 
            </asp:TemplateField> 

            <asp:TemplateField ItemStyle-Width="50px" HeaderText="PartName"> 

             <ItemTemplate> 

              <asp:Label ID="lblPartName" runat="server" 
               Text='<%# Eval("PartName")%>'></asp:Label> 

             </ItemTemplate> 
             <EditItemTemplate> 

              <asp:TextBox ID="txtPartName" runat="server" 
               Text='<%# Eval("PartName")%>'></asp:TextBox> 

             </EditItemTemplate> 
             <FooterTemplate> 

              <asp:TextBox ID="txtPartName" Width="100px" 
               MaxLength="20" runat="server"></asp:TextBox> 

             </FooterTemplate> 

             <ItemStyle Width="30px" /> 

            </asp:TemplateField> 

            <asp:TemplateField ItemStyle-Width="100px" HeaderText="Coverage"> 

             <ItemTemplate> 

              <asp:Label ID="lblCoverage" runat="server" 
               Text='<%# Eval("Coverage")%>'></asp:Label> 

             </ItemTemplate> 

             <EditItemTemplate> 

              <asp:TextBox ID="txtCoverage" runat="server" 
               Text='<%# Eval("Coverage")%>'></asp:TextBox> 

             </EditItemTemplate> 

             <FooterTemplate> 

              <asp:TextBox ID="txtCoverage" runat="server"></asp:TextBox> 

             </FooterTemplate> 

             <ItemStyle Width="100px" /> 

            </asp:TemplateField> 

            <asp:TemplateField ItemStyle-Width="150px" HeaderText="SupplierName"> 

             <ItemTemplate> 

              <asp:Label ID="lblSupplierName" runat="server" 
               Text='<%# Eval("SupplierName")%>'></asp:Label> 

             </ItemTemplate> 

             <EditItemTemplate> 

              <asp:TextBox ID="txtSupplierName" runat="server" 
               Text='<%# Eval("SupplierName")%>'></asp:TextBox> 

             </EditItemTemplate> 

             <FooterTemplate> 

              <asp:TextBox ID="txtSupplierName" runat="server"></asp:TextBox> 

             </FooterTemplate> 

             <ItemStyle Width="150px" /> 

            </asp:TemplateField> 

            <asp:TemplateField ItemStyle-Width="150px" HeaderText="Status"> 

             <ItemTemplate> 

              <asp:Label ID="lblStatus" runat="server" 
               Text='<%# Eval("Status")%>'></asp:Label> 

             </ItemTemplate> 

             <EditItemTemplate> 

              <asp:TextBox ID="txtStatus" runat="server" 
               Text='<%# Eval("Status")%>'></asp:TextBox> 

             </EditItemTemplate> 

             <FooterTemplate> 

              <asp:TextBox ID="txtStatus" runat="server"></asp:TextBox> 

             </FooterTemplate> 

             <ItemStyle Width="150px" /> 

            </asp:TemplateField> 

            <asp:TemplateField> 

             <ItemTemplate> 

              <asp:LinkButton ID="lnkRemove" runat="server" 
               CommandArgument='<%# Eval("Id")%>' 
               OnClientClick="return confirm('Do you want to delete?')" 
               Text="Delete" OnClick="DeleteSP"></asp:LinkButton> 

             </ItemTemplate> 

             <FooterTemplate> 

              <asp:Button ID="btnAddNewSP" runat="server" Text="Add" 
               OnClick="AddNewSP" /> 

             </FooterTemplate> 

            </asp:TemplateField> 

            <asp:CommandField ShowEditButton="True" /> 

           </Columns> 

           <AlternatingRowStyle BackColor="#C2D69B" /> 

           <HeaderStyle BackColor="Green" /> 

          </asp:GridView> 
+1

你是什麼確切的問題? – jonju

+0

也許有沒有任何錯誤要抓到? – VDWWD

+0

'我已經使用了幾種方法,例如在標籤或java腳本上顯示代碼在哪裏? '但他們沒有工作,'你得到什麼錯誤? – Eser

回答

0

嘗試替換您的catch塊

catch (Exception ex) 
      { 
        ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(), Guid.NewGuid().ToString(), "alert('" + ex.Message.ToString() + "');", true); 
      }   

下列要求:

catch (Exception ex) 
    { 
      ScriptManager.RegisterStartupScript(this, this.GetType(), "alert", "alert('" + ex.Message.ToString() + "');", true); 
    } 
+0

我看到這個錯誤:名稱thisGetType()在當前上下文中不存在 – Mom

+0

我的不好,我的代碼中有一個錯字。我更正爲:this.GetType(), 如果您收到錯誤,這意味着您處於正確的軌道上。 請重試 – DaniDev

+0

它沒有工作,什麼也沒有發生:( – Mom