开发者

how to make a Image-button in the user control execute code in the defaultpage.aspx?

1-I have three user controls. 2-I added them to AJAX TabContainer on my default.aspx page

<asp:TabContainer ID="TabContainer1" runat="server">
            <asp:TabPanel runat="server" ID="GroupOne">
                <HeaderTemplate>
                    1
                </HeaderTemplate>
                <ContentTemplate>
                    <SUR:GroupOne ID="group1" runat="server" />                  
                </ContentTemplate>                     
            </asp:TabPanel>
            <asp:TabPanel ID="GroupTwo" runat="server">
                <HeaderTemplate>
                    2
                </HeaderTemplate>
                <ContentTemplate>
                    <SUR:GroupTwo is="group2" runat="server" />
                </ContentTemplate&开发者_如何学Gogt;
            </asp:TabPanel>
           <asp:TabPanel ID="GroupThree" runat="server">
                <HeaderTemplate>
                    3
                </HeaderTemplate>
                <ContentTemplate>
                    <SUR:GroupThree ID="grup3" runat="server" />
                </ContentTemplate>
            </asp:TabPanel>
        </asp:TabContainer>

3- in the first user control i have image-button

<asp:ImageButton ID="ImageButton1" runat="server"  />

4- I have this code in my default.vb

Public Sub movit()
    GroupThree.Enabled = True
    TabContainer1.ActiveTab = GroupThree
End Sub

so how can i execute that sub when i click on the image button in the user-control??


I believe you need to use a delegate to achive this. You can try followings

In your ascx.cs or vb file Add a delegate inside the namespace but outside the UserControl class.

   public delegate void ImageButtonClickEventHandler(Object sender, EventArgs args);

Inside UserControl class add an event using delegate.After that call the delegate inside ImageButtonClick event.

  public event ImageButtonClickEventHandler ImageButtonClickEvent;

private void imageButton_Click(object sender, System.EventArgs e)
    {
        if(ImageButtonClickEvent!= null)
        {
            ImageButtonClickEvent(sender,e);
        }
    }

In your aspx page add this inside page load event.

UserControl1.ImageButtonClickEvent+=new ImageButtonClickEventHandler(UserControl1_ImageButtonClickEvent);

Finally declare UserControl1_ImageButtonClickEvent function

private void UserControl1_ImageButtonClickEvent(Object sender, EventArgs args)
    {       
        //Call your methods
    }


From your designer, double-click the button, this will wire up an event-handler (ImageButton1_Click) and set the onclick property of your imagebutton to ImageButton1_Click.

In the code behind of your page, just call movit() from the generated eventhandler.

[Edit]

Didn't see that the image button was in a user control.

The way to go would be to add an Event to your user control. Raise it when the user clicks the imagebutton. In the page, handle the new event of the user control and call the movit() function.


What aboiut..

1) add an event handler to the user control

public event EventHandler Click
{
    add
    {
        ImageButton1.Click += value;
    }
    remove
    {
        ImageButton1.Click -= value;
    }
}

2) subscribe to this event from the page you are using the control - default.aspx

<user:control runat="server" ID="uc" OnClick="uc_OnClick" />

and

protected void uc_OnClick(object sender, EventArgs e)
{
    movit();
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜