Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old April 8th, 2008, 04:12 PM
Authorized User
 
Join Date: Apr 2008
Location: , , .
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

Reply With Quote
  #2 (permalink)  
Old April 9th, 2008, 03:16 AM
samjudson's Avatar
Friend of Wrox
Points: 8,687, Level: 40
Points: 8,687, Level: 40 Points: 8,687, Level: 40 Points: 8,687, Level: 40
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2007
Location: Newcastle, , United Kingdom.
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 -/
Reply With Quote
  #3 (permalink)  
Old April 9th, 2008, 10:07 AM
Authorized User
 
Join Date: Apr 2008
Location: , , .
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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

Reply With Quote
  #4 (permalink)  
Old April 9th, 2008, 10:14 AM
samjudson's Avatar
Friend of Wrox
Points: 8,687, Level: 40
Points: 8,687, Level: 40 Points: 8,687, Level: 40 Points: 8,687, Level: 40
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2007
Location: Newcastle, , United Kingdom.
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 -/
Reply With Quote
  #5 (permalink)  
Old April 9th, 2008, 10:33 AM
Authorized User
 
Join Date: Apr 2008
Location: , , .
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...
Reply With Quote
  #6 (permalink)  
Old April 9th, 2008, 03:57 PM
samjudson's Avatar
Friend of Wrox
Points: 8,687, Level: 40
Points: 8,687, Level: 40 Points: 8,687, Level: 40 Points: 8,687, Level: 40
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2007
Location: Newcastle, , United Kingdom.
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 -/
Reply With Quote
  #7 (permalink)  
Old April 10th, 2008, 01:46 AM
Authorized User
 
Join Date: Sep 2007
Location: , , Finland.
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];
        }
    }
}
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 02:05 PM.


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