?
Solved

ASP.Net/C# - FindControl Question

Posted on 2009-04-22
13
Medium Priority
?
533 Views
Last Modified: 2012-05-06
Hello all,
I have a repeater where in that repeater I have a label.  What I am doing is appending to the ID of the label control a JobID which is unique to each row.  For example:
 <asp:Label ID="lblPublish_<%#Eval("BILLING_JOB_ID")%>" runat="server" ForeColor="red"></asp:Label>

Now I store the control id and the text value it will have into a generic list so I can handle state with something because of AJAX being used.   When I do a findcontrol on the ID it is not working, however it was working if I just had it as "lblPublish".  I looked in the source and the ctrlName I put together is exact and there in the source code for the HTML.

RepeaterItem ri = btnPublish.NamingContainer as RepeaterItem;
string ctrlName = "lblPublish_" + btnPublish.CommandArgument;
Label lbl = (Label)ri.FindControl(ctrlName);

now its coming out null.  I have to append somehow to my generic list a control id or something so I know that is the control in my list row when I do looping later to write out some data.  Any ideas?
0
Comment
Question by:sbornstein2
  • 8
  • 5
13 Comments
 
LVL 39

Expert Comment

by:abel
ID: 24207452
> What I am doing is appending to the ID of the label control a JobID which is unique to each row.

that's interesting code and actually, it shouldn't work or compile. The unique ID is taken care of by the naming container which is, in this case, the Repeater. To find your control, you can use FindControl("lblPublish").
0
 
LVL 39

Expert Comment

by:abel
ID: 24207483
> I have to append somehow to my generic list a control id or something > so I know that is the control in my list row when I do looping later to write out some data.
The most common technique is simply using indexes and / or the child controls collection. If you need to have some ID in there that you would like to use to lookup, place some asp:Literal in there with Visible=False and set the Text property to the lookup key you want to use. But: this technique should not be necessary. All means to find your data are there already.


0
 

Author Comment

by:sbornstein2
ID: 24207794
so what happens is this:

I have this repeater and label in the repeater that shows a status of success or an error status for each table row that is created in the repeater.  

The repeater is actually in a UpdatePanel for ajax and there is a timer on the panel as well that goes off every few seconds.  So what happens is I populate the label in the repeater but as soon as the timer goes off and the update panel refreshes that control value is lost and goes blank.  So I needed a way to be able to keep those values for each row in state somehow.  

I created a generic list and I now store the Label ClientID and Label Text in the list and then store the list in session.  So everytime the timer goes off I still have the values around.  What I first was doing worked great but I thought too risky as I just used the corresponding row when I looped and an interger like int x = 0 outside the loop and then ++x as I went though and grabbed the value from that corresponding row in the generic list as they should always be the same in order.  However, I thought that was too risky and I wanted to be able to make sure I write the value back to the correct label no matter what if it exists still on the page.  The update panel also when the timer goes off grabs the dataset again and it could change.  So I was hoping maybe by using the ClientID I could do a findcontrol on that.

Hope this makes sense somewhat because of the complexity of the update panel and timer clearing it out.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:sbornstein2
ID: 24207840
one thing to add, so this btnPublish is also in the repeater in each table row.  When clicked on in that row some processing happens actually a file is moved from one server to another.  Then I pass back a status as success or for example file was locked etc.  Then I write that status back to the lblPublish in that row.  Works great but then the timer goes off and refreshes the update panel and those labels all get cleared.  
0
 

Author Comment

by:sbornstein2
ID: 24207886
when the timer goes off now, I loop first through the repeater and grab the text values of the lblPublish for each row of the repeater and the ClientID and store it to the generic list and then store that list in session.  Then right after that the repeater is bound again to the new dataset.  So I think that actually may be the issue.  Now once the repeater is bound again I wanted to write the values back from the generic list to the corresponding label controls somehow based on a findcontrol of that ID or something.
0
 
LVL 39

Accepted Solution

by:
abel earned 2000 total points
ID: 24207924
You seem to say that you are loosing the data that is in a table and is not displayed properly. What might help, actually, is setting the EnableViewState property on the controls containing the Label (or the label itself). This should retain the viewstate across browser requests.

Another option is to rebind the data control to the repeater, which, if you put the binding inside the page_load, is what should already be happening, even on ajax postback.
0
 

Author Comment

by:sbornstein2
ID: 24207944
ya see the thing is the success or error values for this lblPublish, that is not at all in my dataset nor do we want to store it right now which gets bound to the repeater.  So that was also my issue I have these values outside of the bound repeater dataset.
0
 

Author Comment

by:sbornstein2
ID: 24207970
okay set the enableviewstate = true on the label and it still went back to blank.
0
 
LVL 39

Expert Comment

by:abel
ID: 24208001
I'm afraid it is a little bit too much guessing considering all the other issues that could possibly be involved. Can you post your code, at least the code behind, please?
0
 

Author Comment

by:sbornstein2
ID: 24208007
ya the reason why is because the repeater is going to get rebound again and then that label of course will be created again and blank regardless of state I beleive.
0
 

Author Comment

by:sbornstein2
ID: 24208022
so the timer will go off in 3 seconds and then the new dataset is bound to the repeater and my values are gone.  I am wondering if I can somehow append the publish status to the dataset somehow after it is created from a stored proc and before being bound to the repeater.
0
 

Author Closing Comment

by:sbornstein2
ID: 31573419
figured out another way to handle this using the tooltip as a storage field
0
 
LVL 39

Expert Comment

by:abel
ID: 24209656
> figured out another way to handle this using the tooltip as a storage field

interesting twist, now the data remains valid html and the user sees something useful on the mousehover. Thanks for the granting the pts anyway :)
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses

862 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