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:
The following code comes from the DataListItemCreated event
This is the ShowQuestionPanel called from the above call and works fine except for the Focus() method.
From the watch window.
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
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" />
<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