Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 209
  • Last Modified:

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?
0
bschave2
Asked:
bschave2
  • 9
  • 6
3 Solutions
 
Bill NolanOwner, Lead Technology ProgrammerCommented:
Where does 'sender' come from?
0
 
bschave2Author Commented:
the object that sends it, but am getting nothing back.
0
 
bschave2Author Commented:
also tried  Button btn = ((Button)sender);
0
Industry Leaders: 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!

 
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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 9
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now