Gridview中各个事件的操作以及FindControl

 

RowDataBound

 

 

( //编辑前的数据绑定显示 以及 行数据初始化的绑定)

 //编辑前的数据绑定显示

  //行绑定用户名
            if (((TextBox)e.Row.FindControl("txtName")) != null)
            {
                TextBox txbox = (TextBox)e.Row.FindControl("txtName");
                txbox.Text = ((HiddenField)e.Row.FindControl("hdfName")).Value;
            }
            //行绑定性别
            if (((RadioButtonList)e.Row.FindControl("rbtnSex")) != null)
            {
                RadioButtonList rbtnSex = (RadioButtonList)e.Row.FindControl("rbtnSex");
                //rbtnSex.Items.Clear();
                //rbtnSex.Items.Add(new ListItem("男"));
                //rbtnSex.Items.Add(new ListItem("女"));
                rbtnSex.SelectedValue = ((HiddenField)e.Row.FindControl("hdfSex")).Value;
            }

行数据初始化的绑定

 if (e.Row.RowType == DataControlRowType.DataRow) 
 if (e.Row.RowType == DataControlRowType.DataRow) 
            {
                Label lblnumber = (Label)e.Row.FindControl("lblNumber");
                HiddenField hdfnumber = (HiddenField)e.Row.FindControl("hdfNumber");
                lblnumber.Text = hdfnumber.Value;


            }

RowDeleting

 

   protected void GridView_Goods_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            Guid id = (Guid)GridView_Goods.DataKeys[e.RowIndex].Value;
            try
            {
                BusinessClass.Goods.Instance.DeleteGoodsByID(id);
                InitGridViewData(Guid.Parse(Session["OrderNo"].ToString()));

            }
            catch
            {
                this.ClientScript.RegisterStartupScript(this.GetType(), "", "alert('删除失败!');", true);
            }
            finally 
            {
            }
        }

 

 

RowCancelingEdit

 //一行被点击,执行取消命令
        protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GridView1.EditIndex = -1;
            bind();
        }

 

RowEditing

 

  // 编辑
        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridView1.EditIndex = e.NewEditIndex;
            bind();
        }

 

RowUpdating

   //更新
        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            Guid ID = (Guid)GridView1.DataKeys[e.RowIndex].Value;

            //UserLogin表
            string Email = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].FindControl("txtEmail"))).Text.ToString().Trim();
            //User表
            string Number = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].FindControl("txtNumber"))).Text.ToString().Trim();
            string UserName = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[4].FindControl("txtName"))).Text.ToString().Trim();
            string Sex = ((RadioButtonList)(GridView1.Rows[e.RowIndex].Cells[5].FindControl("rbtnSex"))).SelectedValue.ToString().Trim();
            Boolean b = false;
            if (Sex == "")
            {
                b = true;
            }
            string bir = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[6].FindControl("txtbir"))).Text.ToString().Trim();
            string Phone = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[7].FindControl("txtPhone"))).Text.ToString().Trim();
            string Home = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[8].FindControl("txtHome"))).Text.ToString().Trim();
            string Conaddress = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[9].FindControl("txtConaddress"))).Text.ToString().Trim();
            string sss = userInfo.Email;
            userInfo.Id = ID;
            userInfo.Email = Email;


            user.Id = userInfo.GetUserIdByUserLoginId(ID);
            if (user.Id == Guid.Empty)
            {
                ClientScript.RegisterStartupScript(this.GetType(), "", "alert('更新失败,请重试')", true);
                return;
            }
            user.Number = Number;
            user.UserName = UserName;

            user.Sex = b;
            user.Birthday = Convert.ToDateTime(bir);
            user.PhoneNumber = Phone;
            user.HomeAddress = Home;
            user.ConnectionAddress = Conaddress;

            if (Email == ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].FindControl("txtEmail"))).Text.ToString().Trim())
            {
                user.ModifyUser(user);

                ScriptManager.RegisterStartupScript(this, this.GetType(), "", "alert('修改成功!');", true);
                GridView1.EditIndex = -1;
                bind();
                // ClientScript.RegisterClientScriptBlock(this.GetType(), "", "alert('修改成功')", true);
                return;
            }
                //userInfo.UserInfo
                userInfo.UpdateUserEmail(userInfo);
            
                user.ModifyUser(user);


                //  ClientScript.RegisterClientScriptBlock(this.GetType(), "", "alert('修改成功')", true);
                ScriptManager.RegisterStartupScript(this, this.GetType(), "", "alert('修改成功!');", true);
                GridView1.EditIndex = -1;//***************************************************************************注意此句
                bind();
         
        }

 

 RowCommand

 

 protected void GridView_Goods_RowCommand(object sender, GridViewCommandEventArgs e)
        {
           GridViewRow row = ((Button)e.CommandSource).Parent.Parent as GridViewRow;
           Label lblnumber = (Label)row.Cells[5].FindControl("lblNumber");
           Label lblamount = (Label)row.Cells[6].FindControl("lblAmount");
           Label lblprice = (Label)row.Cells[4].FindControl("lblPrice");
            
          // int number = Convert.ToInt32(lblnumber.Text);
          // decimal amount = Convert.ToDecimal(lblamount.Text);
         //  decimal price = Convert.ToDecimal(lblprice.Text);
           if (1 > 0) 
           { 
               if (e.CommandName == "subnumber")
               {
                   if (Convert.ToInt32(lblnumber.Text) > 1)
                   {
                       lblnumber.Text = Convert.ToString(Convert.ToInt32(lblnumber.Text) - 1);
                       lblamount.Text = Convert.ToString(Convert.ToDecimal(lblprice.Text) * Convert.ToInt32(lblnumber.Text));
                   }
                   else  
                   {
                       this.ClientScript.RegisterStartupScript(this.GetType(), "", "alert('已经到达数量下限,无法执行操作');", true);
                   }
                   
               }
               if (e.CommandName == "incnumber")
               {
                   lblnumber.Text = (Convert.ToInt32(lblnumber.Text) + 1).ToString();
                   lblamount.Text = Convert.ToString(Convert.ToDecimal(lblprice.Text) * Convert.ToInt32(lblnumber.Text) );
               }
           }

 

 

 

 

 

posted on 2013-09-05 17:47  YoungPop_Chen  阅读(1402)  评论(0编辑  收藏  举报

导航