Solved

getting value of a label in a repeater

Posted on 2013-02-05
6
642 Views
Last Modified: 2013-02-10
i have a datarepeater in a win form using C# and I am trying to get the value of a label in the repeater but it keeps comming up as label3 on messagebox.show why wont it give me the value?
        private void ButtonRepeater()
        {
            if (DesignMode == false)
            {
                string sqlStr = "";
                string sqlCon = ConfigurationManager.ConnectionStrings["conn1"].ToString();
                sqlStr += " SELECT  id, TaskUID, Factory, ItemNumber, ItemName, ExFactoryDate, Customer, ReportDate, InspectorName, BatchCode, OverallResultChina, OverallResultDallas, ";
                sqlStr += " IsDupro, IsFinalRandomInsp, IsTestSamplePull, Uploaded, Downloaded, PDS_CC, WorkmanShip, OnSiteTest, BarcodeVer, Packaging, Packing, Lbl_Print_Mrkg, ";
                sqlStr += " ShipRemarks, IsCOmplete "; 
                sqlStr += " FROM         Inspection ";
                sqlStr += " WHERE     (IsCOmplete IS NULL) ";
                using(SqlConnection conn = new SqlConnection(sqlCon))
                using(SqlCommand cmd = new SqlCommand(sqlStr,conn))
                {
                    cmd.CommandType = CommandType.Text;
                    cmd.Connection.Open();
                    SqlDataAdapter da = new SqlDataAdapter(cmd);
                    DataTable dt = new DataTable();
                    da.Fill(dt);
                    BindingSource bindingSource1 = new BindingSource();
                    bindingSource1.DataSource = dt;
                    radLabel2.Text = "QC Inspection";
                    radLabel3.DataBindings.Add("Text", bindingSource1, "TaskUID");
                    radLabel4.DataBindings.Add("Text", bindingSource1, "Factory");
                    dataRepeater1.DataSource = bindingSource1;
                    
                }
            }
        }

Open in new window


        private void dataRepeater1_ItemTemplate_Click(object sender, EventArgs e)
        {
            MessageBox.Show(Convert.ToString(radLabel3.Text));
        }

Open in new window

0
Comment
Question by:r3nder
  • 5
6 Comments
 
LVL 20

Assisted Solution

by:Daniel Van Der Werken
Daniel Van Der Werken earned 500 total points
ID: 38854939
You have to do a FindControl() on the repeater.

Something like this, in the event:

        private void dataRepeater1_ItemTemplate_Click(object sender, EventArgs e)
        {
            Label radLabel = (Label)sender.FindControl("radLabel3");
            MessageBox.Show(Convert.ToString(radLabel.Text));
        }

Open in new window


Actually, I'm not sure what your HTML looks like, so I can't tell for sure. Basically, with repeaters and datagrids and such, you have to call FindControl on the repeater item or whatever object is being passed into the event that is of type repeater-something.

Make sense?
0
 
LVL 6

Author Comment

by:r3nder
ID: 38854996
I have no HTML - this is winform
0
 
LVL 6

Author Comment

by:r3nder
ID: 38855041
FindControl does not exist in this context :(
0
How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

 
LVL 6

Accepted Solution

by:
r3nder earned 0 total points
ID: 38855555
here is what i have - I need the value of radlabel3 of the clicked index
        private void dataRepeater1_ItemTemplate_Click(object sender, EventArgs e)
        {
            string index = Convert.ToString(dataRepeater1.CurrentItemIndex); <-- I can get the index
            
                RadLabel radLabel = this.Controls.Find("radLabel3", true).FirstOrDefault() as RadLabel; <--- but the label text is always the first indexes radlabel3
            
           
            MessageBox.Show(Convert.ToString(radLabel.Text));
        }

Open in new window

0
 
LVL 6

Author Comment

by:r3nder
ID: 38855820
got it
task = dataRepeater1.CurrentItem.Controls["radLabel3"].Text;
0
 
LVL 6

Author Closing Comment

by:r3nder
ID: 38872777
pointed me in the right direction - thanks
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.

820 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