DooDoo
DooDoo

Reputation: 13467

Update Main page from user control

I have a grid view in my main page and I display some data for user(using BindGrid method).this grid view has some command buttons for each row that perform some operation like Update. when user clicks on update button I show to him/her a user control to update values.and when user clicks on update I want grid bind to new data(I want call BindGrid for new data). How I can do this and call a method in main page from user control?

Edit 1)

I wrote this code for user control:

public partial class SomeUserControl : System.Web.UI.UserControl
{
public event EventHandler StatusUpdated;

private void FunctionThatRaisesEvent()
{
    if (this.StatusUpdated != null)
        this.StatusUpdated(new object(), new EventArgs());
} 


public void Page_Load(object sender, EventArgs e)
{
    //.... 
}

protected void Button1_Click(object sender, EventArgs e)
{
    FunctionThatRaisesEvent();
}
}

and the designer for user control :

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="SomeUserControl.ascx.cs" Inherits="SomeUserControl" %>
<asp:Button ID="Button1" runat="server" Text="Update" Height="70px" 
onclick="Button1_Click" Width="183px" />

and add this code for main page:

protected void Page_Load(object sender, EventArgs e)
{

}
protected void Unnamed1_Click(object sender, EventArgs e)
{
    SomeUserControl userControl = (SomeUserControl)LoadControl("SomeUserControl.ascx");
    userControl.StatusUpdated += new EventHandler(userControl_StatusUpdated);
    Panel1.Controls.Add(userControl);
}

void userControl_StatusUpdated(object sender, EventArgs e)
{
    GetDate();
}

private void GetDate()
{
    TextBox1.Text = DateTime.Today.ToString();
}

and designer for main page:

<%@ Register src="SomeUserControl.ascx" tagname="SomeUserControl" tagprefix="uc1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
    <div>
        <asp:UpdatePanel ID="upd1" runat="server" UpdateMode="Conditional">
            <ContentTemplate>
                <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                <asp:Button runat="server" Text="Add User Control" Height="44px"  ID="Nims"
                onclick="Unnamed1_Click" Width="133px" />
                <asp:Panel ID="Panel1" runat="server" BackColor="#FFFFCC"></asp:Panel>
            </ContentTemplate>
        </asp:UpdatePanel>
    </div>

    </form>
</body>
</html>

but it does not work and nothing happend. even I add break point for click user control button code but it seems that event not raise.

Upvotes: 0

Views: 8717

Answers (3)

Aarif Qureshi
Aarif Qureshi

Reputation: 474

you need to use event handler for that

define event handler in your ascx page

public event EventHandler ButtonClickDemo;

when you are performing update or delete event use following code there for event handler.

   ButtonClickDemo(sender, e);

in parent page use following

  protected void Page_Load(object sender, EventArgs e)
  {
         btnDemno.ButtonClickDemo += new EventHandler(btn_Click);
  }

the function to bind grid of parent page

  protected void btn_Click(object sender, EventArgs e)
  {
            BindGrid();
  }

above code will call BindGrid function of parent page.

Upvotes: 0

Goose
Goose

Reputation: 3279

Raise the event from the user control, and handle the event from the main page. Check out this question.

//** EDIT **//

You are adding the user control dynamically on button click. When you click the button on your user control it first will initiate postback on the main page - now your user control no longer exists (which is why the event is not raised). If you change your main page designer to look like this:

<%@ Register Src="SomeUserControl.ascx" tagname="SomeUserControl" tagprefix="uc1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<div>
    <asp:UpdatePanel ID="upd1" runat="server" UpdateMode="Conditional">
        <ContentTemplate>
            <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
            <asp:Button runat="server" Text="Add User Control" Height="44px"  ID="Nims"
            onclick="Unnamed1_Click" Width="133px" />
            <asp:Panel ID="Panel1" runat="server" BackColor="#FFFFCC">
            <uc1:SomeUserControl ID="userControl" runat="server" />
            </asp:Panel>
        </ContentTemplate>
    </asp:UpdatePanel>
</div>

</form>

and your code-behind to look like this

protected void Page_Load(object sender, EventArgs e)
{
    userControl.StatusUpdated += new EventHandler(userControl_StatusUpdated);
}

void userControl_StatusUpdated(object sender, EventArgs e)
{
    GetDate();
}

private void GetDate()
{
    TextBox1.Text = DateTime.Today.ToString();
}

you will see what I mean. Try setting breakpoints on the page load events of your main page and your user control to see exactly the order in which things happen.

Upvotes: 1

Firoz Ansari
Firoz Ansari

Reputation: 2515

You can register an event in your user control, raise the event when user clicks on update button and capture that event on the page.

http://codebetter.com/brendantompkins/2004/10/06/easily-raise-events-from-asp-net-ascx-user-controls/

Upvotes: 0

Related Questions