>I sort of understand this casting or typing subject in ASP.NET, and C# specifically, but it's just weird, coming from a Visual FoxPro background. An example of this follows from Stephen Walther's book, titled "ASP.NET 3.5 Unleashed". The value that is found with the FindControl function is already coming from a textbox, and yet the value has to be Typed as a TextBox? Or, maybe it's not quite a TextBox, because the FindControl() function is only finding the txtSearch control on the PreviousPage, not really able to return it as a TextBox object? I suppose this is more C# language "stuff", not really confined to ASP.NET. As a VFP guy, it's a bit weird, although somewhat understandable with repetition and useage.
>
>This code is found on page 99:
>
>TextBox txtSearch = (TextBox)PreviousPage.FindControl("txtSearch");
>lblSearch.Text = String.Format("Search For: {0}", txtSearch.Text);
>
Beyond the reasons why casting is required which have been explained it's worth noting that this code is unsafe. If "txtSearch" is not found then txtSearch will be null and the next line will fail.
Just as bad - if it is found and is not a TextBox the cast itself will fail. Better:
TextBox txtSearch = PreviousPage.FindControl("txtSearch") as TextBox ;
if (txtSearch !=null)
lblSearch.Text = String.Format("Search For: {0}", txtSearch.Text);