2011-11-09 70 views
1

添加了GridView事件,如RowEditing,RowUpdating。 GridView在UserControl內。 UserControl將在aspx上的佔位符控制中動態加載。一切都很好,RowEditing事件提升,但Rowupdating事件不會觸發。 RowEditing事件加載頁面,並且頁面再次加載UserControl,現在GridView處於編輯模式,但是當單擊更新按鈕時,GridView RowUpdating事件不會被觸發。GridView RowUpdating事件不會引發?

示例代碼:

   <asp:TemplateField HeaderText="Product ID"> 
        <ItemTemplate> 
         <%# Eval("pid") %> 
        </ItemTemplate> 
        <EditItemTemplate> 
         <asp:TextBox ID="txtpid" runat="Server" Text='<%# Eval("pid") %>' Columns="30"></asp:TextBox> 
         <asp:RequiredFieldValidator ID="req1" runat="Server" Text="*" ControlToValidate="txtpid"></asp:RequiredFieldValidator> 
        </EditItemTemplate> 
       </asp:TemplateField> 
       <asp:TemplateField HeaderText="Produt Name"> 
        <ItemTemplate> 
         <%# Eval("pname") %> 
        </ItemTemplate> 
        <EditItemTemplate> 
         <asp:TextBox ID="txtpname" runat="Server" TextMode="MultiLine" Rows="10" Columns="50" 
          Text='<%# Eval("pname") %>'></asp:TextBox> 
         <asp:RequiredFieldValidator ID="req2" runat="Server" Text="*" ControlToValidate="txtpname"></asp:RequiredFieldValidator> 
        </EditItemTemplate> 
       </asp:TemplateField> 
       <asp:TemplateField HeaderText="Price"> 
        <ItemTemplate> 
         <%# Eval("price") %> 
        </ItemTemplate> 
        <EditItemTemplate> 
         <asp:TextBox ID="txtprice" runat="Server" TextMode="MultiLine" Rows="10" Columns="50" 
          Text='<%# Eval("price") %>'></asp:TextBox> 
        </EditItemTemplate> 
       </asp:TemplateField> 

      </Columns> 
      <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" /> 
      <RowStyle BackColor="#FFFBD6" ForeColor="#333333" VerticalAlign="Top" /> 
      <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" /> 
      <PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" /> 
      <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" HorizontalAlign="Left" /> 
      <AlternatingRowStyle BackColor="White" /> 
     </asp:GridView> 
     <asp:Label ID="lblMessage" runat="Server" ForeColor="Red"></asp:Label> 

後面的代碼:

保護無效的Page_Load(對象發件人,EventArgs的){

 Page pageHolder = new Page(); 
     UserControl userControl = (UserControl)pageHolder.LoadControl("~/WebUserControl2.ascx"); 
     userControl.Controls.Add(GridView1); 
     holder2.Controls.Add(userControl); 
     if (!IsPostBack) 
     { 
      BindData(); 
     } 
    } 

    /// <summary> 
    /// Bind data to the grid 
    /// </summary> 
    private void BindData() 
    { 
     SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString); 
     SqlDataAdapter dAd = new SqlDataAdapter("select * from product", conn); 
     DataSet dSet = new DataSet(); 
     try 
     { 

      dAd.Fill(dSet,"product"); 
      GridView1.DataSource = dSet.Tables["product"].DefaultView; 
      GridView1.DataBind(); 
     } 
     catch (Exception ee) 
     { 
      lblMessage.Text = ee.Message.ToString(); 
     } 
     finally 
     { 
      dSet.Dispose(); 
      dAd.Dispose(); 
      conn.Close(); 
      conn.Dispose(); 
     } 
    } 

    /// <summary> 
    /// fires when edit link is clicked 
    /// </summary> 
    /// <param name="sender"></param> 
    /// <param name="e"></param> 
    protected void EditRecord(object sender, GridViewEditEventArgs e) 
    { 
     GridView1.EditIndex = e.NewEditIndex;    
     BindData(); 
    } 
    /// <summary> 
    /// fires when cancel link is clicked 
    /// </summary> 
    /// <param name="sender"></param> 
    /// <param name="e"></param> 
    protected void CancelRecord(object sender, GridViewCancelEditEventArgs e) 
    { 
     GridView1.EditIndex = -1; 
     BindData(); 
    } 
    /// <summary> 
    /// fires when update link is clicked 
    /// </summary> 
    /// <param name="sender"></param> 
    /// <param name="e"></param> 
    protected void UpdateRecord(object sender, GridViewUpdateEventArgs e) 
    { 
     GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex]; 

     //int autoid = Int32.Parse(GridView1.DataKeys[e.RowIndex].Value.ToString()); 
     TextBox tPageName = (TextBox)row.FindControl("txtpid"); 
     TextBox tPageDesc = (TextBox)row.FindControl("txtpname"); 
     TextBox dActive = (TextBox)row.FindControl("txtprice"); 


     SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ToString()); 
     SqlCommand dCmd = new SqlCommand(); 
     try 
     { 
      conn.Open(); 
      dCmd.CommandText = "spUpdateData"; 
      //dCmd.CommandType = CommandType.StoredProcedure; 
      // dCmd.Parameters.Add("@AutoID", SqlDbType.Int).Value = autoid; 
      dCmd.CommandText = @"insert into [product](pid,pname,price)values(@pid,@pname,@price)"; 
      dCmd.Parameters.Add("@pid", SqlDbType.Char, 10).Value = tPageName.Text.Trim(); 
      dCmd.Parameters.Add("@pname", SqlDbType.VarChar, 50).Value = tPageDesc.Text.Trim(); 
      dCmd.Parameters.Add("@price", SqlDbType.VarChar,50).Value = dActive.Text.Trim(); 
      dCmd.Connection = conn; 
      dCmd.ExecuteNonQuery(); 

      lblMessage.Text = "Record Updated successfully."; 


      // Refresh the data 
      GridView1.EditIndex = -1; 
      BindData(); 
     } 
     catch (SqlException ee) 
     { 
      lblMessage.Text = ee.Message; 
     } 
     finally 
     { 
      dCmd.Dispose(); 
      conn.Close(); 
      conn.Dispose(); 
     } 

    } 

回答

1

我看不到你的更新按鈕在你的Gridview中。

好吧,無論如何,只要確保有的CommandName =「更新」你的更新按鈕爲了RowUpdating事件正確觸發。

相關問題