【发布时间】:2014-05-02 01:36:27
【问题描述】:
我有一个 DropDownList 与 SqlDataSource 中的项目列表绑定。在 DropDownList_SelectedIndexchanged() 函数的帮助下,我能够生成两个动态文本框。
必需输出:我需要根据用户输入的文本框搜索数据,搜索到的数据将在 Button_Click() 事件的帮助下显示在 JQGrid 中。
当前问题:未检索文本框输入,并且始终检索为空字符串“”。 获得的异常是:“AND”附近的语法不正确(SQL 查询)
如何解决这个问题?
我的aspx代码:
<asp:Panel ID="Panel5" runat="server" Height="221px">
<span style="font-size: 135%; font-family: Verdana; font-weight: bold"> Search Functionalities </span>
<asp:DropDownList ID="DropDownList5" runat="server" DataSourceID="column_list_for_filter" DataTextField="All_Columns" DataValueField="All_Columns" OnSelectedIndexChanged ="DropDownList5_SelectedIndexChanged" AutoPostBack="true">
</asp:DropDownList>
<asp:SqlDataSource ID="column_list_for_filter" runat="server" ConnectionString="<%$ ConnectionStrings:DatabaseConnectionString %>" SelectCommand="SELECT COLUMN_NAME 'All_Columns' FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME='RESULT' "></asp:SqlDataSource>
<asp:Button ID="Button1" runat="server" Font-Bold="True" Font-Names="Arial" Font-Size="Small" OnClick="Button1_Click" Text="Search Flow Periods" Width="144px" />
<asp:Table ID="dynamic_filter_table" runat="server" ToolTip="Results">
</asp:Table>
</asp:Panel>
我的 C# 代码:
//Creation of Two Dynamic Text Box Web Controls on DDL selection
protected void DropDownList5_SelectedIndexChanged(object sender, EventArgs e)
{
createdynamiccontrols();
}
/*Two Text Boxes and Two Labels for input and search the FlowPeriod and display in JqGrid
thru button click event*/
protected void createdynamiccontrols()//(string ID1, string ID2)
{
int i = DropDownList5.SelectedIndex;
++i;
TableRow row;
row = new TableRow();
TableCell cell1 ;
cell1 = new TableCell();
TableCell cell2;
cell2= new TableCell();
// Set a unique ID for each TextBox added
TextBox tb1;
tb1 = new TextBox();
TextBox tb2;
tb2 = new TextBox();
Label lbl1;
lbl1 = new Label();
Label lbl2;
lbl2 = new Label();
// Set a unique ID for each TextBox added
tb1.ID = "lowerbound_" + i.ToString();
tb2.ID = "upperbound_"+ i.ToString() ;
lbl1.Text = "LowerBound:";
lbl1.Font.Size = FontUnit.Point(10);
lbl1.Font.Bold = true;
lbl1.Font.Name = "Arial";
lbl2.Text = "UpperBound:";
lbl2.Font.Size = FontUnit.Point(10);
lbl2.Font.Bold = true;
lbl2.Font.Name = "Arial";
// Add the control to the TableCell
cell1.Controls.Add(lbl1);
cell1.Controls.Add(tb1);
cell2.Controls.Add(lbl2);
cell2.Controls.Add(tb2);
// Add the TableCell to the TableRow
row.Cells.Add(cell1);
row.Cells.Add(cell2);
dynamic_filter_table.Rows.Add(row);
dynamic_filter_table.EnableViewState = true;
ViewState["dynamic_filter_table"] = true;
Button1.EnableViewState = true;
ViewState["Button_1"] = true;
}
protected override object SaveViewState()
{
object[] viewstate = new object[2];
List<string> dynamic_text_values = new List<string>();
foreach (TableRow row in dynamic_filter_table.Controls)
{
foreach (TableCell cell in row.Controls)
{
if (cell.Controls[1] is TextBox)
{
dynamic_text_values.Add(((TextBox)cell.Controls[1]).Text);
}
}
}
viewstate[0] = dynamic_text_values.ToArray();
viewstate[1] = base.SaveViewState();
return viewstate;
}
protected override void LoadViewState(object savedState)
{
if (savedState is object[] && ((object[])savedState).Length == 2 && ((object[])savedState)[0] is string[])
{
object[] newViewState = (object[])savedState;
string[] txtValues = (string[])(newViewState[0]);
if (txtValues.Length > 0)
{
createdynamiccontrols();
}
base.LoadViewState(newViewState[1]);
}
else
{
base.LoadViewState(savedState);
}
}
protected void Button1_Click(object sender, EventArgs e)
{
createdynamiccontrols();
int j = DropDownList5.SelectedIndex;
++j;
Panel6.Visible = true;
JQGrid9.Visible = true;
TextBox lowerboundd = dynamic_filter_table.FindControl("lowerbound_" + j.ToString()) as TextBox;
TextBox upperbound = dynamic_filter_table.FindControl("upperbound_" + j.ToString()) as TextBox;
string testt = lowerboundd.Text;
con.Open();
SqlDataAdapter da = new SqlDataAdapter("SELECT ColumnName1,Columnname2 FROM RESULT WHERE " + DropDownList5.SelectedValue + " >= " + lowerboundd.Text + " AND " + DropDownList5.SelectedValue + " <= " + upperbound.Text, con);
DataSet ds = new DataSet();
da.Fill(ds);
/*Error occurs here as Incorrect Syntax near AND as the string obtained is "" and not
textbox inputs*/
con.Close();
Session["DataforSearch"] = ds.Tables[0];
}
protected void Page_Load(object sender, EventArgs e)
{
//Dynamic controls creation on Page Load
if (!IsPostBack)
{
BindDropDownLists();
}
dynamic_filter_table.EnableViewState = true;
}
【问题讨论】:
-
当页面加载时,您是否看到 j 会选择的正确的 lowerbound_ 和 upperbound 框?
-
@Newyork167 当 Dropdownlist Index 为 16,j 的值为 16 和 ++j 的值为 17 以及在 dynamic_filter_column 表中使用 FindControl 查找的文本框 ID 为 string test = lowerbound.ID 结果是“lowerbound_17”
-
@Newyork167 添加了我的调试截图
-
lowerboundd和upperbound中的值是多少?如果是integer,那么您的查询将起作用。如果不是,那么您需要将TextBox的值包含在查询内的单引号内,如DropDownList5.SelectedValue + " >= '" + lowerboundd.Text + "' -
@Bharadwaj !!!我将 95 和 97 作为下限和上限文本框输入。即使在 savedstates() 和 loadviewstate() 函数中,值也总是以“”的形式出现。如何找回它们?