FormView 子控件的text值
<asp:FormView ID="FormView1" runat="server" DataSourceID="AccessDataSource1"><ItemTemplate>
软件名称:
<asp:Label ID="nameLabel" runat="server" Text='<%# Bind("name") %>'></asp:Label><br />
/ItemTemplate>
</asp:FormView>
现在程序中取出nameLabel子控件的text值,如何,谢谢 --------------------编程问答-------------------- 自己ding --------------------编程问答-------------------- 为什么没人回答 --------------------编程问答-------------------- 前台HTML代码:
<asp:FormView ID="FormView1" runat="server" DataSourceID="AccessDataSource1" OnItemDataBound="FormView1_OnItemDataBound">
<ItemTemplate>
软件名称: <asp:Label ID="nameLabel" runat="server" Text='<%# Bind("name") %>'></asp:Label> <br/>
/ItemTemplate>
</asp:FormView>
后台C#代码:
--------------------编程问答-------------------- FindControl --------------------编程问答-------------------- protected void Page_Load(object sender, EventArgs e)
protected void dtl_1OnItemDataBound(object sender, DataListItemEventArgs e,FormViewCommandEventArgs e)
{
Label myLabel = (Label)e.Item.FindControl("nameLabel"); // 寻找控件
string myString = myLabel.text; // myString就是你要的text值了
}
{
if (!IsPostBack)
{
BindFormView();
}
}
DataTable GetTable()
{
DataTable dt = new DataTable();
dt.Columns.Add("id", typeof(string));
string[] str = new string[20];
for (int i = 0; i < str.Length; i++)
{
str[i] = i.ToString();
}
for (int j = 0; j < str.Length; j++)
{
DataRow row = dt.NewRow();
row["id"] = str[j];
dt.Rows.Add(row);
}
return dt;
}
protected void Button2_Click(object sender, EventArgs e)
{
TextBox txtname = this.FormView1.Row.FindControl("txtname") as TextBox;
if (txtname != null)
{
txtname.Text = "找到了";
}
}
void BindFormView()
{
this.FormView1.DataSource = GetTable();
this.FormView1.DataBind();
} --------------------编程问答-------------------- 不好意思,弄错了。重发一遍,记住要在前台FormView控件中加入 “OnItemDataBound="FormView1_OnItemDataBound"”
前台HTML代码:
<asp:FormView ID="FormView1" runat="server" DataSourceID="AccessDataSource1" OnItemDataBound="FormView1_OnItemDataBound">
<ItemTemplate>
软件名称: <asp:Label ID="nameLabel" runat="server" Text='<%# Bind("name") %>'></asp:Label> <br/>
/ItemTemplate>
</asp:FormView>
后台C#代码:
--------------------编程问答-------------------- 我谢谢了,出差了,无法测试,回来加分,还想问问,vb代码如何写 --------------------编程问答--------------------
protected void FormView1_OnItemDataBound(object sender, FormViewCommandEventArgs e)
{
Label myLabel = (Label)e.Item.FindControl("nameLabel"); // 寻找控件
string myString = myLabel.text; // myString就是你要的text值了
}
Control oCtrl = FormView1.FindControl("nameLabel");
if(oCtrl != null)
{
string sText = ((Label)oCtrl).Text;
}
补充:.NET技术 , ASP.NET