2.foreach遍历Repeater时,使用FindContrl需要判空,记得以前没有这个操作。
<table class="r_line" cellpadding="3" cellspacing="1" width="90%" style="margin-top:20px;">
<tr class="r_title">
<td align="center" style="100px;">编号</td>
<td align="center">产品名称</td>
</tr>
<asp:Repeater ID="rptRecord" runat="server" OnItemDataBound="rptRecord_ItemDataBound">
<ItemTemplate>
<tr style='background-color:<%#(Container.ItemIndex%2==0)?"#fff":"#eee"%>'>
<td><asp:Label ID="lblPID" runat="server" Text="Label"></asp:Label></td>
<td><asp:TextBox ID="txtPName" runat="server" Width="200px"></asp:TextBox></td>
</tr>
</ItemTemplate>
<FooterTemplate>
<tr class="r_bg">
<td colspan="2" align="center">
<asp:Label ID="lblEmpty" Text="无记录.." runat="server" Visible='<%#bool.Parse((rptRecord.Items.Count==0).ToString())%>'></asp:Label>
</td>
</tr>
</FooterTemplate>
</asp:Repeater>
</table>
<tr class="r_title">
<td align="center" style="100px;">编号</td>
<td align="center">产品名称</td>
</tr>
<asp:Repeater ID="rptRecord" runat="server" OnItemDataBound="rptRecord_ItemDataBound">
<ItemTemplate>
<tr style='background-color:<%#(Container.ItemIndex%2==0)?"#fff":"#eee"%>'>
<td><asp:Label ID="lblPID" runat="server" Text="Label"></asp:Label></td>
<td><asp:TextBox ID="txtPName" runat="server" Width="200px"></asp:TextBox></td>
</tr>
</ItemTemplate>
<FooterTemplate>
<tr class="r_bg">
<td colspan="2" align="center">
<asp:Label ID="lblEmpty" Text="无记录.." runat="server" Visible='<%#bool.Parse((rptRecord.Items.Count==0).ToString())%>'></asp:Label>
</td>
</tr>
</FooterTemplate>
</asp:Repeater>
</table>
protected void btnUpdate_Click(object sender, EventArgs e)
{
foreach (Control c in rptRecord.Controls)
{
//这样子行
Label lblPID = c.FindControl("lblPID") as Label;
string strPID = "";
if (lblPID != null)//必须加这个判断
strPID = lblPID.Text;
TextBox txtPName = c.FindControl("txtPName") as TextBox;
string strPName = "";
if (txtPName != null)
strPName = txtPName.Text;
//下面的都不行,应该是因为这里会有null出现,不知何故。
//string strPName = ((TextBox)c.FindControl("txtPName")).Text;
//string strPName = (c.FindControl("txtPName") as TextBox).Text;
//业务逻辑
}
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", "<script>alert('更新成功!')</script>", false);
}
protected void rptRecord_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
DataRowView drv = e.Item.DataItem as DataRowView;
Label lblPID = e.Item.FindControl("lblPID") as Label;//如果是绑定的DataReader则找不到,不知何故。
lblPID.Text = drv["PID"].ToString();
TextBox txtPName = e.Item.FindControl("txtPName") as TextBox;
txtPName.Text = drv["pName"].ToString();
}
}
{
foreach (Control c in rptRecord.Controls)
{
//这样子行
Label lblPID = c.FindControl("lblPID") as Label;
string strPID = "";
if (lblPID != null)//必须加这个判断
strPID = lblPID.Text;
TextBox txtPName = c.FindControl("txtPName") as TextBox;
string strPName = "";
if (txtPName != null)
strPName = txtPName.Text;
//下面的都不行,应该是因为这里会有null出现,不知何故。
//string strPName = ((TextBox)c.FindControl("txtPName")).Text;
//string strPName = (c.FindControl("txtPName") as TextBox).Text;
//业务逻辑
}
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", "<script>alert('更新成功!')</script>", false);
}
protected void rptRecord_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
DataRowView drv = e.Item.DataItem as DataRowView;
Label lblPID = e.Item.FindControl("lblPID") as Label;//如果是绑定的DataReader则找不到,不知何故。
lblPID.Text = drv["PID"].ToString();
TextBox txtPName = e.Item.FindControl("txtPName") as TextBox;
txtPName.Text = drv["pName"].ToString();
}
}
private void Bind()
{
string Sqlstr = "select * from t_ProList";
DataTable dt = new DataTable();
using (SqlConnection conn = new SqlConnection(ConfigurationManager.AppSettings["SQLCONNECTIONSTRING"].ToString()))
{
SqlDataAdapter da = new SqlDataAdapter(Sqlstr, conn);
conn.Open();
da.Fill(dt);
conn.Close();
}
rptRecord.DataSource = dt;//这里必须用DataTable才能动态绑定,绑定DataReader不行。
rptRecord.DataBind();
}
{
string Sqlstr = "select * from t_ProList";
DataTable dt = new DataTable();
using (SqlConnection conn = new SqlConnection(ConfigurationManager.AppSettings["SQLCONNECTIONSTRING"].ToString()))
{
SqlDataAdapter da = new SqlDataAdapter(Sqlstr, conn);
conn.Open();
da.Fill(dt);
conn.Close();
}
rptRecord.DataSource = dt;//这里必须用DataTable才能动态绑定,绑定DataReader不行。
rptRecord.DataBind();
}