Take a look at this code:

private void tahomaToolStripMenuItem_Click(object sender, EventArgs e) { rtbox.Font = new Font("Tahoma", rtbox.Font.Size); } 

This is a completely valid event code, taken from a working Windows Forms form.

And now look at this fragment:

 protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) { if (DropDownList1.SelectedItem == "Times New Roman") this.Label1.Font = new Font("Times New Roman", Label1.Font.Size); } 

It is already taken from the code of the Web Forms form. In both cases, the editor is VC # 2010. But in the second fragment, i.e. in forms for the web, Font - does not exist. And now I do not know how to change the font in the form. Can anyone suggest something?

    1 answer 1

    To begin with, your problem has nothing to do with the C # language. The language is the same for WinForms, as well as for ASP.NET, ASP.NET MVC, WPF, WCF and everything else.

    The difference in your two code fragments is not caused by differences in the language (it is, I repeat, the same everywhere), but by the fact that these fragments use different classes from different assemblies that have similar names and fields for some (not quite random) coincidence. / properties / methods. (In the first case, you have the System.Drawing.Font class from the System.Drawing.dll assembly, and in the second System.Web.UI.WebControls.FontInfo from the System.Web.dll assembly) In other words, you need not hope that By taking code from a WinForms application and stuffing it into an ASP.NET application, you won’t get a bunch of errors.

    Now for your code. As far as I understand, you want to change the font of the label when selecting an item from the drop-down list. Your mistake is that the Font property of the controls does not have a setter, and therefore write something like

     MyControl.Font = new Font(...); 

    You can not. However, you can change the properties of the font itself. For example:

     <asp:DropDownList runat="server" ID="Drop" OnSelectedIndexChanged="OnChanged" AutoPostBack="True"> <asp:ListItem>Times</asp:ListItem> <asp:ListItem>Arial</asp:ListItem> <asp:ListItem>Verdana</asp:ListItem> </asp:DropDownList> <asp:Label runat="server" ID="Label1" Font-Names="Verdana" Font-Size="10pt" >Caption</asp:Label> protected void OnChanged(object sender, EventArgs e) { Label1.Font.Name = (sender as DropDownList).SelectedItem.Text; } 

    And finally - if you want to make a web application, then use ASP.NET MVC for this, it is more convenient, logical and to the best extent separates the view and the model. And it is much more convenient to carry out manipulations on changing the contents of one control depending on the other using knockout.js

    • Exhaustive answer =) Somewhere else it was said that this feature can only be implemented with AJAX. But AJAX is what? Is it a library of classes for working with web forms under C #? - Metome
    • AJAX ( A synchronous Ja vascript and X ML) as such is not related to C # (although it is appropriate and even necessary in ASP.NET MVC applications). AJAX is a technology designed for convenient interaction between a client and a server "in the background." Roughly speaking, it allows you to send requests to the server and receive answers without reloading the page. Modern web applications use AJAX everywhere. For example, on this site you can add a comment without reloading the entire page (but you cannot add or edit a question or answer) - DreamChild
    • in principle, WebForm in a certain sense hides many client and server interactions, giving the programmer the illusion of working with event handlers as in WinForms, however, all the same client-server requests-responses are hidden under the hood, including using ajax. - DreamChild
    • It looks like I got the wrong idea about this technology due to the use of VisualStudio to do the job. In my humble experience, I can say that this or that technology is understood much better and deeper when you work only with code. - Metome pm