Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can't seem to use focus() on my textbox

Status
Not open for further replies.

tshad

Programmer
Jul 15, 2004
386
US
I have tried to set focus to a textbox in my DataList Footer section which has a textbox and I get a null exception for some reason.

The FooterTemplate looks like:

Code:
<FooterTemplate>
    <asp:Panel ID="newQuestionPanel" Visible="false" runat="server">
		<table border="0" cellpadding="0" cellspacing="0">
			<tr>
				<td width="100%">
                  	<asp:TextBox ID="newQuestion" Columns="80" runat="server" />&nbsp;
				<td>
                    <asp:LinkButton CommandName="AddQuestion" CssClass="vertical-align:top" 
                        Text="<img src='..\..\images\addButton.gif'border='0'>" 
                        ID="btnAdd" runat="server" />            
				</td>
			</tr>
		</table>
    </asp:Panel>
    <asp:Button ID="AddQuestion" Text="Add Question" runat="server" 
        CommandName="AddRow" CommandArgument="AddRow"></asp:Button>
</FooterTemplate>

The following code comes from the DataListItemCreated event
Code:
public void DataList_ItemCreated(object Sender, DataListItemEventArgs e)
{
    switch (e.Item.ItemType)
    {
        case ListItemType.Footer:
            if (ShowQuestion)
            {
                ShowQuestionPanel((Panel)e.Item.FindControl("newQuestionPanel"));
            }
            break;
    }
}

This is the ShowQuestionPanel called from the above call and works fine except for the Focus() method.
Code:
        private void ShowQuestionPanel(Panel panel)
        {
            TextBox textbox;

            ((Button)(panel.FindControl("AddQuestion"))).Text = "Close Question";
            // ((TextBox)(panel.FindControl("NewQuestion"))).Focus();
            textbox = (TextBox)panel.FindControl("NewQuestion");

            textbox.Text = "toms test";
            //textbox.Focus();

        }

From the watch window.
Code:
textbox.Focus()	'textbox.Focus()' threw an exception of type 'System.NullReferenceException'
textbox.Text	"toms test"

So why can I set the Text but I can't set the focus to the Textbox???
Why is it a SystemNullReferenceException

I originally tried to do it in one statement (which is commented out above and also got the System.NullReferenceException.

But the following works as it does when I create the Textbox explicitly:

((TextBox)(panel.FindControl("NewQuestion"))).Text

Why doesn't this work?

Thanks,

Tom
 
Also, when I press the button, this seems to work:

((TextBox)(((Button)e.CommandSource).Parent.FindControl("NewQuestion"))).Focus();

So not sure why the other code doesn't.

Tom
 
I found out that if the program "ShowQuestionPanel", is called from: DataList_ItemCreated - it doesn't work (the Focus() get the error).

If the same method is called from: DataList_ItemCommand - it does work.

Not sure why it works in one but not the other.

In both cases, I can do:

textbox = (TextBox)panel.FindControl("NewQuestion");
textbox.Text = "toms test";

Thanks,

Tom
 
Fine.

But the question is why doesn't this work?

Thanks,

Tom
 
I'm not sure if this matters, but in your code you have "NewQuestion", but the control name in the markup "newQuestion"
Not sure if the case would somehow make a difference.

As far as your code is, it should work. I looked up on MSDN about the ItemCreated event and you should have access to the controls per their examples.
 
But access isn't the problem as you can see, I do have access. I can set the .Text property. But I get an error with the .Focus.

But I only have the problem in the DataList_ItemCreated but not in the DataList_ItemCommand.

Thanks,

Tom
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top