-->

Get value from programmatically created TextBox in

2019-05-26 20:41发布

问题:

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

回答1:

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:

Button queryButton = new Button();
queryButton.UseSubmitBehavior = false;
queryButton.ID = "querybutton";
queryButton.Text = "Query";
queryButton.Click += new EventHandler(queryButton_Click);
Controls.Add(queryButton);

Then, write the click event handler:

void queryButton_Click(object sender, EventArgs e)
{
    TextBox querybox = this.FindControl("querybox") as TextBox;
    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
}


回答2:

Try stepping through and looking into the Request.Form["name"] object.

What is probably happening is your Text box may not be being saved properly in the view state, but if it is a valid form object when a post back occurs it should exist within the Request.Form object, the ID may be different so you may have to do some searching.



回答3:

Trikks,

I've found this which might help you.

Try looking for the text box in the load event (after checking its a postback!) rather than CreateChildControls which will separate your code out and make things a bit clearer. As Mmerrell says the id will probably get altered too, depending on the rest of the page.



回答4:

You get the null reference exception because you do a

TextBox querybox = (TextBox)FindControl("querybox");

on the PAGE object. So you're searching for page->querybox

But the textbox is in page->form1->querybox.

You need to write a recursive findcontrol, because querybox is a control in the form1 control, not a control in page.

public static Control FindControlRecursive(Control container, string name)
{
    if ((container.ID != null) && (container.ID.Equals(name)))
        return container;

    foreach (Control ctrl in container.Controls)
    {
        Control foundCtrl = FindControlRecursive(ctrl, name);
        if (foundCtrl != null)
            return foundCtrl;
    }
    return null;
}


回答5:

It might help to wrap the code in a !Page.IsPostBack check. Otherwise the textboxes get recreated on postback and delete any information.

if (!Page.IspostBack) {
    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);
} else {
    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

Oh and don't trust the user to enter a query on your database.

Your database will crash and burn