簡體   English   中英

在asp.net中調用C#函數

[英]Calling a C# function in asp.net

我有一個WebForm before_adm.aspx.cs,其代碼如下:

.
.
.
public partial class _Default : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
    {
          DropDown abs =new DropDown();
          abs.DropDown();
    }
.....

我想調用一個函數DropDown(),該函數位於文件DropDown.cs中。DropDown.cs中的代碼如下:

using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.Odbc;
using System.Data.SqlClient;
using System.Collections;
using System.IO;
using iTextSharp.text.html.simpleparser;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html;

/// <summary>
/// Summary description for DropDown
/// </summary>
public class DropDown
{
    public DropDown()
    {
        try
        {

            OdbcConnection myConn = new OdbcConnection("Driver={MySQL ODBC 3.51 Driver};Server=10.155.160.130;Database=testcase;User=root;Password=;Option=3;");
            OdbcCommand myCmd = new OdbcCommand("select skey,casecode from casetype", myConn);
            myConn.Open();
            OdbcDataReader myReader = myCmd.ExecuteReader();

            //Set up the data binding.
            DropDownList3.DataSource = myReader;
            DropDownList3.DataTextField = "skey";
            DropDownList3.DataValueField = "casecode";
            DropDownList3.DataBind();
            DropDownList3.Items.Insert(0, "<-- Select -->");

        }
        catch (Exception ex)
        {
            Response.Write(ex.StackTrace);
        }

        if (DropDownList2.SelectedItem.Text == "Register")
        {
            DropDownList3.Items.Remove(DropDownList3.Items.FindByText("STA"));
            DropDownList3.Items.Remove(DropDownList3.Items.FindByText("STM"));
        }
        else if (DropDownList2.SelectedItem.Text == "Stamp")
        {
            DropDownList3.Items.Remove(DropDownList3.Items.FindByText("WP"));
            DropDownList3.Items.Remove(DropDownList3.Items.FindByText("FA"));
            DropDownList3.Items.Remove(DropDownList3.Items.FindByText("LPA"));
            DropDownList3.Items.Remove(DropDownList3.Items.FindByText("SA"));
            DropDownList3.Items.Remove(DropDownList3.Items.FindByText("CP"));
            DropDownList3.Items.Remove(DropDownList3.Items.FindByText("CA"));
            DropDownList3.Items.Remove(DropDownList3.Items.FindByText("CAPL"));
            DropDownList3.Items.Remove(DropDownList3.Items.FindByText("MCA"));
            DropDownList3.Items.Remove(DropDownList3.Items.FindByText("CRA"));
            DropDownList3.Items.Remove(DropDownList3.Items.FindByText("XFER"));
            DropDownList3.Items.Remove(DropDownList3.Items.FindByText("ARP"));
            DropDownList3.Items.Remove(DropDownList3.Items.FindByText("TXA"));
            DropDownList3.Items.Remove(DropDownList3.Items.FindByText("CRIR"));
            DropDownList3.Items.Remove(DropDownList3.Items.FindByText("WPCR"));
            DropDownList3.Items.Remove(DropDownList3.Items.FindByText("AO"));
            DropDownList3.Items.Remove(DropDownList3.Items.FindByText("AUA"));
            DropDownList3.Items.Remove(DropDownList3.Items.FindByText("CONP"));
            DropDownList3.Items.Remove(DropDownList3.Items.FindByText("ELEP"));
            DropDownList3.Items.Remove(DropDownList3.Items.FindByText("COAP"));
            DropDownList3.Items.Remove(DropDownList3.Items.FindByText("COA"));
            DropDownList3.Items.Remove(DropDownList3.Items.FindByText("ARA"));
            DropDownList3.Items.Remove(DropDownList3.Items.FindByText("CREF"));
            DropDownList3.Items.Remove(DropDownList3.Items.FindByText("ITA"));
            DropDownList3.Items.Remove(DropDownList3.Items.FindByText("AAR"));
            DropDownList3.Items.Remove(DropDownList3.Items.FindByText("XFA"));
            DropDownList3.Items.Remove(DropDownList3.Items.FindByText("TACR"));
            DropDownList3.Items.Remove(DropDownList3.Items.FindByText("EXAP"));
            DropDownList3.Items.Remove(DropDownList3.Items.FindByText("COP"));
            DropDownList3.Items.Remove(DropDownList3.Items.FindByText("AUE"));
            DropDownList3.Items.Remove(DropDownList3.Items.FindByText("AUW"));
            DropDownList3.Items.Remove(DropDownList3.Items.FindByText("CRMA"));
            DropDownList3.Items.Remove(DropDownList3.Items.FindByText("CAREV"));
            DropDownList3.Items.Remove(DropDownList3.Items.FindByText("CONF"));
            DropDownList3.Items.Remove(DropDownList3.Items.FindByText("PILWP"));
            DropDownList3.Items.Remove(DropDownList3.Items.FindByText("SMWP"));
            DropDownList3.Items.Remove(DropDownList3.Items.FindByText("XOB"));
            DropDownList3.Items.Remove(DropDownList3.Items.FindByText("SMCP"));
            DropDownList3.Items.Remove(DropDownList3.Items.FindByText("AELEP"));
            DropDownList3.Items.Remove(DropDownList3.Items.FindByText("RUIT"));
            DropDownList3.Items.Remove(DropDownList3.Items.FindByText("CS"));
            DropDownList3.Items.Remove(DropDownList3.Items.FindByText("RUCA"));
            DropDownList3.Items.Remove(DropDownList3.Items.FindByText("CRIA"));
            DropDownList3.Items.Remove(DropDownList3.Items.FindByText("CUSA"));
            DropDownList3.Items.Remove(DropDownList3.Items.FindByText("EXA"));
            DropDownList3.Items.Remove(DropDownList3.Items.FindByText("SMCRC"));
            DropDownList3.Items.Remove(DropDownList3.Items.FindByText("EA"));
            DropDownList3.Items.Remove(DropDownList3.Items.FindByText("CRMAB"));
            DropDownList3.Items.Remove(DropDownList3.Items.FindByText("CRMAM"));
            DropDownList3.Items.Remove(DropDownList3.Items.FindByText("COAPM"));
            DropDownList3.Items.Remove(DropDownList3.Items.FindByText("CPM"));
            DropDownList3.Items.Remove(DropDownList3.Items.FindByText("MCAM"));
            DropDownList3.Items.Remove(DropDownList3.Items.FindByText("AS"));
            DropDownList3.Items.Remove(DropDownList3.Items.FindByText("PILSM"));
            DropDownList3.Items.Remove(DropDownList3.Items.FindByText("CRREF"));
            DropDownList3.Items.Remove(DropDownList3.Items.FindByText("SMCR"));
            DropDownList3.Items.Remove(DropDownList3.Items.FindByText("AUSFC"));

        }
        else if (DropDownList2.SelectedItem.Text == "<-- Select -->")
        {
            DropDownList3.Items.Clear();
        }
    }

}

到現在為止我做了什么,對嗎? 我現在得到的錯誤為:

The name 'DropDownList2' does not exist in the current context
The name 'DropDownList3' does not exist in the current context  
The name 'Response' does not exist in the current context

我要創建此函數,因為我將創建許多此類WebForm,並且我想在上調用相同的函數

DropDownList2_SelectedIndexChanged我需要傳遞什么參數。 我想知道傳遞參數的方法。 請指導我。 我想在.cs文件中創建許多這樣的功能,它將具有許多其他用戶控件。

問題在於DropDown類沒有對DropDownList2的引用,也沒有對'Response'的引用,因為那是Page對象的屬性。 我們可以通過傳遞對DropDownList和Page的引用並稍作調整以使用這些變量來解決此問題。

  1. 更改DropDown abs =new DropDown(); DropDown abs =new DropDown(this.DropDownList2, Page page);
  2. public DropDown()更改為public DropDown(DropDownList DropDownList2, Page page)
  3. 更改Response.Write(ex.StackTrace); page.Response.Write(ex.StackTrace); `

如果您仍然感到困惑或陷入困境,那么編寫ASP.NET程序可能為時過早-我將通過閱讀一些教程來學習C#和OO的基礎知識。

您應該將dropDown作為參數傳遞到您的DropDown方法中:

DropDown abs =new DropDown((DropDownList)sender);
....
public DropDown(DropDownList DropDownList2)..

而不是:

Response.Write

采用:

HttpContext.Current.Response.Write

試試這個

public class DropDown
{
    DropDownList dropDownList3 = null;
    DropDownList dropDownList2 = null;

    public DropDown()
    {
    }

    public void Register(DropDownList dropDown2, DropDownList dropDown3)
    {
        this.dropDownList2 = dropDown2;
        this.dropDownList3 = dropDown3;
        // your code goes here
     }
}

使用HttpContext.Current.Response.Write

將此課程用作

DropDown abs = new DropDown();
abs.Register(dropdown1, dropdown2);

盡量不要在構造函數中編寫可能會引發異常的代碼

如果要手動調用選定的索引更改事件,則可以放置以下代碼:

DropDownList2_SelectedIndexChanged(this,null);

我認為您正在嘗試實現級聯下拉菜單。 您具有實用程序類DropDown.CS。 Thementhod“ Dropdown”是一個構造函數,而不是一個方法,因此每當您初始化Dropdown類時,都會調用它。 當您選擇第一個下拉列表時,將調用選定的已更改索引。

這有幫助嗎?

不知道是否有幫助,但是您也可以使用擴展方法來擴展股票類的功能。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM