Re: ASP, C#.net

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



On Nov 1, 8:01 am, Kbalz <Kurtas.Balc...@xxxxxxxxx> wrote:
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.

.



Relevant Pages

  • RE: user control - image button only works on second select
    ... So does the test page and usercontrol I sent you works as you expected? ... control, as for the main page and dropdownlist, I don't think there will ... user control - image button only works on second select ... | only working on selecting the button twice. ...
    (microsoft.public.dotnet.framework.aspnet.webcontrols)
  • Re: UserControl event & method call sequence
    ... a usercontrol on an aspx page, following are the order of events. ... It does not always use this user control ... >>> I have an aspx page that loads an ascx user control. ...
    (microsoft.public.dotnet.framework.aspnet.buildingcontrols)
  • Re: Context is not a member of....
    ... assuming that the code behind of the user control i.e ... Enter.ascx.vb resides in the App_Code directory, if I add the CodeFile ... If the code behind of the user control Enter.ascx.vb is placed in some ... TextBoxes are named txt1 & txt2. ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: Instantiate control at runtime
    ... property for the user control to true to go to the ... previous questions in the click event of a button control. ... Button click is not too late to create checkboxes. ... John Saunders ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: ASP, C#.net
    ... I have a user control. ... I had to add an override to the ... OnInit method, ... get the gridview to retain its DataSource.. ...
    (microsoft.public.dotnet.languages.csharp)