将一般 ASP.NET 控件视为基于列表的控件(即 DropDownList、RadioButtonList 等)
Treat a general ASP.NET Control like a list-based control (i.e. DropDownList, RadioButtonList, etc.)
我构建了一个方法,可以使用用户指定的适当数据清除和重置 DropDownList,如下所示:
protected void ResetDDL(DropDownList ddl, DataTable dt)
{
ddl.Items.Clear();
ddl.DataSource = dt;
ddl.DataBind();
}
private void LoadVehicleTypes()
{
DataTable dt = new DataTable();
SqlConnection sc = new SqlConnection(ConfigurationManager.ConnectionStrings["sqlconnectionstring"].ConnectionString);
sc.Open();
SqlDataAdapter sda = new SqlDataAdapter("procedure", sc);
sda.SelectCommand.CommandType = CommandType.StoredProcedure;
sda.Fill(dt);
sc.Close();
ResetDDL(ddlYourDropDown, dt);
}
虽然这可以正常工作,但我注意到为 RadioButtonList 执行相同操作的代码是相同的,这让我想知道是否有一种方法可以将 ResetDDL 与 RadioButtonList 等效项组合:ResetRBL。为此,我尝试将代码中的 DropDownList
替换为 Control
,但这只会导致 Control does not contain a definition for 'Items/DataSource'
错误,因此我查看是否有任何方法可以告诉程序给定的控件是 DropDownList 或 RadioButtonList,以及一旦它们被确认为 DDL 或 RBL 如何处理它们。这里的想法是取其中一个控件的名称,找到实际的控件本身,然后执行重置方法。
从这里开始,我 运行 进行了一次测试,看看我是否可以获得控件的类型 -- 我可以!
bool itworks = false;
string tst = rbl.GetType().ToString(); // in this case, tst is "System.Web.UI.WebControls.DropDownList"
if (tst.Contains("RadioButtonList"))
itworks = true;
从这里开始,我认为获取控件类型就像从 GridView 中的控件中获取文本一样简单,但事实并非如此。我意识到虽然您可以轻松获取 GridView 的文本并将其放入过程中 --
foreach (GridViewRow row in gvTempVehicleData.Rows)
{
SqlCommand cmdVData = new SqlCommand("cmdVDataProcedure", sc);
cmdVData.CommandType = CommandType.StoredProcedure;
cmdVData.Parameters.AddWithValue("DataYear", ((TextBox)row.FindControl("txtTempDataYear")).Text);
cmdVData.ExecuteNonQuery();
}
-- 对于 GridView 之外的车辆,似乎无法完成相同的操作。但我仍然想知道:有没有办法将一个控件的名称作为一个字符串,找到同名的实际控件,并使用我要查找的控件类型的方法?如果这是可能的,我认为它可以帮助减少我可以支配的代码量。
如有任何帮助,我们将不胜感激!
问题在于接受的控件类型。该方法应该接受 ListControl
,而不是 Control
,如下所示:
public void ResetList(ListControl control, DataTable newData)
{
control.Items.Clear();
control.DataSource = newData;
control.DataBind();
}
我构建了一个方法,可以使用用户指定的适当数据清除和重置 DropDownList,如下所示:
protected void ResetDDL(DropDownList ddl, DataTable dt)
{
ddl.Items.Clear();
ddl.DataSource = dt;
ddl.DataBind();
}
private void LoadVehicleTypes()
{
DataTable dt = new DataTable();
SqlConnection sc = new SqlConnection(ConfigurationManager.ConnectionStrings["sqlconnectionstring"].ConnectionString);
sc.Open();
SqlDataAdapter sda = new SqlDataAdapter("procedure", sc);
sda.SelectCommand.CommandType = CommandType.StoredProcedure;
sda.Fill(dt);
sc.Close();
ResetDDL(ddlYourDropDown, dt);
}
虽然这可以正常工作,但我注意到为 RadioButtonList 执行相同操作的代码是相同的,这让我想知道是否有一种方法可以将 ResetDDL 与 RadioButtonList 等效项组合:ResetRBL。为此,我尝试将代码中的 DropDownList
替换为 Control
,但这只会导致 Control does not contain a definition for 'Items/DataSource'
错误,因此我查看是否有任何方法可以告诉程序给定的控件是 DropDownList 或 RadioButtonList,以及一旦它们被确认为 DDL 或 RBL 如何处理它们。这里的想法是取其中一个控件的名称,找到实际的控件本身,然后执行重置方法。
从这里开始,我 运行 进行了一次测试,看看我是否可以获得控件的类型 -- 我可以!
bool itworks = false;
string tst = rbl.GetType().ToString(); // in this case, tst is "System.Web.UI.WebControls.DropDownList"
if (tst.Contains("RadioButtonList"))
itworks = true;
从这里开始,我认为获取控件类型就像从 GridView 中的控件中获取文本一样简单,但事实并非如此。我意识到虽然您可以轻松获取 GridView 的文本并将其放入过程中 --
foreach (GridViewRow row in gvTempVehicleData.Rows)
{
SqlCommand cmdVData = new SqlCommand("cmdVDataProcedure", sc);
cmdVData.CommandType = CommandType.StoredProcedure;
cmdVData.Parameters.AddWithValue("DataYear", ((TextBox)row.FindControl("txtTempDataYear")).Text);
cmdVData.ExecuteNonQuery();
}
-- 对于 GridView 之外的车辆,似乎无法完成相同的操作。但我仍然想知道:有没有办法将一个控件的名称作为一个字符串,找到同名的实际控件,并使用我要查找的控件类型的方法?如果这是可能的,我认为它可以帮助减少我可以支配的代码量。
如有任何帮助,我们将不胜感激!
问题在于接受的控件类型。该方法应该接受 ListControl
,而不是 Control
,如下所示:
public void ResetList(ListControl control, DataTable newData)
{
control.Items.Clear();
control.DataSource = newData;
control.DataBind();
}