我用DataGrid控件来显示信息,在每行信息后面有个按钮,当我点击按钮时就会弹出新窗体。我在ItemCommand事件中写了如下代码:Button bt=(Button)e.Item.FindControl("dgBarcodeInput");
bt.Attributes.Add("onclick", "openform();");现在出现问题:只有点击两次按钮才能弹出对话框!请问是怎么回事呢? 应该如何解决啊?谢谢!

解决方案 »

  1.   

    你的Page_Load是否加了 !IsPostBack 的判断 ?
      

  2.   

    private void Page_Load(object sender, System.EventArgs e)
    {
     // 在此处放置用户代码以初始化页面
     if(!Page.IsPostBack)
     {
      BindOnInit();
     }private void BindLoad()
    {
      if(ddlarray.Items[0].Value!="0")
       {
       dv=Bind();
       dgProduct.DataSource=dv;
       dgProduct.DataKeyField="Pid";
       dgProduct.DataBind();   }}Bind();是返回一个DataView!
      

  3.   

    你这个按钮设置CommandName了吗?
      

  4.   

    我在ItemCommand事件中写了如下代码:Button bt=(Button)e.Item.FindControl("dgBarcodeInput");
    bt.Attributes.Add("onclick", "openform();");
    ======================》
    你在ItemCommand事件才加上onclick事件,当然第一次点击的时候没有加拉,自然不会执行
    要把这一段
    Button bt=(Button)e.Item.FindControl("dgBarcodeInput");
    bt.Attributes.Add("onclick", "openform();");
    放在ItemDataBound里面
      

  5.   

    谢谢楼上的朋友,按你说的做后出现未将对象引用设置到对象的实例的错误,这句话报错:bt.Attributes.Add("onclick", "openform();");
      

  6.   

    谢谢amandag(高歌) ,也加了CommandName,但是还不行啊!放在ItemDataBound里面后出现
    未将对象引用设置到对象的实例的错误,这句话报错:bt.Attributes.Add("onclick", "openform();");
      

  7.   

    private void dgrdTitles_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
    {
    if(e.Item.ItemType == ListItemType.Item|| e.Item.ItemType == ListItemType.AlternatingItem))
    {
    Button bt=(Button)e.Item.FindControl("dgBarcodeInput");
    bt.Attributes.Add("onclick", "openform();");
    }
    }