2012-06-22 18 views
0

我在gridview中添加產品並檢查las記錄是否存在於列表中我有一個消息來更新已存在的記錄或刪除最後插入!以下是我的代碼,但它不起作用!誰能幫我?javascript與if語句在asp.net c後面的代碼中觸發按鈕c#

public void SaveToList() 
     { 
      try 
      { 
       OrdersDataContext contex = DataContextSingleton.Instance; 

       _order = new Order(); 
       _order.quant = Int32.Parse(quant.Text); 
       _order.price = price.Text; 
       _order.totalprice = Decimal.Parse(totprice.Text); 
       _order.discoun_id = Int32.Parse(TextBox3.Text); 
       _order.prod_id = Int32.Parse(TextBox2.Text); 
       _order.regist_id = Int32.Parse(TextBox4.Text); 

       _readyToSave.Add(_order); 
       //contex.Orders.InsertOnSubmit(ord); 
       //contex.SubmitChanges(); 
       if (_readyToSave.Count() != 0) 
       { 
        var selectO = (from o in _readyToSave 
            orderby o.prod_id 
            select new 
            { 
             val1 = o.prod_id 
            }).Last(); 

        TextBox15.Text = selectO.val1.ToString(); 

        var selectpr = from p in _readyToSave 
            where (p.prod_id == Int32.Parse(TextBox15.Text)) 
            select p; 
        if (selectpr.Count() > 0) 
        { 
         btnAdd.Attributes.Add("onclick", "var agree=confirm('This product exist on you order! Do you want to update it???'); if (agree){document.getElementById('<%= btnUpdate%>').click();} else{document.getElementById('<%= btnDelNew %>').click();}"); 
         //Response.Write("<script language='javascript'>alert('This product already have been added in list!!!')</script>"); 
        } 
       } 

       ClearTheFields(); 
       Sum(); 
      } 
      catch 
      { 
      } 

     } 

不知道爲什麼,但它停止工作,不調用按鈕.. btnAdd.Attributes.Add(「點擊」,「VAR同意=確認(「本品對你存在訂購!'); if(agree){document.getElementById(''+ btnUpdate.ClientID +'').click();} else {document.getElementById('「+ btnDelNew.ClientID +「').click();}」);誰能幫我???

回答

1

嘗試與ClientId屬性

btnAdd.Attributes.Add("onclick", "var agree=confirm('This product exist on you order! Do you want to update it???'); if (agree){document.getElementById('<%= btnUpdate.ClientId%>').click();} else{document.getElementById('<%= btnDelNew.ClientId %>').click();}"); 
+0

請原諒這樣的問題:我已經限制了GridView控件的知識,但會在按鈕的屬性中使用文字「<%= btnUpdate.ClientId%>」自動分析爲「ctr001_btnUpdate」(例如)?我的直覺是,它只是簡單地發送到瀏覽器,就像字面意思,只是'<%= btnUpdate.ClientID%>' – freefaller

+0

沒什麼,仍然不起作用! –

0

你必須定義爲<%= btnUpdate%><%= btnDelNew %>按鈕名稱。它們應該定義爲<%= btnUpdate.ClientID %><%= btnDelNew.ClientID %>

UPDATE: 如果freefaller的直覺是正確的(我沒有測試以上),然後添加onclick屬性如下所示:

btnAdd.Attributes.Add("onclick", "var agree=confirm('This product exist on you order! Do you want to update it???'); if (agree){document.getElementById('" + btnUpdate.ClientID + "').click();} else{document.getElementById('" + btnDelNew.ClientID + "').click();}"); 
+0

請原諒這個問題:我對GridView的瞭解有限,但會在按鈕屬性中使用文字'<%= btnUpdate.ClientId%>'自動分析爲'ctr001_btnUpdate'(例如)?我的直覺是它只是簡單地發送給瀏覽器,就像'<%= btnUpdate.ClientID%>' – freefaller

0

我回答這個沒有GridView的真正知識,但我的直覺是,你真的將文本<%=btnUpdate%>(或<%=btnUpdate.ClientID%>)發送到瀏覽器。

我認爲你需要做的是改變開頭的行...

btnAdd.Attributes.Add("onclick"... 

要...

btnAdd.Attributes.Add("onclick", "var agree=confirm('This product exist on you order! Do you want to update it???'); if (agree){document.getElementById('" + btnUpdate.ClientID + "').click();} else{document.getElementById('" + btnDelNew.ClientID + "').click();}"); 
+0

謝謝!這個對我有用! –