Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 8th, 2008, 04:12 PM
Authorized User
 
Join Date: Apr 2008
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default gridview postback

hi

i have a little problem... this is the situation: i created a class which inherits from gridview class (i did this so i can use gridview event and methods), i'm loading dynamically (i need to do this because i read the information of how many webcontrols i need in the webpage from .ini file) this class into another class which returns to my aspx page when i click the button and then shows the information i need, to here everything it's ok, but when i'm trying to use the gridview events (previously declared in the class which inherits) to select a row for example, the gridview does a postback and the page is going to nothing... the information is not there anymore

i think this is because i'm not handling correctly the postback event...

what do you think about this?? can you give me an advice??


thanks


BR

 
Old April 9th, 2008, 03:16 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

Are you setting the ID of the controls? Without the ID being set the ASP.Net engine cannot work out which control fired the event.

/- Sam Judson : Wrox Technical Editor -/
 
Old April 9th, 2008, 10:07 AM
Authorized User
 
Join Date: Apr 2008
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

yes, in fact, the controls id are also dynamically setted

 
Old April 9th, 2008, 10:14 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

It's very hard to guess what might be the problem without seeing some code.

Would it be possible to post a snippet of your Page_Load code so we can check you are doing everything correctly (specifically the bit where you load the controls).

/- Sam Judson : Wrox Technical Editor -/
 
Old April 9th, 2008, 10:33 AM
Authorized User
 
Join Date: Apr 2008
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

ok, the "problem" here is that i'm not loading the controls in the Page_Load event, these controls are loaded when i click a button... look:

this is the button i click to get all the controls i need in the page...

protected void btnSubmit_Click(object sender, EventArgs e)
{
   ArrayList list = new ArrayList();

   list = biz.Update(txt.Text, iniFilePath, pageID);
   foreach (Control ctrl in list)
   {
     Form.Controls.Add(ctrl);
   }
}
-------
this is another class: this class reads an .ini file and gets the controls i must have in the page and those lines down are to create only the gridview
---
---
try
{
  myGrid = oGrid._AddGrid(oGrid, dTGrid);
  ComponentArray.Add(myGrid);
}
---
---
this is another class: this one, creates an object type Control to hold the gridview
--
--
public Control _AddGrid(Component objGrid, DataTable dT)
{
  usrGrd = new UserGridView(objGrid.ComponentID, objGrid.ComponentTop, objGrid.ComponentLeft, objGrid.ComponentWidth, dT);

  return usrGrd;
}
--
--
this is another class: this one, creates the gridview... inherits from System.Web.UI.WebControls.Gridview
--
--
public class UserGridView : System.Web.UI.WebControls.GridView
    {
        public UserGridView(string grdId, int top, int left, Unit width, DataTable dT)
        {
            this.SelectedIndexChanged += new EventHandler(UserGridView_SelectedIndexChanged);
            this.DataBound += new EventHandler(UserGridView_DataBound);

            this.ID = grdId;
            this.AutoGenerateColumns = true;
            this.AutoGenerateSelectButton = true;
            this.EmptyDataText = "No data found";
            this.ForeColor = System.Drawing.Color.Yellow;
            this.ShowHeader = true;
            this.HeaderStyle.ForeColor = System.Drawing.Color.Cyan;
            this.RowStyle.HorizontalAlign = HorizontalAlign.Center;
            this.SelectedRowStyle.BackColor = System.Drawing.Color.Gray;
            this.GridLines = GridLines.None;
            //this.DataKeyNames = dataKeyNames;
            this.Style["Position"] = "Absolute";
            this.Style["Top"] = top + "px";
            this.Style["Left"] = left + "px";
            this.Width = width;
            this.DataSource = dT;
            this.DataBind();
        }

        protected void UserGridView_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void UserGridView_DataBound(object sender, EventArgs e)
        {

        }
    }

i hope you can understand the code and the logic of the application...
 
Old April 9th, 2008, 03:57 PM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

OK, if the controls are being added in the submit button click, and then the page is reloaded (i.e. when a row is selected or something) then the controls will no longer exist, so no events get fired.

/- Sam Judson : Wrox Technical Editor -/
 
Old April 10th, 2008, 01:46 AM
Authorized User
 
Join Date: Sep 2007
Posts: 92
Thanks: 0
Thanked 0 Times in 0 Posts
Default

You have to create dynamic controls at Page_Init. If they are created after that they disappear during page load just like samjudson said.

If you can't create controls at that point you can at least get their values after postback from Request object. Use some prefix in dynamically created control's IDs to separate them from all the other controls and values. Little example:

Code:
const string dynControlPrefix = "dyn_";

void Page_Init(object sender, EventArgs e) {
    foreach (string key in Request.Params.AllKeys) {
        if (key.StartsWith(dynControlPrefix)) {
            string value = Request[key];
        }
    }
}





Similar Threads
Thread Thread Starter Forum Replies Last Post
Get GridView Cell Value Based on GridView Column stublair C# 2008 aka C# 3.0 0 September 4th, 2008 08:30 AM
Postback grstad ASP.NET 2.0 Basics 3 June 13th, 2008 07:56 AM
postback sameer_1981 ASP.NET 1.0 and 1.1 Basics 1 February 23rd, 2007 09:36 AM
Custom Postback and Gridview refresh wirerider ASP.NET 2.0 Basics 5 February 28th, 2006 04:33 PM
Postback raulsormus Classic ASP Basics 0 February 22nd, 2005 09:36 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.