Home > Enterprise >  Can I use the same function in different ASP.NET pages?
Can I use the same function in different ASP.NET pages?

Time:11-19

protected void DropDownMainProduct_SelectedIndexChanged(object sender, EventArgs e)
{
    string mainProductCode = DropDownMainProduct.SelectedValue;

    if (mainProductCode == "0")
    {
        DropDownSubProduct.SelectedValue = "0";
        DropDownSubProduct.Attributes.Add("disabled", "true");
    }
    else
    {
        DropDownSubProduct.Attributes.Remove("disabled");
    }
}

Can I use the DropDownMainProduct_SelectedIndexChanged function in different ASP.NET pages without re-writing the dropdown's IndexChanged method?

CodePudding user response:

Well, you can have each page call your "general" routines page, and thus you only have one copy of that code.

   protected void DropDownMainProduct_SelectedIndexChanged(object sender, EventArgs e)
    {
         MyCode.MyDropDown(sender);
    }

And in your project wide project, then this:

public static class MyCode
{

    static void MyDropDownRoutine(Object ctl)
    {
        DropDownList dl = (DropDownList)ctl;

        string mainProductCode = dl.SelectedValue;

        if (mainProductCode == "0")
        {
            dl.SelectedValue = "0";
            dl.Attributes.Add("disabled", "true");
        }
        else
        {
            dl.Attributes.Remove("disabled");
        }
    }
}

So, yes, the code routine can be called and used by all pages.

But, you STILL have to place that dd in each page, you STILL have to set the datatext/datavalue fields, and you STILL need that code routine.

Why not build a user control, and then you can drag/drop that same control into each page, and thus you ONLY EVER have one copy of the markup, the code, and the one event?

CodePudding user response:

There are different ways to accomplish this. This might help.

You want to re-use DropDownMainProduct_SelectedIndexChanged on other pages. That method interacts with another drop-down, DropDownSubProduct. This implies that you're going to have both drop-downs on both pages.

A user control allows you to define your own control which contains multiple controls. The user control has its own code-behind with event handlers for the controls it contains.

So instead of trying to figure out how to re-use the event handlers, you can just put both drop-downs and their event handlers in a user control and re-use that on multiple pages.

  • Right-click on your project, "Add new item," and add a Web Forms User Control. You can call it "MyDropDowns.ascx".
  • Add both of your drop-downs to that user control.
  • Add the event handlers for the drop-downs to the user control just like you would with a web form. It's like a WebForm within a W

Now you've got a new file - MyDropDowns.ascx (or whatever you called it) in your project. The next step is to add it to your web form.

Beneath the page directive, add a registration for your user control.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" 
    Inherits="WebApplication1.WebForm1" %>
<%@ Register TagPrefix="uc" TagName="MyDropDowns" 
    Src="~/MyDropDowns.ascx" %>

"uc" is just a common convention - it indicates that the tag represents a user control.

Now you can add the control to your WebForm like any other control. You can even add more than one to the same page.

<form id="form1" runat="server">
    <div>
        <uc:MyDropDowns ID="MyDropDowns1" runat="server" />
        <uc:MyDropDowns ID="MyDropDowns2" runat="server" />
    </div>
</form>

If your WebForm needs to get values from the user control, you can add properties to the user control's code behind, like

public string MainProductSelectedValue()
{
    return DropDownMainProduct.SelectedValue;
}

...and then the WebForm can read those properties just like it reads the properties of other server controls:

var productSelectedValue = MyDropDowns1.MainProductSelectedValue()

You can also add public methods and events to the user control so that the WebForm can interact with the user control or respond to its events.

Here is some more documentation.

  • Related