0
votes

I have a visual webpart that list the students.

Also have a webpart to add/edit student.

After deploying the application, I created new webpart page and added CreateStudent webpart in a zone and ListStudent webpart in another zone.

When I add a student I need to find that student details in the grid of ListStudent webpart.

I think I need to connect the two webparts making CreateStudent webpart as provider webpart and ListStudent webpart as consumer webpart, but my doubt is, I dont need to pass any particular value to the ListStudent webpart.

I have a funstion call in ListStudent webpart Page_Load which set the datasource of the gridview and binding it. How can this be done?

2

2 Answers

0
votes

Here are simple provider and consumer Web Parts. The provider UI accepts a text field that it passes to the consumer Web Part which simply outputs it. The connection between the Web Parts is the following interface:

namespace ConnectedWebParts
{
    public interface IParcel
    {
        string ID { get; }
    }
}

The Provider Web Part implements this interface and must have a method with the attribute ConnectionProvider that returns itself (since it implements the interface):

namespace ConnectedWebParts
{
    public class ProviderWebPart : WebPart, IParcel
    {
        protected TextBox txtParcelID;
        protected Button btnSubmit;
        private string _parcelID = "";

        protected override void CreateChildControls()
        {
            txtParcelID = new TextBox() { ID = "txtParcelID" };
            btnSubmit = new Button() { ID = "btnSubmit", Text="Submit"};
            btnSubmit.Click += btnSubmit_Click;
            this.Controls.Add(txtParcelID);
            this.Controls.Add(btnSubmit);
        }

        void btnSubmit_Click(object sender, EventArgs e)
        {
            _parcelID = txtParcelID.Text;
        }

        [ConnectionProvider("Parcel ID")]
        public IParcel GetParcelProvider()
        {
             return this;
        }

        string IParcel.ID
        {
             get { this.EnsureChildControls();  return _parcelID; }
        }
     }
 }

The Consumer Web Part must define a method with a ConnectionConsumer attribute that accepts an object that implements the connection interface (the provider Web Part) as a parameter:

namespace ConnectedWebParts
{
    public class ConsumerWebPart : WebPart
    {
        protected IParcel _provider;
        protected Label lblParcelID;
        protected override void CreateChildControls()
        {
            lblParcelID = new Label();
            if (_provider != null && !String.IsNullOrEmpty(_provider.ID))
                lblParcelID.Text = _provider.ID;    

            this.Controls.Add(lblParcelID);
        }

        [ConnectionConsumer("Parcel ID")]
        public void RegisterParcelProvider(IParcel provider)
        {
            _provider = provider;
        }
    }
}