How do I get the object id from sender when the object s in a materpage in .net 2010?

I have a page that uses a master page. In the content placeholder I have a update panel. In this update panel I am dynamically creating buttons. I need to be able to get the id from the sender object in the generic button event I have created.

I have tried to get the type of object by:
Type t = sender.GetType(), but The object comes back blank. I

 I have a feeling that it's because I have the object in a bunch of parent controls. How would I get the id back from the sender in this scenario?

Thanks in advance!

Both have gotten me nowhere. Is there someth id back from the sender?
bschave2Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bill NolanOwner, Lead Technology ProgrammerCommented:
Where does 'sender' come from?
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
bschave2Author Commented:
the object that sends it, but am getting nothing back.
0
bschave2Author Commented:
also tried  Button btn = ((Button)sender);
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Daniel Van Der WerkenIndependent ConsultantCommented:
try this.

Type t = typeof( sender );

make sure sender isn't null too.
0
bschave2Author Commented:
telling me that the type or namespace could not be found.

this is my .cs code...


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace CallOptionsAvailability
{
    public partial class dynamic_update_panel : System.Web.UI.Page
    {
        protected override void OnPreInit(EventArgs e)
        {
            base.OnPreInit(e);
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            for (int i = 0; i < 3; i++)
            {
                Table tbl = new Table();

                tbl.ID = "tbl" + i.ToString();
                tbl.Width = Unit.Pixel(800);

                for (int j = 0; j < 10; j++)
                {
                    TableRow tr = new TableRow();

                    for (int k = 0; k < 5; k++)
                    {
                        TableCell tc = new TableCell();
                        tc.Text = "tbl_" + i.ToString() + "_row_" + j.ToString() + "_col_" + k.ToString();
                        tr.Cells.Add(tc);
                    }

                    tbl.Rows.Add(tr);
                }

                Literal newLn = new Literal();
                newLn.Text = "<br />";

                Button button = new Button();
                button.Text = "Update Tbl";
                button.ID = "btn_tbl_" + i.ToString(); 
                button.Click += new EventHandler(HandleButtonClick);

                UpdatePanel1.ContentTemplateContainer.Controls.Add(tbl);
                UpdatePanel1.ContentTemplateContainer.Controls.Add(newLn);
                UpdatePanel1.ContentTemplateContainer.Controls.Add(button);
                UpdatePanel1.ContentTemplateContainer.Controls.Add(newLn);
            }
        }

        private void HandleButtonClick(object sender, EventArgs eventArgs)
        {
            Button btn = sender as Button;
            Type t = typeof(sender);
            string str = btn.ID.ToString();
            //Button btn = UpdatePanel1.TemplateControl.FindControl(str);

            string str1 = ((Control)sender).ClientID;
        }
    }
}

Open in new window

0
Bill NolanOwner, Lead Technology ProgrammerCommented:
a) is 'sender' null?
b) does 'Button btn = sender as Button;' work?
c) can you look over the object 'sender' in your debugger and see what it is?
0
bschave2Author Commented:
a) yes
b) it returns "{Text = Update Table}"
c) yes, I can see everything. I can even click on base{System.Web.UI.WebControls.WebControl} and then click on the base{System.Web.UI.Control} and I can see the id that I need. But when I try btn.id.tostring(), I get null returned.
0
Bill NolanOwner, Lead Technology ProgrammerCommented:
If 'sender' is null, how can you read its properties in the debugger?
0
bschave2Author Commented:
don't know!
0
Bill NolanOwner, Lead Technology ProgrammerCommented:
Check the type via:

Type t = sender.GetType();
0
bschave2Author Commented:
I did that and it comes back null. I forgot to mention that I am also saving these objects in the viewstate, so they should not be null when the event is fired.
0
Bill NolanOwner, Lead Technology ProgrammerCommented:
The debugger shows it is valid...?  Check these to see if you can get somewhere::

1. Make sure you are compiling in debug mode
2. Clean & rebuild entire solution
3. Temporarily remove the UpdatePanel
0
bschave2Author Commented:
Attached is what I told you. I can view the id, but I cannot make make it equal to the string. result
0
bschave2Author Commented:
I closed and reopened .net and this works to get the id from the sent object.

string str = ((Button)sender).ID.ToString();

Thanks guys.
0
Bill NolanOwner, Lead Technology ProgrammerCommented:
How did you close and reopen .NET?

Yes, that would get the string.  It appears from all you've stated, and from your screenshot, that you've always had access to the button.
0
bschave2Author Commented:
I closed the program and then re-launched it. I didn't have access to the button. I was on a VM and sometimes get weird lack of functionality in my programs. Don't know if it's the VM connection, my home internet connection or what. The screenshot showed that I was getting the value when hovering over the .ID value on the right side of the variable.

Thanks.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.