我尝试使用asp c#.net
从mssql数据库绑定telerik自动填充文本框。
<telerik:RadAutoCompleteBox AllowCustomEntry="true" ID="RadAutoCompleteBox2" runat="server" InputType="Text">
<TextSettings SelectionMode="Single" />
</telerik:RadAutoCompleteBox>
我的.aspx.cs文件的代码如下所示
protected void Page_Load(object sender, EventArgs e)
{
try
{
SqlDataSource SqlDataSource1 = new SqlDataSource();
SqlDataSource1.ID = "SqlDataSource1";
this.Page.Controls.Add(SqlDataSource1);
SqlDataSource1.ConnectionString = "my connection";
SqlDataSource1.SelectCommand = "select citylocation from citylocationtbl where cityname=@cityname";
SqlDataSource1.SelectParameters.Add("@cityname", Request.QueryString["city"].ToString());
RadAutoCompleteBox2.DataSourceID = "SqlDataSource1";
RadAutoCompleteBox2.DataTextField = "citylocation";
RadAutoCompleteBox2.DataBind();
}
catch (Exception ex)
{
}
}
当我删除参数时,代码工作正常,但在参数行I中仍然出现错误。
已声明变量名@cityname。变量名在查询批处理或存储过程中必须是唯一的。
我收到错误,因为当我按任意键telerik autosuggesbox时会自动回复并一次又一次地绑定数据。
答案 0 :(得分:0)
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
LoadCity(Request.QueryString["city"].ToString());
}
}
protected void LoadCity(string _city)
{
try
{
SqlDataSource SqlDataSource1 = new SqlDataSource();
SqlDataSource1.ID = "SqlDataSource1";
this.Page.Controls.Add(SqlDataSource1);
SqlDataSource1.ConnectionString = "my connection";
SqlDataSource1.SelectCommand = "select citylocation from citylocationtbl where cityname=@cityname";
SqlDataSource1.SelectParameters.Add("@cityname", _city);
RadAutoCompleteBox2.DataSourceID = "SqlDataSource1";
RadAutoCompleteBox2.DataTextField = "citylocation";
RadAutoCompleteBox2.DataBind();
}
catch (Exception ex)
{
}
}