Solution of the viewstate problem: http://www.denisbauer.com/ASPNETControls/DynamicControlsPlaceholder.aspx
-----Original Message----- From: Dean Fiala [mailto:[EMAIL PROTECTED] Sent: donderdag 29 april 2004 5:27 To: [EMAIL PROTECTED] Subject: RE: [AspNetAnyQuestionIsOk] Please Please Help - DataGrid ItemCommand does not fire. Yes, You have run into one of the joys of working with dynamic control creation. When your page posts back on the button click, the button doesn't exist in the viewstate of the form, hence when the form is recreated, it has no buttons and the argument sent with the postback is lost. There are two solutions for this: 1) Add these buttons at design time, this includes the button's in the form's viewstate. You don't seem to have any conditional logic for creating them, so this would be the simplest approach. 2) if you want to create them dynamically, do it in a method. Call it AddColumns. Then override the LoadViewState method on the Page to call AddColumns, so they will be reloaded before PageLoad is called and you will not lose the command info and your event will fire. Note you will also want to call AddColumns initially from a code path that is in !Page.IsPostBack (so they are created when the page initially loads). here's an example. It will only reload the columns on postback if they were loaded initially (if you decide to add them conditionally). protected override void LoadViewState(object savedState) { base.LoadViewState(savedState); if((bool)ViewState[HasColumns]) AddColumns(); } private void AddColumns() { ButtonColumn btD = new ButtonColumn(); btD.Text =Delete; btD.CommandName =Delete; btD.ButtonType = ButtonColumnType.PushButton; dg.Columns.Add(btD); ViewState[HasColumns] = true; } HTH, Dean Fiala ----------------------------- Very Practical Software, Inc. http://www.vpsw.com/links.aspx -----Original Message----- From: Rajendra Appalla [mailto:[EMAIL PROTECTED] Sent: Wednesday, April 28, 2004 10:19 PM To: [EMAIL PROTECTED] Subject: [AspNetAnyQuestionIsOk] Please Please Help - DataGrid ItemCommand does not fire. Hi, Can anybody please help me with this issue. The paging works beautifully. I set the viewstate of DataGrid to FALSE, but using a ViewState variable I managed to do the paging with custompaging. I also used a StoredProcedure to just retrieve the records for that page. Everything works fine so far. But the buttons in the buttoncolumns of DataGrid, when clicked does not fire the ItemCommand event. Here is the code below. I request you all to please please help me with this issue. I am very eagerly waiting for some solution. private void Page_Load(object sender, System.EventArgs e) { DataGrid1.AutoGenerateColumns = false; DataGrid1.EnableViewState = false; DataGrid1.AllowCustomPaging = true; DataGrid1.PageSize = 2; DataGrid1.ItemCommand += new DataGridCommandEventHandler(DataGrid1_ItemCommand); btFirst.Command += new CommandEventHandler(NavigationButtonClick); btPrevious.Command += new CommandEventHandler(NavigationButtonClick); btNext.Command += new CommandEventHandler(NavigationButtonClick); btLast.Command += new CommandEventHandler(NavigationButtonClick); if(!IsPostBack) { ViewState["pageIndex"]=1; BindGrid(); } } protected void BindGrid() { string strSql = "a SELECT statement that gets column names I use as HeaderText of Boundcolumns"; myAdapter = new SqlDataAdapter(strSql, strConn); myDataSet = new DataSet(); myAdapter.Fill(myDataSet, "dtColumns"); DataGrid1.DataSource = myDataSet.Tables["dtColumns"]; int numRows = myDataSet.Tables["dtColumns"].Rows.Count; //Here are the button columns that should fire the ItemCommand event ButtonColumn btnDelete = new ButtonColumn(); btnDelete.Text = "Delete"; btnDelete.CommandName="Delete"; btnDelete.ButtonType = ButtonColumnType.PushButton; DataGrid1.Columns.Add(btnDelete); ButtonColumn btnEdit = new ButtonColumn(); btnEdit.Text = "Edit"; btnEdit.ButtonType = ButtonColumnType.PushButton; btnEdit.CommandName="Edit"; DataGrid1.Columns.Add(btnEdit); for(i=0; i < numRows; i++) { BoundColumn bndColumn = new BoundColumn(); bndColumn.HeaderText = myDataSet.Tables["dtColumns"].Rows[i]["tblLabel"].ToString(); bndColumn.DataField = myDataSet.Tables["dtColumns"].Rows[i]["FieldName"].ToString(); DataGrid1.Columns.Add(bndColumn); } //I omitted some code that works with getting the parameters to send to storedprocedure. sqlConn = new SqlConnection(strConn); SqlCommand sqlCmd = new SqlCommand("GetData_SP", sqlConn); sqlCmd.CommandType = CommandType.StoredProcedure; //I omitted some code here - list of command parameters to pass to stored procedure here. sqlConn.Open(); myReader = sqlCmd.ExecuteReader(); DataGrid1.DataSource = myReader; DataGrid1.DataBind(); myReader.Close(); sqlConn.Close(); //using these to keep track of the page I am at while paging if(!(Page.IsPostBack)) { if(intRecordCount % DataGrid1.PageSize == 0) intTotalPages = intRecordCount/DataGrid1.PageSize; else intTotalPages = ((int)(intRecordCount/DataGrid1.PageSize)) + 1; } } protected void NavigationButtonClick(object sender, CommandEventArgs e) { string direction = ((Button)sender).CommandName; switch (direction.ToUpper()) { case "FIRST" : ViewState["pageIndex"]= 1; break; case "PREVIOUS" : if((int)(ViewState["pageIndex"]) <= intTotalPages && (int)(ViewState["pageIndex"]) > 0) ViewState["pageIndex"] = Convert.ToInt32(ViewState["pageIndex"]) - 1; break; case "NEXT" : if((int)(ViewState["pageIndex"]) < intTotalPages && (int)(ViewState["pageIndex"]) >= 0) ViewState["pageIndex"] = Convert.ToInt32(ViewState["pageIndex"]) + 1; break; case "LAST" : ViewState["pageIndex"] = intTotalPages; break; default : break; } BindGrid(); } public void DataGrid1_ItemCommand(object sender, DataGridCommandEventArgs e) { if (e.CommandName == "Delete") { Delete(id); } else if (e.CommandName=="Edit") { Edit(id); } } void Delete(int id) { //some code to do here BindGrid(); } void Edit(int id) { //somecode to do here } //I am using this event as I am custompaging protected void PageIndexChanged(Object sender, DataGridPageChangedEventArgs e) { DataGrid1.CurrentPageIndex = e.NewPageIndex; ViewState["pageIndex"] = Convert.ToInt32(e.NewPageIndex); BindGrid(); } } } [Non-text portions of this message have been removed] Yahoo! Groups Links Yahoo! Groups Links ------------------------ Yahoo! Groups Sponsor ---------------------~--> Buy Ink Cartridges or Refill Kits for your HP, Epson, Canon or Lexmark Printer at MyInks.com. Free s/h on orders $50 or more to the US & Canada. http://www.c1tracking.com/l.asp?cid=5511 http://us.click.yahoo.com/mOAaAA/3exGAA/qnsNAA/saFolB/TM ---------------------------------------------------------------------~-> Yahoo! Groups Links <*> To visit your group on the web, go to: http://groups.yahoo.com/group/AspNetAnyQuestionIsOk/ <*> To unsubscribe from this group, send an email to: [EMAIL PROTECTED] <*> Your use of Yahoo! Groups is subject to: http://docs.yahoo.com/info/terms/
