0

我在 gridview 中添加产品并检查列表中是否存在 las 记录我有消息要更新现有记录或删除最后插入的记录!下面是我的代码,但它不起作用!谁能帮我?

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("onclick", "var agree=confirm('该产品存在于您的订单中!您要更新它吗??? '); 如果(同意){document.getElementById('" + btnUpdate.ClientID + "').click();} else{document.getElementById('" + btnDelNew.ClientID + "').click();} "); 谁能帮我???

4

3 回答 3

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();}");
于 2012-06-22T10:35:50.630 回答
0

I'm answering this without a real knowledge of GridViews, but my gut feeling is that you're literally sending the text <%=btnUpdate%> (or <%=btnUpdate.ClientID%>) to the browser.

What I think you need to do is change the line starting with...

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

To...

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();}");
于 2012-06-22T10:51:58.847 回答
0

您将按钮名称定义为<%= btnUpdate%><%= btnDelNew %>。它们应该被定义为<%= btnUpdate.ClientID %><%= btnDelNew.ClientID %>

更新:如果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();}");
于 2012-06-22T10:35:57.967 回答