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
Solved

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

Posted on 2008-10-30
8
1,173 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
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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Iteration Help (Asp.net VB) 5 24
Formula for calculating ROI on training 6 49
Input parameteres to DragOver 2 19
Code works but breaks when I add one section 4 20
Introduction Got endorsements from your clients?  Great!  There is almost nothing better than word-of-mouth advertising.  But how can you do that on the internet?  Sure you can make a page for endorsement quotations and list them all, but who is …
The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

789 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