ASP, C#.net

K

Kbalz

Having a hell of a time with this..

I have a user control. The control is a serach interface for my
database, and each user can customize which fields to search by, and
which return in the results. This information is stored in SQL Server.
Based on that information, I dynamically add text boxes and labels as
the user loads the user control. When the search is performed, I
dynamically add Columns to my gridview based on the SQL Server
information. The grid view also has a command field "open", so when
clicked the userControl sends some information to the parent page (via
eventhandler).

This usercontrol can be dropped into any page.. Everything is working
with the user control. The parent page works with the eventhandler.
Within the usercontrol's codebehind, I had to add an override to the
OnInit method, since I'm using dynamic controls.. I did this as many
sites advised, to prevent losing the dynamic controls and their values
when the control performs a postback.

The problem occurs when the parent page performs a post back.. The
text boxes in the userControl still render, and keep their values..
but the gridview seems to lose its DataSource, and therefore returns
no rows. Note that the parent page is not doing any tricks with
viewstate, or oninit overrides.. I've tried a few things, but can NOT
get the gridview to retain its DataSource..

I think today I'm going to write up a watered down version that still
shows the problem with lots of code for someone to help me test with..

Does anyone have any ideas off the top of their head that I can try?
 
K

Kbalz

Having a hell of a time with this..

I have a user control. The control is a serach interface for my
database, and each user can customize which fields to search by, and
which return in the results. This information is stored in SQL Server.
Based on that information, I dynamically add text boxes and labels as
the user loads the user control. When the search is performed, I
dynamically add Columns to my gridview based on the SQL Server
information. The grid view also has a command field "open", so when
clicked the userControl sends some information to the parent page (via
eventhandler).

This usercontrol can be dropped into any page.. Everything is working
with the user control. The parent page works with the eventhandler.
Within the usercontrol's codebehind, I had to add an override to the
OnInit method, since I'm using dynamic controls.. I did this as many
sites advised, to prevent losing the dynamic controls and their values
when the control performs a postback.

The problem occurs when the parent page performs a post back.. The
text boxes in the userControl still render, and keep their values..
but the gridview seems to lose its DataSource, and therefore returns
no rows. Note that the parent page is not doing any tricks with
viewstate, or oninit overrides.. I've tried a few things, but can NOT
get the gridview to retain its DataSource..

I think today I'm going to write up a watered down version that still
shows the problem with lots of code for someone to help me test with..

Does anyone have any ideas off the top of their head that I can try?

I was able to fix this.. what I ended up doing was storing the
gridview's datasource in a session state. And in the UserControl's
overrided OnInit method, I pulled the Session value, cast it to
SqlDataSource, re bound it, and dynamically re added the columns
(which was its own method I just had to call).. here is the OnInit

initSearcTerms() - Adds Dynamic text boxes
addColumnsToResults() Adds Dynamic Columns to GridView

protected override void OnInit(EventArgs e)
{
base.OnInit(e);

if (IsPostBack)
{
initSearchTerms();
object gv = Session["gv"];
try
{
addColumnsToResults();
SqlDataSource sds = (SqlDataSource)gv;
gvSearchResults.DataSource = sds;
gvSearchResults.DataBind();
}
catch(Exception e1)
{
//its null, just ignore.
}
}
}


When the user clicks search, or uses the command button "open" - the
datasource is updated into the session value.. Sorry for lack of the
rest of the code, maybe this will still help someone.
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

And that's the solution, if you add controls dynamically you need to
recreate them in postback

--
Ignacio Machin
http://www.laceupsolutions.com
Mobile & warehouse Solutions.
Kbalz said:
Having a hell of a time with this..

I have a user control. The control is a serach interface for my
database, and each user can customize which fields to search by, and
which return in the results. This information is stored in SQL Server.
Based on that information, I dynamically add text boxes and labels as
the user loads the user control. When the search is performed, I
dynamically add Columns to my gridview based on the SQL Server
information. The grid view also has a command field "open", so when
clicked the userControl sends some information to the parent page (via
eventhandler).

This usercontrol can be dropped into any page.. Everything is working
with the user control. The parent page works with the eventhandler.
Within the usercontrol's codebehind, I had to add an override to the
OnInit method, since I'm using dynamic controls.. I did this as many
sites advised, to prevent losing the dynamic controls and their values
when the control performs a postback.

The problem occurs when the parent page performs a post back.. The
text boxes in the userControl still render, and keep their values..
but the gridview seems to lose its DataSource, and therefore returns
no rows. Note that the parent page is not doing any tricks with
viewstate, or oninit overrides.. I've tried a few things, but can NOT
get the gridview to retain its DataSource..

I think today I'm going to write up a watered down version that still
shows the problem with lots of code for someone to help me test with..

Does anyone have any ideas off the top of their head that I can try?

I was able to fix this.. what I ended up doing was storing the
gridview's datasource in a session state. And in the UserControl's
overrided OnInit method, I pulled the Session value, cast it to
SqlDataSource, re bound it, and dynamically re added the columns
(which was its own method I just had to call).. here is the OnInit

initSearcTerms() - Adds Dynamic text boxes
addColumnsToResults() Adds Dynamic Columns to GridView

protected override void OnInit(EventArgs e)
{
base.OnInit(e);

if (IsPostBack)
{
initSearchTerms();
object gv = Session["gv"];
try
{
addColumnsToResults();
SqlDataSource sds = (SqlDataSource)gv;
gvSearchResults.DataSource = sds;
gvSearchResults.DataBind();
}
catch(Exception e1)
{
//its null, just ignore.
}
}
}


When the user clicks search, or uses the command button "open" - the
datasource is updated into the session value.. Sorry for lack of the
rest of the code, maybe this will still help someone.
 
K

Kbalz

Hi,

And that's the solution, if you add controls dynamically you need to
recreate them in postback

--
Ignacio Machinhttp://www.laceupsolutions.com



I was able to fix this.. what I ended up doing was storing the
gridview's datasource in a session state. And in the UserControl's
overrided OnInit method, I pulled the Session value, cast it to
SqlDataSource, re bound it, and dynamically re added the columns
(which was its own method I just had to call).. here is the OnInit
initSearcTerms() - Adds Dynamic text boxes
addColumnsToResults() Adds Dynamic Columns to GridView
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
if (IsPostBack)
{
initSearchTerms();
object gv = Session["gv"];
try
{
addColumnsToResults();
SqlDataSource sds = (SqlDataSource)gv;
gvSearchResults.DataSource = sds;
gvSearchResults.DataBind();
}
catch(Exception e1)
{
//its null, just ignore.
}
}
}
When the user clicks search, or uses the command button "open" - the
datasource is updated into the session value.. Sorry for lack of the
rest of the code, maybe this will still help someone.- Hide quoted text -

- Show quoted text -

Do you forsee any improvements in that area for future version
of .NET? It is a lot of extra work IMO!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top