How can I style Blazor Components differently
Say I have Component <MyText />
. How can I style it differently based on different places it is implemented?
For instance, this is what I would want to do (using bootstrap classes for ease):
MyDocument.razor
<div> This text below will be red.</div>
<MyText class="text-danger" />
<div> This text below will be blue.</div>
<MyText class="text-info" />
1 answer
-
answered 2020-11-25 05:49
Brian Parker
Take a look at Attribute splatting and arbitrary parameters
MyText.razor
<div @attributes="AdditionalAttributes">Some Cool Text</div> @code { [Parameter(CaptureUnmatchedValues = true)] public Dictionary<string, object> AdditionalAttributes { get; set; } }
MyDocument.razor
<div> This text below will be red.</div> <MyText class="text-danger" /> <div> This text below will be blue.</div> <MyText class="text-info" />
See also questions close to this topic
-
Insert New Record to the tblPurchase Table Update tblProductStockTable Quantity
I have table called tblPurchase and tblProductStock. When I insert new record to the tblPurchase table I want to update my tblProductStock table. `
Alter TRIGGER [dbo].[trig_passQuantity_toProductStock] ON [dbo].[tblPurchase] AFTER INSERT AS BEGIN SELECT MAX(ItemName) ItemName, ItemCode, SUM(Quantity) Quantity FROM tblPurchase GROUP BY itemCode; UPDATE tblProductStock SET Quantity = tblProductStock.Quantity FROM tblPurchase WHERE tblProductStock.ItemCode = tblPurchase.ItemCode INSERT INTO tblProductStock ( ItemName, ItemCode, Quantity )`enter code here` SELECT ItemName, ItemCode, Quantity FROM tblPurchase WHERE ItemCode NOT IN (SELECT ItemCode FROM tblProductStock) END
I used above trigger to update and inset data. insert is working as expected. but Update query is not working. This is my both tables
In here I add Dell Laptop twice. But In my tblProductStock table did not change to 110. still showing 50. How can I edit this Update query.
-
Is my way of using MongoDB C# Driver FindAsync flawed?
Still kind of new to C# etc.. My Code gives me the results I want, but I'am uncertain if I did implemented it in an "okay" way. Please elaborate if you feel like answering. My goal was to search for entries in my MongoDB via FindAsync. Here my Code for that:
public async Task<List<T>> SearchDocAsync<T, U>(string collection, string findFieldName, U findValue) { var _collection = db.GetCollection<T>(collection); var filter = Builders<T>.Filter.Eq(findFieldName, findValue); var result = await (_collection.FindAsync(filter).Result.ToListAsync()); return result; }
and here the way I call it in Winforms:
private async void BtnReadAll_Click(object sender, EventArgs e) { var asyncResults = await myMongoAux.SearchDocAsync<MyTModel, string>(myCollectionName, "MyDBString", "TestString"); foreach (var r in asyncResults) { RtxbxResult.Text += $"{r.MyDBInt}\t{r.MyDBString}\n"; } }
Is this safe? Shouldn't I be using a (using ) (but how)? And I have read something about that you should use
ConfigureAwait(false)
How and why?
Be so kind and help me out understand more. Thank you for your time :)
-
C# Universal coloured console ouput
Usually git-bash on Windows uses the mintty terminal emulator where the colours cannot be controlled in the same way as in a standard windows console (which appears when running cmd.exe) or in the Windows Terminal.
Is there a way to tell if mintty is being used so I can fall back to Ansi sequences rather than this kind of code:
Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("Red"); Console.ForegroundColor = ConsoleColor.Blue; Console.WriteLine("Blue"); Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("Green");
Thanks
-
How can I make a website/page containing n number of bubble? (each bubble containing random website/page links)
How can I make a website/page containing n number of bubble? (each bubble containing random website/page links) Like there are 1000 bubbles but only 100 are showing on screen and they are randomly moving to any direction. Bubbles are coming from any direction on screen and same way they are go to any direction from screen to disappear. If we touch/click on one bubble, it will redirect another website/page. While colliding with each bubble move opposite direction.
-
I am unable to get the animation when i scroll to next section
Whenever I am going to next section the animation I am unable to see, it is happening whenever the page is loading, please help me getting animation when scrolled to next section..
Website link: http://demo.lamppostmedia.in/arklan-dev/
-
Style css using jquery in odoo 14
I need your help in finding a method to use jquery to style css in odoo 14. I not asking how to add xpath to .js file, I already have this. I am asking how to style css using jquery in odoo. Can I? or it cant be ? Because I did a lot of researches and it was zero result Thanks in advance
-
Having issues changing a string in my react component on a state update
I have a string that represents my file name which I will be passing to the react-csv CSVLink<> component. I initially define the string as something like "my-data.csv", and am trying to update it with the appropriate data I receive from an axios request. It seems I don't understand the true flow of how these react components work and when data is available. I have the following at the top of my file before the component is rendered.
let fileName = "my-data.csv"; ... const setFileName = (tabName?) => { if (!data || !data?.moreData) { // I thought this would be a way to check if the data has been retrieved yet return "equipment-data.csv"; } return tabname != "Average Data" ? "Equipment " + tabname + " " + `${data.dates[0].date}` + ".csv" // format it with data name + date : tabname + `${data.dates[0].date}` + ".csv"; // Same as above, this just results in a different file name };
Further along in my component where it is actually rendered/returned, I try to set it like so:
<CSVLink fileName={setFileName()} data={testData} />
However, this isn't grabbing all the data to format the file correctly. It returns something like "Equipment undefined 01-18-2021.csv", so it's not grabbing everything.
Initially, I simply tried updating the "filename" string within the setFileName() function itself. Something like this:
const setFileName = (tabName?) => { if (!data || !data?.moreData) { // I thought this would be a way to check if the data has been retrieved yet fileName = "equipment-data.csv"; } return tabname != "Average Data" ? fileName = "Equipment " + tabname + " " + `${data.dates[0].date}` + ".csv" // format it with data name + date : fileName = tabname + `${data.dates[0].date}` + ".csv"; // Same as above, this just results in a different file name };
But this resulted in the file name never actually being changed, but rather stuck with the placeholder value I give it in the first place. Any answers and explanations as to why this isn't working would be a lifesaver :)
-
React Native:How to change color for all component in app
In my project, all the elements share the same default color, now my project has a custom color customization option. How can I change my colors's all of the above ingredients without having to go into each one. I'm using react hooks
-
Angular listen to function that returns some variable
So I have a function in my service that's returning a value and I want my component to be listening to the value and update it when it changes so I thought It would be a good idea to subscribe to it but it doesn't work as intended and doesn't update the value when it changes. Is there a different way to do this?
ngOnInit(): void { this.translateService.onLangChange.subscribe(() => this.getLocaleDateFormat()); } getLocaleDateFormat(): Observable<string> { this.dateAdapter.setLocale(Languages[this.translateService.currentLang]); this.currLang = Languages[this.translateService.currentLang]; console.log( 'get' + this.currLang); return of (this.currLang); } ngOnInit(): void { this.localeDateAdapterService.getLocaleDateFormat().subscribe(currLang => this.currLang = currLang); }
I don't need to know when it changes I just the value to update when it changes. Without using a timer and also without using OnLangChange as a trigger:
ngOnInit(): void { this.translateService.onLangChange.subscribe(() => this.localeDateAdapterService.getLocaleDateFormat().subscribe(currLang => this.currLang = currLang)); }
-
accessing httpclient service from within another service in .net 5
I am adding a httpclient as a service (within ConfigureServices(IServiceCollection services)) as follows:
services.AddHttpClient<RequestsClient>(c => { c.DefaultRequestHeaders.Add("User-Agent", "HttpClientFactory"); }) .ConfigurePrimaryHttpMessageHandler(() => { return new HttpClientHandler() { UseDefaultCredentials = true }; });
the RequestsClient class is constructed as:
public RequestsClient (HttpClient client, IHttpContextAccessor hca, ILogger<RequestsClient > log, Configuration config)
to use the RequestsClient service in any class/component needing it i'm injecting it as:
[Inject] protected RequestsClient requestsClient { get; set; }
all this works great.
I'm now in need of creating a second service, lets call it "TimedService". How can I use my RequestsClient service from within my second service, TimedService?
injecting it like i do with components won't work as the RequestsClient always is null. is there a way to give TimedService service access to my RequestsClient service?
I'm sorry if this is a stupid question, I'm fairly new to this
-
Bind Collections using IDbContextFactory
I am about to create a blazor application with ef core. I read the Microsoft recommentation about how to Setup EF Core DbContextFactory
Until now i used in my Razor-Components bindindings to the DbSet like
<select @bind-value="dbContext.People" />
since I read the Article I'm a little confused of how to used it now, because they use the dbcontext inside of a using-Statement.
because of the using statement i cant directly bind a dbset anymore???
@inject IDbContextFactory<DatabaseContext> DbFactory <select @bind-value="dbContext.People" /> @code { DatabaseContext dbContext; protected override Task OnInitializedAsync() { using var dbContext = DbFactory.CreateDbContext(); } }
Whats your suggestion for that?
If i use
<input @bind-value="People" /> @code { ICollection<Person> People; protected override Task OnInitializedAsync() { using var dbContext = DbFactory.CreateDbContext(); People = dbContext.People; } }
nothing will be displayed. The Debugger tells me:
Cannot access a disposed context instance. A common cause of this error is disposing a context instance that was resolved from dependency injection and then later trying to use the same context instance elsewhere in your application. This may occur if you are calling 'Dispose' on the context instance, or wrapping it in a using statement. If you are using dependency injection, you should let the dependency injection container take care of disposing context instances. Object name: 'DatabaseContext'.
-
Blazor server side performance issues
Its a Blazor server side project with about 100 .razor pages, When a change occurs on one of the pages, it takes about 19 seconds to rebuild pages, restart IIS Express and refresh the page. It's awful, I make only a minor change in html, but it takes long time to show the results. The test result is like this :
With 100 razor pages and all css and js references
build : 10 sec
refresh page : 19 sec (include 10 secs for build)When we remove 90 pages of 100 pages (10 pages remains) :
build : 3 sec
refresh page : 12 sec (include 3 secs for build)When we remove all css and js references :
build : 3 sec
refresh page : 6 sec (include 3 secs for build)It is not good at all, because the project is growing and finally we will have about 400 pages! and extra css and js references will added. In this case, the time will be much longer for developing.
what's the solution? Thanks