创建方法/类以填充下拉菜单 [英] Creating a Method/Class to Populate Dropdown

查看:80
本文介绍了创建方法/类以填充下拉菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我已经在网上寻找了一段时间,并且我肯定对此会有一个非常简单的答案.但是我有一些代码在ASP.Net应用程序的很多页面中使用,我想将其放入方法/类中,以便在整个过程中使用它.

这是原始代码,有人可以帮助我将其放入类/方法中吗?

Hi,

I''ve been looking online for a while now and im sure there is going to be a very simple answer to this one. But I have a bit of code which is used in quite a few pages on our ASP.Net Application, and I would like to place it into a method/class so that I can use it throughout.

Here is the original code, would someone be able to help me put it into a class/method.

Private Sub PopulateResults()
        'Populate the results dropdowns at the bottom of the page
        Dim SQLconn As SqlConnection
        Dim SQLComm As SqlCommand
        Dim SQLDr As SqlDataReader
        Dim GroupID As String = Session.Item("GroupID")

        SQLconn = New SqlConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)
        SQLComm = New SqlCommand("prcHHGenResultList", SQLconn)
        SQLComm.CommandType = CommandType.StoredProcedure
        SQLComm.Parameters.AddWithValue("@GroupID", GroupID)

        Try
            SQLconn.Open()

            SQLDr = SQLComm.ExecuteReader()

            'Set the values and the text of the dropdown
            ddlSPBusinessPDVResult.DataSource = SQLDr
            ddlSPBusinessPDVResult.DataValueField = "ResultID"
            ddlSPBusinessPDVResult.DataTextField = "ResultDescription"
            ddlSPBusinessPDVResult.DataBind()

            SQLDr.Close()

            SQLComm = New SqlCommand("prcHHGenResultList", SQLconn)
            SQLComm.CommandType = CommandType.StoredProcedure
            SQLComm.Parameters.AddWithValue("@GroupID", GroupID)
            SQLComm.Parameters.AddWithValue("@ClientID", ClientID)

            'Add blank row first
            Dim lifirst As New ListItem("", "")
            ddlSPBusinessPDVResult.Items.Insert(0, lifirst)
        Catch ex As Exception
            Session("Error") = ex.Message
            Response.Redirect("Error.aspx")
        Finally
            SQLconn.Close()
        End Try
    End Sub



任何建议深表感谢!

谢谢,



Any advice is much appreciated!!

Thanks,

推荐答案

好吧...您已经在方法中拥有它了...所以....因为您没有真正指定它很难确定您在这里要问什么.

我会假设此代码在其他地方使用,但下拉菜单有所不同?在这种情况下,您可以使用下拉菜单作为参数,而不是使用ddlSPBusinessPDVResult.将其传递给ByRef而不是ByVal,以便它将正确更新.如果其他地方在您使用代码的地方发生了变化,您也希望将其作为参数传递.例如,您可能需要传递SQL parms的值.

我在想这样的东西:
Well...you already have it in a method...so....since you didn''t really specify it''s hard to know for sure what you''re asking here.

I''ll assume that this code is used in other places but for a different drop down? If that is the case, instead of using ddlSPBusinessPDVResult you''ll want to pass in a drop down as a parameter. Pass it in ByRef instead of ByVal so that it will update it properly. If other things change in the different places you use the code, you''ll want to pass those in as parameters too. For example, the values of your SQL parms will probably need to be passed in.

I''m thinking something like this:
Public Sub FillDDL(ByRef ddl As System.Web.UI.WebControls.DropDownList, ByVal strGroupID As String, ByVal strClientID As String)
   'The code you have already, reworked using dll instead of dllSPBusinessPDVResult
   'And using the new strGroupID and strClientID parms
End Sub



虽然,如果要在一个网页上调用此方法,则可能还希望将SQL命令作为parm传递.这样,您可以一次设置它,然后在每次调用它时都重新使用它,而不是每次都重新创建它.



Although, if you''re calling this method on one web page, you''ll probably also want to pass in the SQL command as a parm. That way you can set it up once and then re-use it everytime you call it instead of recreating it every time.


你好

您可以只使用session的值而不是session

例如:

Hello

You can just use the value of session instead of session

for example:

Public Function PopulateResults(groupID As String ... and somethings else) As String
	'...

	Try
		'...

		'At the last line of Try:
		Return String.Empty
	Catch ex As Exception
		'Session["Error"] = ex.Message
		'Response.Redirect("Error.aspx")
		Return ex.Message
	Finally
		SQLconn.Close()
	End Try
End Function



然后使用以下代码代替调用方法:



Then instead of calling the method, use this code:

Dim [error] As String = PopulateResults(groupID)

If Not String.IsNullOrEmpty([error]) Then
	Session("Error") = [error]
	Response.Redirect("Error.aspx")
End If


在您的方法中创建方法业务层并返回一个可用于绑定UI中的控件的数据源.许多人尝试创建类似的方法,然后将控件作为参数进行填充,但这不允许应用程序出于灵活性和可扩展性而需要松散耦合.

您还应该了解使用状态
Create the method in your business layer and return a datasource that can be used to bind the controls in your UI. Many people try to create similar methods and pass the control to be populated as a parameter but that doesn''t allow for the loose coupling an application needs for flexibility and extensibility.

You should also learn about the using state
using(SqlConnection conn = new SqlConnection(...))
{

}


这篇关于创建方法/类以填充下拉菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆