Skip to main content

Command Palette

Search for a command to run...

Day 2: Blazor Components & Razor Syntax

What is a Component?

Updated
1 min read
  • Components are reusable UI pieces (like buttons, forms, or entire pages).

  • Defined in .razor files (e.g., MyComponent.razor).

  • Combine HTML + C# logic using Razor syntax.

Razor Syntax Basics

Create a Simple Component

Let’s create a component that toggles text visibility.

Data Binding

Event Handling

Respond to user actions (clicks, input changes, etc.).

EventExample
Button Click@onclick="MethodName"
Input Change@oninput="MethodName"
Form SubmissionOnValidSubmit="HandleSubmit"

Practice Task

Create a "Like Button" Component:

  1. Show a button with a thumbs-up icon (👍).

  2. Display the number of likes.

  3. When clicked, increment the likes count.

Common Issues (For Beginners)

  • Missing @code Block: Ensure logic is inside @code { ... }.

  • Incorrect Event Names: Use @onclick, not onclick.

  • Case Sensitivity: @bind is lowercase, not @Bind.

Key Takeaways

  • Components are reusable and can have their own logic.

  • Razor Syntax lets you mix HTML and C# with @.

  • Data Binding keeps UI and variables in sync.

  • Events like @onclick let you handle user interactions.

More from this blog

আপনার প্রোডাক্টিভিটি বাড়ান ১০ গুণ!

৫০টি ফ্রি AI টুল — এখন কাজ হবে ১০ গুণ দ্রুত ও স্মার্টভাবে! 1. ChatGPT – যেকোনো লেখা বা প্রশ্নের উত্তর দিতে পারে।2. Canva AI – ডিজাইন তৈরি ও কনটেন্ট সাজাতে AI সহায়তা।3. Pictory – লেখা থেকে অটো ভিডিও তৈরি করে।4. Copy. ai – মার্কেটিং কপিরাইটিং বা ব্ল...

Apr 28, 20253 min read

Abdullah’s Tech Haven

14 posts

Discover simplified insights, tips, and tutorials on .NET Core, Blazor, Entity Framework Core, SQL, and C# at Abdullah’s Tech Haven—a practical hub for tech enthusiasts and developers!