Title

Saturday, 7 February 2015

How to retrieve the user input from the dynamically generated text box inside a panel of webpage


I have a Dropdownlist whose datasource is a SqlDataSource. I am able to generate two text boxes inside a panel using OnSelectedIndexChanged DropDownlist Event.

My C# code:

protected void DropDownList5_SelectedIndexChanged(object sender, EventArgs e)   {   Table table = new Table();   TableCell cell1 = new TableCell();   TableCell cell2 = new TableCell();   TableRow row = new TableRow();   var tb1 = new WebForms.TextBox();   var tb2 = new WebForms.TextBox();     // Set a unique ID for each TextBox added   tb1.ID = "lowerbound";   tb2.ID = "upperbound";     // Add the control to the TableCell   cell1.Controls.Add(tb1);   cell2.Controls.Add(tb2);     // Add the TableCell to the TableRow   row.Cells.Add(cell1);   row.Cells.Add(cell2);   table.Rows.Add(row);   Panel5.Controls.Add(table);   }

I have a button click event to search the input criteria given by the user in those two dynamically generated text boxes. The button click event will also display the results in a JQGrid.

Button Click Event for Search: C# Code

protected void Button1_Click(object sender, EventArgs e)   {   Panel6.Visible = true;   JQGrid9.Visible = true;   TextBox lowerbound = Panel5.FindControl("lowerbound") as TextBox;   TextBox upperbound = Panel5.FindControl("upperbound") as TextBox;    /*Using FindControls i am trying to search for textbox controls generated dynamically  inside the panel5 based on the ID set in the dynamic text box creation logic in the  selectedindexchanged function.*/     con.Open();   SqlDataAdapter da = new SqlDataAdapter("SELECT Column Names FROM VXRESULT WHERE " + DropDownList5.SelectedValue + " >= " + lowerbound.Text + " AND " + DropDownList5.SelectedValue + " <= " + upperbound.Text, con);    /*Problem here is Object reference not set to an instance of an object.Nullreference Exception.   I am not able to retrieve the textbox inputs provided by user. How to proceed */   DataSet ds = new DataSet();   da.Fill(ds);   con.Close();   Session["DataforSearch"] = ds.Tables[0];     }
Answer

No comments:

Post a Comment