[英]Treat a general ASP.NET Control like a list-based control (i.e. DropDownList, RadioButtonList, etc.)
I have built a method that clears and resets a DropDownList with the appropriate data as specified by the user, like so:我已经构建了一个方法,可以使用用户指定的适当数据清除和重置 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);
}
While this works as well as it should, I noticed that the code to do the same for a RadioButtonList is identical and it made me wonder if there's a way to combine ResetDDL with the RadioButtonList equivalent: ResetRBL.虽然这可以正常工作,但我注意到对 RadioButtonList 执行相同操作的代码是相同的,这让我想知道是否有一种方法可以将 ResetDDL 与 RadioButtonList 等效项相结合:ResetRBL。 To do this, I tried replacing DropDownList
in the code with Control
, but this only yielded in a Control does not contain a definition for 'Items/DataSource'
error, so I looked to see if there was any way to tell the program that a given Control is a DropDownList or RadioButtonList and what to do with them once they are confirmed to be a DDL or RBL.为此,我尝试用Control
替换代码中的DropDownList
,但这仅在Control does not contain a definition for 'Items/DataSource'
产生, Control does not contain a definition for 'Items/DataSource'
错误Control does not contain a definition for 'Items/DataSource'
,因此我查看是否有任何方法可以告诉程序给定的 Control 是 DropDownList 或 RadioButtonList 以及一旦确认它们是 DDL 或 RBL 后如何处理它们。 The idea here is to take the name of one of these controls, find the actual control itself, and execute the reset method.这里的想法是取这些控件之一的名称,找到实际的控件本身,并执行重置方法。
From here, I ran a test to see if I could get the Control's type -- and I could!从这里,我运行了一个测试,看看我是否能得到 Control 的类型——我能!
bool itworks = false;
string tst = rbl.GetType().ToString(); // in this case, tst is "System.Web.UI.WebControls.DropDownList"
if (tst.Contains("RadioButtonList"))
itworks = true;
From here I thought getting the control type would be as simple as getting the text out of a Control in a GridView, but this was not the case.从这里开始,我认为获取控件类型就像从 GridView 中的控件中获取文本一样简单,但事实并非如此。 I realized that whereas you can easily fetch the GridView's text and put it into a procedure --我意识到虽然您可以轻松获取 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();
}
-- it doesn't seem like quite the same can be done for a vehicle outside a GridView. -- 对于 GridView 之外的车辆,似乎不太一样。 But still I'm wondering: is there a way to take a Control's name as a string, find the actual control of the same name, and use the methods of the control type I'm looking for?但我仍然想知道:有没有办法将控件的名称作为字符串,找到同名的实际控件,并使用我正在寻找的控件类型的方法? If this is possible, I think it could help a bit to chop down the amount of code at my disposal.如果这是可能的,我认为减少我可以使用的代码量会有所帮助。
Any help at all would be greatly appreciated!任何帮助都将不胜感激!
The problem is that the type of control being taken in. Rather than a Control
, the method should accept a ListControl
like so:问题在于所采用的控件类型。该方法应该接受一个ListControl
而不是Control
,如下所示:
public void ResetList(ListControl control, DataTable newData)
{
control.Items.Clear();
control.DataSource = newData;
control.DataBind();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.