Solved

When Accessing repeater items of repeater in AJAX Updatepanel, it returns wrong values after update

Posted on 2008-10-30
8
1,165 Views
Last Modified: 2013-12-17
Hi,

I've got a repeater control within an AJAX update panel. The repeater shows product info and a textbox where the user can insert a quantity to order.

Because there are too many products to show them all, I only show products starting with 'A', and above the repeater links with 'B', 'C', etc. If the user clicks the 'B'-link, the repeater is newly databound with products starting with 'B'. As the repeater is in an updatepanel, this happens without postback as wanted.

This works fine and the Repeater shows the right products. The problem is, that if the user clicks the submit button on the page, i loop through the repeateritems. BUT: the repeater items contain values from the page that was originally loaded; lblID still contains the IDs from the 'A' products instead of the correct ID for the 'B'-products.

What am i doing wrong? How can i access the current values of the repeater items?


 In simplified form, the repeater looks like this:
<asp:Repeater ID="rptProducts" runat="server">
 <ItemTemplate>
  <tr>
   <td><asp:Label ID="lblID" runat="server" Visible="false"></asp:Label></td>
   <td><asp:TextBox ID="txtQuantity" Columns="1"  runat="server" EnableViewState="false"></asp:TextBox></td>
  </tr>
 </ItemTemplate>
</asp:Repeater>

In the button click handler, i'm looping like this:
foreach (RepeaterItem rpi in rptProducts.Items)
{
 TextBox txtQuantity = (TextBox)rpi.FindControl("txtQuantity");                
 Label lblID = (Label)rpi.FindControl("lblID");
}

Thanks!
0
Comment
Question by:dready
  • 4
  • 4
8 Comments
 
LVL 12

Expert Comment

by:techExtreme
Comment Utility
Hello,
first a quick fix:
If your whole repeater is in an update panel and your links are not, put them into one also.

Also, if your update panel is not updated automatically, i.e. its updatemode is 'Conditional', make sure you update it on each link click i.e. either user clicks a or b or c... your update your panel.
Basically the moment your repeater binds, update your update panel.
Also, put your submit button in update panel as well.


Now if this isn't the case,
I'm arranging your controls here, using three udpate panels (All having updatemode set to always)


<updatepanel.... id"upanel1"...
  your 'A','B','C'.. links here
</updatepanel


<updatepanel.... id"upanel2"...
  your repeater control here
</updatepanel


<updatepanel.... id"upanel1"...
  your submit button here
</updatepanel

Plz note if you go by this solution , i.e. updatemode set to always, you need not call the .Update method of the updatepanel , which otherwise you must call all time if conditional mode is et.
Hopefully this will be your solution.
Let me know if any doubt/issue.

Happy Coding!
0
 
LVL 11

Author Comment

by:dready
Comment Utility
Hi TechExtreme,

Thanks for your reply. I've been playing around with the updatepanels quite a bit. My current situation is like this:
<updatepanel id="updatepanel1"..
my 'A', 'B', links
my repeater control
my button
</updatepanel

The update panel is not set to conditional mode. I do not call update() anywhere.

So basically the whole page is in an update panel now. I realise this sort of makes the partial update function of the UpdatePanel useless, but i'd first like to get this to work and at least the browserhistory doesn't get full when clicking a lot of different letter-links.
Like I said before: all the updates seem to take place and the right products are shown. Only when accessing the values of the repeater items in the click handler it shows the old values.

Anything set up wrongly like this?
0
 
LVL 12

Expert Comment

by:techExtreme
Comment Utility
Hi,
this is even better, you have everything in one place.

If you are able to see the new values in the page which is rendered,
you should get them in the backend in the server side code.

However to be more sure, can you paste the backend code event handlers here
so that i can have a look at how you are fetching stuff and why old values
are persisted?
0
 
LVL 11

Author Comment

by:dready
Comment Utility
Here the simplified version of my code behind
* Repeater is page variable:
protected System.Web.UI.WebControls.Repeater rptProducts;

* Letter-link Click handler:
private void lbB_Click(object sender, EventArgs e)
{
            rptProducts.DataSource = BLL.PricelistManager.GetPriceList("b");
            rptProducts.DataBind();            
}

Databind
private void rptProducts_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
switch (e.Item.ItemType)
{
    case ListItemType.AlternatingItem:
    case ListItemType.Item:
        ProductEntity pe = (ProductEntity)e.Item.DataItem;
        Label lblID = (Label)e.Item.FindControl("lblID");
        lblID.Text = pe.ID.ToString();
        ... etc
        break;
    default:
        break;
}
}

Submit click:
private void btnSubmit_Click(object sender, ImageClickEventArgs e)
{  
    foreach (RepeaterItem rpi in rptProducts.Items)
   {
    TextBox txtQuantity = (TextBox)rpi.FindControl("txtQuantity");                
    Label lblCode = (Label)rpi.FindControl("lblCode"); //here it contains wrong value
    }  
}
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 12

Accepted Solution

by:
techExtreme earned 500 total points
Comment Utility
I hope the lblCode what you are referring in the submit button click is
set properly in the itemdatabound of the repeater as I can only see
lblID being bound.

Otherthan that, your code seems clean. However I have one doublt, did you
dragged and dropped the repeater on your page or it is dynamically added to
the page?
0
 
LVL 11

Author Comment

by:dready
Comment Utility
sorry, the lblCode is actually lblID. For making the code easier to understand, i renamed the original name of lblCode to lblID, but just forgot to do that in the submit button click.

I didn't drag and drop the repeater on the page.
I just typed in <repeater.... > lots of typing</repeater> in the aspx page, and added a page variable to the code behind manually, and in InitializeComponent i added

this.rptProducts.ItemDataBound += new RepeaterItemEventHandler(rptProducts_ItemDataBound);

and created the handler.

Anything forgotten there maybe?
0
 
LVL 11

Author Comment

by:dready
Comment Utility
Hey TechExtreme!

I found the problem!!!! After you said all looks OK, i started searching for a cause away from the repeater and updatepanel issue; Now, the page I used has a different way of handling the viewstate; it doesn't derive from page class, but some custom class that persists viewstate to a file on the server. I just tried what would happen if i derive this page from Page again, and then it worked!!!!! So the issue lies in the 'persistViewstateToFile' class and not in the UpdatePanel;

Thanks so much for narrowing down the problem for me. Full points more then deserved!!!!!!!
0
 
LVL 12

Expert Comment

by:techExtreme
Comment Utility
Hia,
Thanks. Yes persisting viewstate in a file does reduce some overhead as the page becomes light, but you never know if you intruduce other bugs because of it.
Happy Coding!
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
OverviewThis article demonstrates a simple search form using AJAX. The purpose of the article is to demonstrate how to use the same code to render a page and javascript (JQuery) and AJAX to make subsequent calls to refine the results. The princip…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now