本文介绍了在asp.net中调用C#函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 限时删除!! 我有一个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; 使用System.Configuration; 使用System.Data; 使用System.Linq; 使用System.Web; 使用System.Web.Security; 使用System.Web.UI; 使用System.Web.UI.HtmlControls; 使用System.Web.UI.WebControls; 使用System.Web.UI.WebControls.WebParts; 使用System.Xml.Linq; 使用System.Data.Odbc; 使用System.Data.SqlClient; 使用System.Collections; 使用System.IO; 使用iTextSharp.text.html.simpleparser; 使用iTextSharp.text; 使用iTextSharp.text.pdf; 使用iTextSharp.text.html; ///< summary> /// 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(); //设置数据绑定。 DropDownList3.DataSource = myReader; DropDownList3.DataTextField =skey; DropDownList3.DataValueField =casecode; DropDownList3.DataBind(); DropDownList3.Items.Insert(0,< - Select - >); catch(Exception ex) { Response.Write(ex.StackTrace); (DropDownList2.SelectedItem.Text ==Register) { DropDownList3.Items.Remove(DropDownList3.Items.FindByText(STA) $ b if )); 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( ); $ b 无论我到现在为止做了什么,这是正确的吗?我现在得到的错误是: 名称'DropDownList2'在当前上下文中不存在名称'DropDownList3'在当前上下文中不存在名称'Response'在当前上下文中不存在 我想创建这个函数,因为我将创建许多这样的WebForms,并且我想调用上的相同函数 解决方案 Try this way 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 }}Use HttpContext.Current.Response.WriteUse this class as DropDown abs = new DropDown();abs.Register(dropdown1, dropdown2);Try not to write code in constructor which might throw exception 这篇关于在asp.net中调用C#函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 1403页,肝出来的..
09-07 20:02