这是a.aspx文件
<form id="form1" runat="server">
<div id="top">
<asp:Label ID="Label1" text="产品编号" width="65px" runat="server"> </asp:Label>
<asp:TextBox ID="TextBox1" width="150px" runat="server"> </asp:TextBox>
<asp:Button ID="Button1" text="查询" runat="server" OnClick="Button1_Click"> </asp:Button>
</div>
</form>这是a.aspx.cs文件
conn.ConnectionString = connString;
conn.Open();
string productID = ((TextBox)Page.FindControl("TextBox1")).Text.ToString();
sqlString = "SELECT * FROM ProductInfo WHERE ID='%" + TextBox1.Text.ToString() + "%'";
SqlCommand cmd = new SqlCommand(sqlString, conn);
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);如题???
如在a.aspx.cs 直接用TextBox1.text
sqlString = "SELECT * FROM ProductInfo WHERE ID='" + TextBox1.Text.ToString() + "'";
在编译是就会出错:"当前上下文中找不到控件名称"TextBox1""
如这句替换
sqlString = "SELECT * FROM ProductInfo WHERE ID='" + ((TextBox)Page.FindControl("TextBox1")).Text.ToString()+ "'";
就不会出错
<form id="form1" runat="server">
<div id="top">
<asp:Label ID="Label1" text="产品编号" width="65px" runat="server"> </asp:Label>
<asp:TextBox ID="TextBox1" width="150px" runat="server"> </asp:TextBox>
<asp:Button ID="Button1" text="查询" runat="server" OnClick="Button1_Click"> </asp:Button>
</div>
</form>这是a.aspx.cs文件
conn.ConnectionString = connString;
conn.Open();
string productID = ((TextBox)Page.FindControl("TextBox1")).Text.ToString();
sqlString = "SELECT * FROM ProductInfo WHERE ID='%" + TextBox1.Text.ToString() + "%'";
SqlCommand cmd = new SqlCommand(sqlString, conn);
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);如题???
如在a.aspx.cs 直接用TextBox1.text
sqlString = "SELECT * FROM ProductInfo WHERE ID='" + TextBox1.Text.ToString() + "'";
在编译是就会出错:"当前上下文中找不到控件名称"TextBox1""
如这句替换
sqlString = "SELECT * FROM ProductInfo WHERE ID='" + ((TextBox)Page.FindControl("TextBox1")).Text.ToString()+ "'";
就不会出错
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货