开发者

Get value from programmatically created TextBox in C#

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 CreateChildControl开发者_开发问答s() {

    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:

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜