Last post May 16, 2019 08:33 AM by PatriceSc
May 15, 2019 03:37 PM|JyotrmayaM|LINK
I want to know what's the difference between below two:
1 : (Control)this.FindControl("ControlName");
2: FindControl<Control> (ControlName) as control
May 15, 2019 04:07 PM|PatriceSc|LINK
Seems rather Web Forms than Web Pages. The first one returns a control named this way (or null if not found) :
The 2nd is not part of .NET but rather a popular extension so the exact behavior may differ depending on the actual implementation. I expect FindControl<T> to return a control named this way AND being assignable to the expected type. If the type is not the
expected one it could return null or may be throws an exception
Then it is using https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/as which is likely misused here. It allows to get an object that is assignable
to the type or null (which by design is what I expected FindControl<T> to return already)
So IMO both lines
var obj=FindControl<MyType>("MyName") as MyType;
will just always produce the same result ie "as MyType" doesn't server any purpose.
May 16, 2019 05:56 AM|samwu|LINK
I tried your FindControl<Control> (ControlName) as control and found that there is no FindControl<Control> method in Asp.net. Can you share your complete code? Or what do you want to do with it?
(Control)this.FindControl("ControlName"); This is searches the current naming container for the specified server control.
<asp:TextBox ID="TextBox1" runat="server">TextBox</asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
protected void Button1_Click(object sender, EventArgs e)
//Control c = this.FindControl("TextBox1");
//TextBox tb= (TextBox)c;
TextBox tb = (TextBox)this.FindControl("TextBox1");
this.Label1.Text = tb.Text;
May 16, 2019 07:53 AM|JyotrmayaM|LINK
I'm trying to achieve exactly what you've given example of.
The thing is the same code has been playing it's purpose till date (written by some other developer). It never came to my notice, till yesterday when bug has been raised.
Just want to confirm if both works the same or same or anything is different?
May 16, 2019 07:54 AM|JyotrmayaM|LINK
So did you mean both the syntax exactly the same?
May 16, 2019 08:33 AM|PatriceSc|LINK
I meant #1 is the standard .NET function.
That for #2 I expect that "FindControl<Control>(ControlName) as Control" could be shortened to "FindControl<Control>(ControlName)" (and it's not standard so what it does exactly depends on the exact implementation).
Now if you have a bug it's best to tell about it directly so that we better understand what you want to discuss and can focus on your exact problem. Do you mean you that your app works when using #1 but fails using #2 instead (or the other way round
? for example #2 could be recursive while #1 is not so it's possible that #2 could find a control that #1 won't be able to find).
If you need further help please tell what happens so that we can know the EXACT problem you are dealing with.