Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Calling a method on a webform in an ascx file. 1

Status
Not open for further replies.

LauraCairns

Programmer
Jul 26, 2004
173
0
0
GB
Hey everyone. I was wondering if someone could help me with a small problem. I have designed a user control and I would have inserted it on a aspz page (WebForm1). The User control is being used to help with the navigation through a datagrid. The control handles all the paging issues and on the webform I do all the DataBinding.

My problem is that at the end of code of an on-click event for a link button on my user control (DataGridNavigation.ascx) I need to call the DataBind Method. The problem is that the DataBind method is sitting on the WebForm1 code behind page.

Here is a part of my code on the Code behind in the user control (ascx.cs file) and commented is here I need to call the DataBind method.

private void No1_Click(object sender, System.EventArgs e)
{
_currentPageNumber = int.Parse(No1.Text);
this.CurrentPage.Text = _currentPageNumber.ToString();
ShowHideNavigation();
/*Need to call the DataBind method in here somehow
This DataBind Method is sitting on the WebForm1 code behind page*/
}

I would be very grateful if someone could help me please.
 
in the constructor of the control you could specify a reference to the page containing the control. or you could create a delegate and an event in your custom control, raise this event inside the click procedure you shown in your post, and subscribe to this event with your function that does the databinding. this way, when the click procedure runs, it will raise the event defined by you and call the method of the WebForm1 that registered to it.

--------------------------
"two wrongs don't make a right, but three lefts do" - the unknown sage
 
sort of see what you mean but wouldn't have a clue how to write the code for that. It seems fairly complicated.

Basically to highlight my problem again.
on my webform1 I have a method
public void BindData()
{

}

And on my DataGridNavigation.ascx.cs file I need to call this method: -

private void No1_Click(object sender, System.EventArgs e)
{
_currentPageNumber = int.Parse(No1.Text);
this.CurrentPage.Text = _currentPageNumber.ToString();
ShowHideNavigation();
//Call BindData Method here
}

Is this the only way for me to achieve this and if so could you please please help me a little more with the code for this.
Thanks ever so much for helping so far
 
add this outside of the class for the custom control
Code:
public delegate void dBindData();

inside the component add:
Code:
public event dBindData OnBindData();

public void RaiseOnBindDataEvent()
{
    foreach (dBindData bd in this.OnBindData.GetInvocationList())
    {
        bd();
    }
}

modify the click function to
Code:
private void No1_Click(object sender, System.EventArgs e)
{
    _currentPageNumber = int.Parse(No1.Text);
    this.CurrentPage.Text = _currentPageNumber.ToString();
    ShowHideNavigation();
    raiseOnBindDataEvent();
}

now inside the page, where the custom control gets created add
Code:
customControl.OnBindData += new dBindData(BindData);

--------------------------
"two wrongs don't make a right, but three lefts do" - the unknown sage
 
add this outside of the class for the custom control
Code:
public delegate void dBindData();

inside the component add:
Code:
public event dBindData OnBindData;

public void RaiseOnBindDataEvent()
{
    foreach (dBindData bd in this.OnBindData.GetInvocationList())
    {
        bd();
    }
}

modify the click function to
Code:
private void No1_Click(object sender, System.EventArgs e)
{
    _currentPageNumber = int.Parse(No1.Text);
    this.CurrentPage.Text = _currentPageNumber.ToString();
    ShowHideNavigation();
    raiseOnBindDataEvent();
}

now inside the page, where the custom control gets created add
Code:
customControl.OnBindData += new dBindData(BindData);

--------------------------
"two wrongs don't make a right, but three lefts do" - the unknown sage
 
Cheers this is a great a way of doing things. My boss was impressed with this. thanks
 
I'll add a star for your boss then. :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top