[英]Populate drop down list in Web Forms
I am populating DDL's in asp.net web forms and use a sqlutilility class that I have for all my sql classes that call my stored procedures. 我正在asp.net Web表单中填充DDL,并为所有调用存储过程的sql类使用sqlutilility类。
But on each drop down list I keep repeating the same code and renameing it and then calling it when I want to use it. 但是在每个下拉列表中,我一直重复相同的代码并重命名它,然后在我想使用它时调用它。
What I want to know is. 我想知道的是。 is there a way using this code instead of repeating it all the time?
有没有办法使用此代码而不是一直重复?
protected void Populate_Authority()
{
// Init()
// ------
SqlConnection conn = null;
SqlDataReader rdr = null;
SqlCommand cmd = null;
//
drp_Authority.Items.Clear();
try
{
// Conn
// ----
conn = new SqlConnection(ConfigurationManager.ConnectionStrings["conn"].ConnectionString);
conn.Open();
// Cmd
// ---
cmd = new SqlCommand("[sp_Populate_Authority]", conn);
cmd.CommandType = CommandType.StoredProcedure;
// Execute
// -------
rdr = cmd.ExecuteReader();
// Row(s)?
// -------
if (rdr.HasRows)
{
// Read
// ----
while (rdr.Read())
{
// Populate
// --------
drp_Authority.Items.Add(
new ListItem(
rdr["Authority"].ToString(),
rdr["AuthorityID"].ToString()));
}
// Blank
// -----
drp_Authority.Items.Insert(0, String.Empty);
}
// Clean up / Close down
// ---------------------
cmd.Dispose();
rdr.Dispose();
rdr.Close();
conn.Dispose();
conn.Close();
}
catch (SqlException ex)
{
// Throw Sql Exception
// -------------------
throw ex;
}
finally
{
// Clean up / Close down
// ---------------------
if (cmd != null)
{
cmd.Dispose();
}
if ((rdr != null) && (!rdr.IsClosed))
{
rdr.Dispose();
rdr.Close();
}
if ((conn != null) && (conn.State != ConnectionState.Closed))
{
conn.Dispose();
conn.Close();
}
}
}
Take your code, wrap it in a helper method: 获取您的代码,将其包装在辅助方法中:
protected void Populate_Ddl(DropDownList ddl, string command, string valueName, string keyName)
{
// Init()
// ------
SqlConnection conn = null;
SqlDataReader rdr = null;
SqlCommand cmd = null;
//
ddl.Items.Clear();
try
{
// Conn
// ----
conn = new SqlConnection(ConfigurationManager.ConnectionStrings["conn"].ConnectionString);
conn.Open();
// Cmd
// ---
cmd = new SqlCommand(command, conn);
cmd.CommandType = CommandType.StoredProcedure;
// Execute
// -------
rdr = cmd.ExecuteReader();
// Row(s)?
// -------
if (rdr.HasRows)
{
// Read
// ----
while (rdr.Read())
{
// Populate
// --------
ddl.Items.Add(
new ListItem(
rdr[keyName].ToString(),
rdr[valueName].ToString()));
}
// Blank
// -----
ddl.Items.Insert(0, String.Empty);
}
// Clean up / Close down
// ---------------------
cmd.Dispose();
rdr.Dispose();
rdr.Close();
conn.Dispose();
conn.Close();
}
catch (SqlException ex)
{
// Throw Sql Exception
// -------------------
throw ex;
}
finally
{
// Clean up / Close down
// ---------------------
if (cmd != null)
{
cmd.Dispose();
}
if ((rdr != null) && (!rdr.IsClosed))
{
rdr.Dispose();
rdr.Close();
}
if ((conn != null) && (conn.State != ConnectionState.Closed))
{
conn.Dispose();
conn.Close();
}
}
}
Then call it from your code: 然后从您的代码中调用它:
Populate_Ddl(drp_Authority, "[sp_Populate_Authority]", "Authority", "AuthorityID");
And repeat for all ddl's. 并重复所有ddl。
Make a generic function for filling dropdownlist as shown below: 制作一个通用功能来填充下拉列表,如下所示:
protected void Populate_DropDownList(DropDownList ddl, string storedProceduceName, string dropdownTextField, string dropdownValueField)
{
//Modify your code accordingly
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.