I got this itching problem and I cant get the code to work
How can i read the value from the TextBox when the form is posted?
Some code...
protected override void CreateChildControls() {
base.CreateChildControls();
TextBox queryBox = new TextBox();
queryBox.ID = "querybox";
queryBox.ToolTip = "Enter your query here and press submit";
Controls.Add(queryBox);
Button queryButton = new Button();
queryButton.UseSubmitBehavior = false;
queryButton.ID = "querybutton";
Controls.Add(queryButton);
if (Page.IsPostBack == true) {
try {
string query = querybox.Text;
DataGrid dataGrid = new DataGrid();
dataGrid.DataSource = Camelot.SharePointConnector.Data.Helper.ExecuteDataTable(query, connectionString);
dataGrid.DataBind();
Controls.Add(dataGrid);
} catch (Exception a) {
Controls.Add(new LiteralControl(a.Message));
} // try
} // if
} // void
I've shortened the code a bit but you see the idea, its the string query = querybox.text that wont work. I've tried with a few different variants, i.e.
TextBox querybox = (TextBox)FindControl("querybox");
string query = querybox.Text;
But no...
Any tips is appreciated! Thanks
The problem is that your controls are not populated with values from the ViewState in CreateChildControls. I'd recommend using a click event handler on your button.
Update your button code:
Then, write the click event handler: