views:

10447

answers:

3

Currently, I have a dynamically created gridview on my page. When a user enters something in a textbox and presses a button, the whole page refreshes to populate the gridview and make it visible. I do not want that anymore. How would I go about using the UpdatePanel to make the gridview visible and populate it?

<div class="span-93 prepend-2 top">
  <strong>Enter  Number</strong><br />
  <asp:TextBox ID="PartNumber" runat="server" Width="100"></asp:TextBox>
  <asp:Button ID="CreateButton" runat="server" Width="85" Text="Locate" OnClick="CreateButton_Click" />
 </div>
<asp:Label ID="Select" runat="server" Font-Bold="true" Text="Select choice" Visible="false"></asp:Label><br />
   <ajax:UpdatePanel ID="UpdatePanel" runat="server" UpdateMode="Conditional">
    <ContentTemplate>
     <asp:GridView ID="GridView" Visible="false" runat="server"  HeaderStyle-Width="200" HeaderStyle-BackColor="#2B6292" HeaderStyle-ForeColor="White" 
     AllowSorting="true" AllowPaging="true" Width="600" AutoGenerateColumns="False" OnRowCreated="GridView_OnRowCreated" 
     DataKeyNames="Id" onsorting="GridView_OnSort">
      <Columns>
       ...
      </Columns>
     </asp:GridView>
    </ContentTemplate>
    <Triggers>
     <ajax:AsyncPostBackTrigger ControlID="CreateButton"/>
    </Triggers>
   </ajax:UpdatePanel>

Theres another button on the page called CreateButton, obviously, that will populate the gridview and make it visible so a user can select from it. Is this possible? Thanks in advance.

Edit: Binding Code to gridview:

    protected void Create_Click(object sender, EventArgs e)
     {
      if (!String.IsNullOrEmpty(Number.Text))
      {
       BLL newbll = new BLL();
       Database.DataTable tempTable = newbll.GetItemByPartNumber(Number.Text);

       if (Table.Count != 0)
       {
    DataTable table = tempTable ;

    string[] VID = { "Id" };
    GridviewDiv.Visible = true;
    GridView.DataSource = table;
    GridView.DataKeyNames = VID;
    GridView.DataBind();
   }
  }
 }
A: 

Since your update panel's update mode is set to Conditional, in your code behind after your call to DataBind on the grid view, you will need to call Update() on the name of your Update Panel (named UpdatePanel in this instance).

Lance Harper
I got Error on page saying: Sys.InvalidOperationException: Could not find UpdatPanel with ID ctl00_Main_GridviewUpPanel. If it is being updated dynamically then it must be inside another UpdatePanel
First of all, is "UpdatPanel" the name of your update panel, or is it misspelled? It's named "UpdatePanel" in your example. Is your update panel nested inside of another one? You'll have to traverse the child controls within the outside update panel to find the update panel you need to update.
Lance Harper
Lance - The name of it is UpdatePanel, it was just misspelled, and no, it's not inside of another update panel.
Could you post what your binding code looks like in the code behind?
Lance Harper
Beneath your call to GridView.DataBind(), are you able to reference your update panel? It should show up in Intellisense and that means the page will know of its existence. Just call the Update() method from it.
Lance Harper
Lance, I have done that and it still shows it couldnt find it for the error on page.
A: 

Having declared the CreateButton as a trigger you shouldn't have to explicitly call Update() to do the refresh

Where have you declared the CreateButton ? - it must be in the same 'NamingContainer' as the UpdatePanel for it to be found

Can you move the declaration of the CreateButton inside your update panel. If so you do not need to explicitly declare a trigger - see if that makes a difference

Tom Carter
Tom, I can't because the button in question has to be next to the initial text box. When that gets clicked, a gridview below that becomes visible and populated with the code-behind.
OK...But (without an explicit call to Update()) does clicking on the button cause : the gridview to display, a full refresh to occur, nothing at all or is there some error message displayed (or compilation error) ?
Tom Carter
When I click the button to show the gridview, nothing happens and I get an error on page, which I have stated in the first comments posted by RSolberg
You need to verify that that the Naming Container of the Button is the same as that of the UpdatePanel - where is the CreateButton declared ?
Tom Carter
Edited, I have declared the createbutton above the updatepanel
A: 

Use the following

<asp:UpdatePanel ID="..." runat="server" UpdateMode="Conditional" ChildrenAsTriggers="true">       
    <ContentTemplate>
      <asp:GridView  ....>
      </asp:GridView .....>

    ///buttons ..............whatever

    </ContentTemplate>