C# 中的变量无法从 html 文本框中获取值
VARIABLE IN C# cant get the value from html text box
我正在尝试在我的数据库中搜索订单,但无法找到输入文本框中的文本
我已经尝试了一些我在这里和其他网站上看到的东西,但变量无论如何都无法获得值
Asp.net
<div class="input-container">
<input type="text" id="txtSearch" runat="server" class="input-field" style="color: black;" placeholder="Pesquisar.."/>
<button id="btnsearch" class="wrapper" OnServerClick="btnsearch_Click" runat="server"><i class="fas fa-search" style="color:#8b9095;" ></i></button>
</div>
CS
protected void btnsearch_Click(object sender, EventArgs e)
{
con.Open();
var txtSearch = FindControl("txtSearch") as TextBox;
SqlCommand cmd = new SqlCommand("select DISTINCT No_ from [encomenda] where No_= @No_", con);
cmd.Parameters.AddWithValue("@No_", txtSearch);
SqlDataReader sdr = cmd.ExecuteReader();
if (sdr.Read())
{
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
sda.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
}
con.Close();
}
看到一个严重的错误...
首先,txtSearch
不是 TextBox
,而是 html input control
。所以这一行 returns null.
var txtSearch = FindControl("txtSearch") as TextBox;
正确的方法是
var txtSearchInput = FindControl("txtSearch") as HtmlInputText;
然后使用txtSearchInput.Value
或者您可以简单地使用 txtSearch.Value
作为
protected void btnsearch_Click(object sender, EventArgs e)
{
con.Open();
SqlCommand cmd = new SqlCommand("select DISTINCT No_ from [encomenda] where No_= @No_", con);
cmd.Parameters.AddWithValue("@No_", txtSearch.Value);
SqlDataReader sdr = cmd.ExecuteReader();
if (sdr.Read())
{
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
sda.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
}
con.Close();
}
我正在尝试在我的数据库中搜索订单,但无法找到输入文本框中的文本 我已经尝试了一些我在这里和其他网站上看到的东西,但变量无论如何都无法获得值
Asp.net
<div class="input-container">
<input type="text" id="txtSearch" runat="server" class="input-field" style="color: black;" placeholder="Pesquisar.."/>
<button id="btnsearch" class="wrapper" OnServerClick="btnsearch_Click" runat="server"><i class="fas fa-search" style="color:#8b9095;" ></i></button>
</div>
CS
protected void btnsearch_Click(object sender, EventArgs e)
{
con.Open();
var txtSearch = FindControl("txtSearch") as TextBox;
SqlCommand cmd = new SqlCommand("select DISTINCT No_ from [encomenda] where No_= @No_", con);
cmd.Parameters.AddWithValue("@No_", txtSearch);
SqlDataReader sdr = cmd.ExecuteReader();
if (sdr.Read())
{
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
sda.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
}
con.Close();
}
看到一个严重的错误...
首先,txtSearch
不是 TextBox
,而是 html input control
。所以这一行 returns null.
var txtSearch = FindControl("txtSearch") as TextBox;
正确的方法是
var txtSearchInput = FindControl("txtSearch") as HtmlInputText;
然后使用txtSearchInput.Value
或者您可以简单地使用 txtSearch.Value
作为
protected void btnsearch_Click(object sender, EventArgs e)
{
con.Open();
SqlCommand cmd = new SqlCommand("select DISTINCT No_ from [encomenda] where No_= @No_", con);
cmd.Parameters.AddWithValue("@No_", txtSearch.Value);
SqlDataReader sdr = cmd.ExecuteReader();
if (sdr.Read())
{
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
sda.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
}
con.Close();
}