当页面中有很多控件且控件的ID值只有数字差别时,可以定义数组控件
<table width="100%" class="infotable1" cellspacing="0" cellpadding="0">
<tr>
<td>产品型号:</td>
<td><asp:Label ID="lblSpec1" runat="server" Text=""></asp:Label></td>
<td><asp:Label ID="lblSpec2" runat="server" Text="" ></asp:Label></td>
<td><asp:Label ID="lblSpec3" runat="server" Text="" ></asp:Label></td>
<td ><asp:Label ID="lblSpec4" runat="server" Text="" ></asp:Label></td>
</tr>
<tr>
<td class = "tdr" > 产品厂商:</td>
<td class="tdr2"> <asp:Label ID="lblCom1" runat="server" Text=""></asp:Label></td>
<td><asp:Label ID="lblCom2" runat="server" Text=""></asp:Label></td>
<td><asp:Label ID="lblCom3" runat="server" Text=""></asp:Label></td>
<td><asp:Label ID="lblCom4" runat="server" Text=""></asp:Label></td>
</tr>
</table>
<tr>
<td>产品型号:</td>
<td><asp:Label ID="lblSpec1" runat="server" Text=""></asp:Label></td>
<td><asp:Label ID="lblSpec2" runat="server" Text="" ></asp:Label></td>
<td><asp:Label ID="lblSpec3" runat="server" Text="" ></asp:Label></td>
<td ><asp:Label ID="lblSpec4" runat="server" Text="" ></asp:Label></td>
</tr>
<tr>
<td class = "tdr" > 产品厂商:</td>
<td class="tdr2"> <asp:Label ID="lblCom1" runat="server" Text=""></asp:Label></td>
<td><asp:Label ID="lblCom2" runat="server" Text=""></asp:Label></td>
<td><asp:Label ID="lblCom3" runat="server" Text=""></asp:Label></td>
<td><asp:Label ID="lblCom4" runat="server" Text=""></asp:Label></td>
</tr>
</table>
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class test : System.Web.UI.Page
{
BLL.SqlScriptProduct bllSQL = new BLL.SqlScriptProduct();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string numStr = "8;9;10;11;12;13;14;";
string[] arr = numStr.Split(';');
///根据传递的Session分别显示在相应的列中
int controlID = 1;
for (int i = 0; i < arr.Length - 1; i++)
{
Label[] lbl = GetLabelControl(controlID);
SearchProduct(arr[i], bllSQL.strSQLSearID, lbl);
controlID++;
}
}
}
/// <summary>
/// 取出指定ID的所有文本控件
/// </summary>
/// <param name="j">id值</param>
/// <returns></returns>
private Label[] GetLabelControl(int ID)
{
Label[] pt = new Label[20];
pt[0] = (Label)this.FindControl("lblSpec" + ID);
pt[1] = (Label)this.FindControl("lblCom" + ID);
return pt;
}
public void SearchProduct(string strID, string strSQL, Label[] lbls)
{
DataTable dt = null;
dt = BLL.SQLHandler.SearchOrpmParaData_1(strSQL, strID);
//显示详细数据
if (dt != null && dt.Rows.Count > 0)
{
lbls[0].Text = dt.Rows[0].ItemArray[0].ToString();
lbls[1].Text = dt.Rows[0].ItemArray[1].ToString();
lbls[2].Text = dt.Rows[0].ItemArray[2].ToString();
lbls[3].Text = dt.Rows[0].ItemArray[3].ToString();
}
}
}
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class test : System.Web.UI.Page
{
BLL.SqlScriptProduct bllSQL = new BLL.SqlScriptProduct();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string numStr = "8;9;10;11;12;13;14;";
string[] arr = numStr.Split(';');
///根据传递的Session分别显示在相应的列中
int controlID = 1;
for (int i = 0; i < arr.Length - 1; i++)
{
Label[] lbl = GetLabelControl(controlID);
SearchProduct(arr[i], bllSQL.strSQLSearID, lbl);
controlID++;
}
}
}
/// <summary>
/// 取出指定ID的所有文本控件
/// </summary>
/// <param name="j">id值</param>
/// <returns></returns>
private Label[] GetLabelControl(int ID)
{
Label[] pt = new Label[20];
pt[0] = (Label)this.FindControl("lblSpec" + ID);
pt[1] = (Label)this.FindControl("lblCom" + ID);
return pt;
}
public void SearchProduct(string strID, string strSQL, Label[] lbls)
{
DataTable dt = null;
dt = BLL.SQLHandler.SearchOrpmParaData_1(strSQL, strID);
//显示详细数据
if (dt != null && dt.Rows.Count > 0)
{
lbls[0].Text = dt.Rows[0].ItemArray[0].ToString();
lbls[1].Text = dt.Rows[0].ItemArray[1].ToString();
lbls[2].Text = dt.Rows[0].ItemArray[2].ToString();
lbls[3].Text = dt.Rows[0].ItemArray[3].ToString();
}
}
}