0
votes

I have a page from where a user can search for items and so on. I use a .ascx page to display the result in a grdview.

The aspx page only has a seacrh box and a button and on button click I want to pass the value from inside the textbox to the ascx page and have the gridview populate with the result. But after debugging I found that the gridview is set to null after the button click. All other values are there and the result comes back from the database.

Can some one please help. I have been looking for a solution for last few hours and have treid a few diffeerent things but still not getting anywhere.

thanks in advance

EDIT

Here is my code

This is the aspx page.

<%@ Register Src="Search.ascx" TagName="Search" TagPrefix="uc1" %>
    <form id="form1" runat="server">
        <div class="content">
            <div style="padding:5px;">
                <b>Search</b>: <asp:TextBox runat="server" ID="txtSearch" />
                <asp:RequiredFieldValidator ErrorMessage="*" ValidationGroup="SearchGroup" ControlToValidate="txtSearch" ID="RequiredFieldValidator1"
                    runat="server" />
            </div>
            <div style="padding:5px;">
                <asp:Button Text="Search" runat="server" ValidationGroup="SearchGroup" OnClick="btnSearch_Click" ID="btnSearch" />
            </div>
        </div>
        <asp:ScriptManager runat="server" />
        <asp:UpdatePanel runat="server">
            <ContentTemplate>
                <div class="Results">
                    <uc1:Search ID="UserControl1" runat="server" />
                </div>
            </ContentTemplate>
        </asp:UpdatePanel>

    </form>

In the ASCX page I have the following

The code behind

Dim test As Object = txtSearch.Text

Dim StockList As cls_Stock_Col = MyLOTSDB.LoadStock_Col()
Try
    Dim dt As DataTable = New DataTable()
    Dim dcol As New DataColumn("TradeName", GetType(System.String))
    dt.Columns.Add(dcol)

    'Create an ID column for adding to the Datatable
    dcol = New DataColumn("PackSize", GetType(System.String))
    dt.Columns.Add(dcol)

    dcol = New DataColumn("PLU", GetType(System.String))
    dt.Columns.Add(dcol)

    dcol = New DataColumn("SOH", GetType(System.String))
    dt.Columns.Add(dcol)

    dcol = New DataColumn("RetailAsCurrency", GetType(System.String))
    dt.Columns.Add(dcol)

    For Each col As DataColumn In dt.Columns
        'Declare the bound field and allocate memory for the bound field.
        Dim bfield As New BoundField()
        'Initalize the DataField value.
        bfield.DataField = col.ColumnName
        'Initialize the HeaderText field value.

        Dim DisplayName As String = ""
        Dim DataFormat As String = ""
        Dim FieldType As Type = col.GetType()
        Dim StringLength As Int32

        GetFieldDetails(TheType, col.ColumnName, DisplayName, DataFormat, FieldType, StringLength)
        bfield.HeaderText = DisplayName

        If (DataFormat = "$0.00") Then
            bfield.DataFormatString = "{0:C2}"
        End If


        bfield.SortExpression = "col.ColumnName"
        'Add the newly created bound field to the GridView.
        GridView1.Columns.Add(bfield)
    Next

    GridView1.DataSource = StockList
    GridView1.DataBind()
Catch ex As Exception

End Try

I can debug and the error is thrown when I try to add the column to the gridview,

Thanks again

2
Dynamically adding the user control to the page? Maybe this will help as it needs to exist before the button event fires. stackoverflow.com/questions/7306305/…Valamas
without code only theoretical assumptions you can get.Aristos
Instead of creating the gridview from code-behind, write the markup in the ascx filemshsayem
that wont work as I plan to use it for different searches. The code works if its on the aspx page but as soon as I move the code the the user control page it compains about the gridview being nulldogwasstar
Where does that initialization logic get called? For dynamically created controls you need to initialize them after each postback. Try calling that initialization logic for your GridView in your Page_Init method.user467384

2 Answers

0
votes
The aspx page only has a seacrh box and a button and on 
button click I want to pass the value from inside the textbox to the ascx page

You can do this by specifying a public property on your ascx control which you can access on calling page.

Inside your ascx code behind

private string searchText ;
Public string SetSearchText
{
      get { return searchText; }
      set { return searchText; }   
}  

In your calling page on button click, just set the property of the ascx control

Inside your main page on button click

YourAscxControl.SetSearchText= yourTextBox.Text.Trim();