[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

User controls' events

Posted on 2009-06-29
10
Medium Priority
?
524 Views
Last Modified: 2012-06-21
Hello,

I have written a user control containing a dropdown. On the user control I declare a delegate and an event 'OnSelectedIndexChangedDelegate' when changing the selected index.

I am using the user control in 1 aspx page successfully:

ctlLevel1.OnSelectedIndexChanged += new ddlToText.OnSelectedIndexChangedDelegate(ctlLevel1_OnSelectedIndexChanged);

But, when putting the user control within a griView TemplateField, I can't use it the way I did in the previous page because 'ctLevel1' is not recognized (it is contained within the gridView).
How can I use the user control events within the gridView?

Thanks
0
Comment
Question by:Relegence
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
10 Comments
 
LVL 39

Expert Comment

by:abel
ID: 24740351
Normally, you can do that declaratively using ASP.NET code:

<custom:MyControl OnSelectedIndexChanged="FunctionInScopeToHandleThis" runat="server" />

and if I'm not mistaken that should work for you to.

If for some reason you need to do this programmatically, your best bet is the RowDataBound event which fires after the Row has been databound. From there you can use FindControl, cast the result to your control, then bind the event. This, however, has a tricky bit to it which appears when the page is reloaded. So, if you can do it declaratively like explained above, it would be the easiest method by far.

-- Abel --
0
 

Author Comment

by:Relegence
ID: 24742278
I cannot add it to the declaration of the control, as proposed, it does not appear on the list of available events (it is an event I added, not 1 of the default events).

Since I am using the control when editing a row ('EditItemTemplate'), I have added it to 'grid_RowEditing' event:

            ((ddlToText)gvRTNIDs.Rows[e.NewEditIndex].FindControl("ctlLevel1")).OnSelectedIndexChanged += new ddlToText.OnSelectedIndexChangedDelegate(ctlLevel1_OnSelectedIndexChanged);

Unfortunately, that doesn't work.

Any ideas?
0
 
LVL 39

Expert Comment

by:abel
ID: 24742950
> it does not appear on the list of available events
It should, actually. You may consider adding it even if the intellisense doesn't help you. Use the Code View, not the Design View of the ASPX.

Alternatively, if you don't find the place to add the event (it must be there, really!), you can use a workaround. Take the default OnLoad or OnInit event. The sender is your control, cast it, add the event, and you should be done.

If those approaches don't suit you, can you elaborate on what doesn't work of the code above? Error? What error? etc.

-- Abel --
0
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.

 

Author Comment

by:Relegence
ID: 24743243
Thank you very much for trying to help.

I tried to address the 'SelectedIndexChanged' event of my control's dropDown using:

protected void gvRTNIDs_RowEditing(object sender, GridViewEditEventArgs e)
{
  gvRTNIDs.EditIndex = e.NewEditIndex;
  FillGridViewData(entity.ListRTNIDs);

  ((ddlToText)gvRTNIDs.Rows[e.NewEditIndex].FindControl("ctlLevel1")).DDL.SelectedIndexChanged   +=new EventHandler(DDL_SelectedIndexChanged);
}

When debugging, the debugger never enters the appropriate function (DDL_SelectedIndexChanged)

When trying to register to the event in the 'RowDataBound ' event:

if (e.Row.RowType == DataControlRowType.DataRow)
{
  ((ddlToText)gv.Rows[e.Row.RowIndex].FindControl("ctlLevel1")).OnSelectedIndexChanged += new ddlToText.OnSelectedIndexChangedDelegate(ctlLevel1_OnSelectedIndexChanged);
}

I keep getting the exception 'Index was out of range. Must be non-negative and less than the size of the collection'. I don't understand why.

Thanks...
0
 
LVL 39

Expert Comment

by:abel
ID: 24743286
Can you show me how the control has been declared in the ASPX file please?

Your methods above are different. How can one code do DDL.SelectedIndexChanged and the other do OnSelectedIndexChanged? Perhaps you use different methods?

But let's first try to do it the preferred way: through the declarative syntax. If that doesn't work, it will probably not work the other way either.
0
 

Author Comment

by:Relegence
ID: 24743386
This is how the control was declared in the aspx code:

<EditItemTemplate>
  <controls:ddlTOText ID="ctlLevel1" Name="ctlLevel1" runat="server"/>
</EditItemTemplate>

Indeed, I tried both ways - once to address my controls' selectedIndexChanged event I added, and also to address the control dropdown's selectedIndexChanged event.

Both didnt work.

I'll add the code of my ASCX control.
ASCX code
----------
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="ddlToText.ascx.cs" Inherits="TickerTool.ddlToText" %>
 
<table style="position:relative;top:8px;">
    <tr>
        <td colspan="2">
            <asp:DropDownList ID="ddl" runat="server" Width="100px" AutoPostBack="true" OnSelectedIndexChanged="ddl_SelectedIndexChanged"></asp:DropDownList>   
            <asp:TextBox ID="txt" runat="server" style="display:none" Width="92px"></asp:TextBox>     
        </td>
    </tr>
    <tr>
        <td>
            <a href="#" id="lnk" runat="server" class="smallLink" onserverclick="toggle">Add</a>
        </td>
        <td align="right">
            <a href="#" id="lnkCancel" runat="server" class="smallLink" onserverclick="cancel" visible="false">Cancel</a>
        </td>
    </tr>
</table>
 
ASCX CS code
-------------
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
 
namespace TickerTool
{
    public partial class ddlToText : System.Web.UI.UserControl
    {
        public delegate void OnSelectedIndexChangedDelegate(object    sender, EventArgs e);
        public event OnSelectedIndexChangedDelegate OnSelectedIndexChanged;
 
        public delegate void OnLinkClickedDelegate(object sender, EventArgs e);
        public event OnLinkClickedDelegate OnLinkClicked;
 
        protected void Page_Load(object sender, EventArgs e)
        {
            
        }
 
        protected void toggle(object sender, EventArgs e)
        {
            if (lnk.InnerText == "Add")
            {
                ddl.Attributes.Add("style", "display:none");
                txt.Attributes.Add("style", "display:inline");
                lnk.InnerText = "New";
                lnkCancel.Visible = true;
            }
            else
            {
                ddl.Attributes.Add("style", "display:inline");
                txt.Attributes.Add("style", "display:none");
                lnk.InnerText = "Add";
                lnkCancel.Visible = false;
            }
 
            if (OnLinkClicked != null)
            {
                OnLinkClicked(sender, e);
            }
        }
 
        protected void cancel(object sender, EventArgs e)
        {
            ddl.Attributes.Add("style", "display:inline");
            txt.Attributes.Add("style", "display:none");
            lnk.InnerText = "Add";
            lnkCancel.Visible = false;
        }
        
 
        public DropDownList DDL
        {
            get
            {
                return this.ddl;
            }
        }
 
       
 
        protected void ddl_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (OnSelectedIndexChanged != null)
            {
                OnSelectedIndexChanged(sender, e);
            }
        }
 
        
       
    }
}

Open in new window

0
 
LVL 39

Accepted Solution

by:
abel earned 2000 total points
ID: 24743430
If I read your code correctly, it should be possible to do it as follows (if your IntelliSense doesn't work, please try to Clean Solution / Rebuild and ignore IntelliSense):

<EditItemTemplate>
  <controls:ddlTOText ID="ctlLevel1" Name="ctlLevel1" runat="server" OnSelectedIndexChanged="DDL_SelectedIndexChanged"/>
</EditItemTemplate>

If this throws an error, can you show the error?
0
 

Author Comment

by:Relegence
ID: 24743462
It is finally working. Thank you so much.

I don't understand why it wasn't working previously...

Thanks!!!
0
 
LVL 39

Expert Comment

by:abel
ID: 24743561
What did the trick? The declarative syntax?

I've had problems with this before as well and usually cleaning the project helped most of the times, sometimes reopening VS, or simply ignoring IntelliSense (it builds, but IntelliSense just doesn't get it). It also helps placing the usercontrols outside of the project in their own control lib.

Glad it works now.
0
 

Author Comment

by:Relegence
ID: 24743626
Yes, the declarative syntax was the solution here :-)
0

Featured Post

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!

Question has a verified solution.

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

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

650 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