Solved

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

Posted on 2008-10-30
8
1,179 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
8 Comments
 
LVL 12

Expert Comment

by:techExtreme
ID: 22842088
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
ID: 22842242
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
ID: 22842313
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
Learn by Doing. Anytime. Anywhere.

Do you like to learn by doing?
Our labs and exercises give you the chance to do just that: Learn by performing actions on real environments.

Hands-on, scenario-based labs give you experience on real environments provided by us so you don't have to worry about breaking anything.

 
LVL 11

Author Comment

by:dready
ID: 22842459
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
 
LVL 12

Accepted Solution

by:
techExtreme earned 500 total points
ID: 22842530
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
ID: 22842589
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
ID: 22842820
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
ID: 22843374
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

Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
The most up-to-date version of this article is on my Blog https://iconoun.com/blog/
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …

688 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