【问题标题】:Calling a C# function in asp.net在 asp.net 中调用 C# 函数
【发布时间】:2011-01-06 16:53:17
【问题描述】:

我有一个 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.cs文件中的函数DropDown() 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

我想创建这个函数,因为我将创建许多这样的 WebForms 并且我想调用相同的函数

DropDownList2_SelectedIndexChanged 我需要传递什么参数。我想知道传递参数的方式。请指导我。我想在 .cs 文件中创建许多这样的函数,这些函数将具有许多其他用户控件。

【问题讨论】:

    标签: c# asp.net function parameters


    【解决方案1】:

    问题在于 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 的基础知识。

    【讨论】:

    • ") & ;"预计在您的第一点的代码行上。还有另一个错误 Invalid expression term ')'
    • 错误 1 ​​'DropDown' 不包含 'DropDown' 的定义,并且找不到接受类型为 'DropDown' 的第一个参数的扩展方法 'DropDown'(您是否缺少 using 指令或程序集参考?)
    • 您还必须复制换行的文本,而不仅仅是第一行 (DropDown abs =new DropDown(this.DropDownList2, )。想清楚。
    【解决方案2】:

    试试这个方法

    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);
    

    尽量不要在构造函数中编写可能引发异常的代码

    【讨论】:

    • 谢谢。你能告诉我如何将“Editor1”作为参数传递,它是一个 ajax 控件。我收到错误消息:当前上下文中不存在名称“Editor1”
    • 发布代码。无法理解什么是编辑器,哪个对象包含它以及您从哪里调用下拉菜单
    【解决方案3】:

    您应该将 dropDown 作为参数传递给您的 DropDown 方法:

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

    而不是:

    Response.Write
    

    使用:

    HttpContext.Current.Response.Write
    

    【讨论】:

    • Error1 当前上下文中不存在名称“DropDownList3”。我复制并粘贴了您回答的任何内容。那力道工作。再次出现同样的错误!
    • 你的 DropDownList3 是什么?如果您需要,您也可以将 DropDownList3 作为参数传递给 DropDown 函数...
    【解决方案4】:

    如果您想手动调用选定的索引更改事件,您可以放置​​以下代码:

    DropDownList2_SelectedIndexChanged(this,null);
    

    【讨论】:

      【解决方案5】:

      我认为您正在尝试实现级联下拉菜单。您有一个实用程序类 DropDown.CS。 Thementhod "Dropdown" 是一个构造函数,而不是一个方法,因此每当您初始化类 Dropdown 时都会调用它。当您更改第一个下拉菜单时,将调用已更改的选定索引。

      这有用吗?

      【讨论】:

      • 那我需要做什么?你能帮帮我吗?我是初学者。
      【解决方案6】:

      不知道是否有帮助,但您也可以使用扩展方法来扩展股票类的功能。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-03-23
        • 2011-11-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-08-14
        • 1970-01-01
        • 2011-05-31
        相关资源
        最近更新 更多