Get value from programmatically created TextBox in C#

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-06 11:55:20

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
}

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.

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.

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;
}

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!