Link to home
Start Free TrialLog in
Avatar of bschave2
bschave2

asked on

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?
ASKER CERTIFIED SOLUTION
Avatar of Bill Nolan
Bill Nolan
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of bschave2
bschave2

ASKER

the object that sends it, but am getting nothing back.
also tried  Button btn = ((Button)sender);
SOLUTION
Avatar of Daniel Van Der Werken
Daniel Van Der Werken
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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

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?
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.
If 'sender' is null, how can you read its properties in the debugger?
don't know!
Check the type via:

Type t = sender.GetType();
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.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Attached is what I told you. I can view the id, but I cannot make make it equal to the string. User generated image
I closed and reopened .net and this works to get the id from the sent object.

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

Thanks guys.
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.
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.