Disabled option in button is not being changed dynamically - c#

I am trying to set the disabled property in a button based on a value i am setting OnInitialized. But the button remains disabled even when EnableRegistration is set to true OnInitialized. How to do toggle the disabled property based on the EnableRegistration?
#page "/course/register"
<h3 class="my-5">Course Registration</h3>
<div class="container">
<div class="alert alert-info">
<p class="fw-bold">People who register for full course will be given priority over individual module registrations.
After registration, look for an email from us to see if you got in and instructions to pay the course fee.
Registrations will open at 6:00 PM on March 20th.
</p>
</div>
<div class=row>
<div class="col-6">
<button onclick="Register" id="fullcourse" disabled="#(EnableRegistration == true ? "false": "true")" class="btn btn-primary">Register for Full Course</button>
</div>
<div class="col-6">
<button onclick="Register" id="fullcourse" disabled="#(EnableRegistration == true ? "false": "true")" class="btn btn-secondary">Register for First Module</button>
</div><br/>
#if (!string.IsNullOrEmpty(Message))
{
<p>#Message</p>
}
</div>
</div>
#code {
public bool EnableRegistration;
public bool Registered = false;
public string Message = "";
protected override void OnInitialized()
{
var registrationDateTime = new DateTime(2022, 3, 15, 6, 33, 0);
if(Registered == false && (DateTime.Compare(DateTime.Now, registrationDateTime) == 1 || DateTime.Compare(DateTime.Now, registrationDateTime) == 0))
{
EnableRegistration = true;
}
}
public void Register()
{
Message = "Registered";
}
}
Thanks.

Replace
disabled="#(EnableRegistration == true ? "false": "true")"
with
disabled="#(!EnableRegistration)"
HTML does not support ="true" but Blazor does. In the end result you need either just disabled or an empty string. The razor compiler arranges that for you.

You need
<button id="fullcourse" disabled="#(!EnableRegistration)" class="btn btn-primary">Register for Full Course</button>

Related

How to fix this simple ElementNotFoundException in Blazor?

I am unit testing a blazor app. I get a ElementNotFoundException. I think the cause for this is an if statement in the the index.razor page. see code below:
<div class="row">
<div class="col-12">
#if ((challenges != null) && (challenges.Count > 0))
{
<MultiStepComponent Id="MultiStepContainer" Challenges="#challenges">
<div class="row p-3">
<div class="col-6" id="challengeContainer">
#foreach(var c in challenges)
{
<MultiStepNavigation Name="#c.Title">
<h1>#c.Title</h1>
<img class="float-left" src="#c.ImagePath" width="200" />
#foreach(var sentence in c.Description)
{
<p>#sentence</p>
}
</MultiStepNavigation>
}
</div>
<div class="col-6">
<textarea rows="26" cols="120" #bind="input" id="input"></textarea>
<button class="btn" id="runBtn" #onclick="RunAsync">Run</button>
<br />
<textarea rows="10" cols="120" id="output" readonly>#((MarkupString)Output)</textarea>
</div>
</div>
</MultiStepComponent>
}
</div>
</div>
The code behind of this page (index.razor.cs) has the following initialization code:
protected override async Task OnInitializedAsync()
{
jsonRepository = new JSONChallengeRepository();
challenges = await jsonRepository.GetChallengesAsync();
}
The test for this page is here:
[Test]
public async Task Compile_code_Success()
{
_codingChallengeService.Setup(c => c.SendInputToCompilerAsync("50+50")).ReturnsAsync("100");
_testContext.Services.AddScoped(x => _codingChallengeService.Object);
var razorComponent = _testContext.RenderComponent<Index>();
razorComponent.Instance.challenges = GetChallenges();
if ((razorComponent.Instance.challenges != null) && (razorComponent.Instance.challenges.Count > 0))
{
var runBtn = razorComponent.FindAll("button").FirstOrDefault(b => b.OuterHtml.Contains("Run"));
var input = razorComponent.Find("#input");
input.Change("50+50");
runBtn.Click();
var outputArea = razorComponent.Find("#output");
var outputAreaText = outputArea.TextContent;
Assert.AreEqual("100", outputAreaText);
}
Assert.IsNotNull(razorComponent.Instance.challenges);
}
The #input is missing..Why??
Thanks in advance!
I am guessing the problem is that you do not cause the component under test to re-render when you assign razorComponent.Instance.challenges property/field, and if the component does not re-render, then the markup inside #if ((challenges != null) && (challenges.Count > 0)) block in the component is not displayed.
In general, dont mutate properties (parameters) of components through the razorComponent.Instance. If you really have to do so, make sure to trigger a render after.
Instead, pass parameters to the component through the RenderComponent or SetParametersAndRender methods, or through services injected into components. That will cause the component to go through its normal render life-cycle methods.

Put a line of string in textarea everytime I click a checkbox

Basically I want my code to update the textarea as users put a check in a checkboxes inside a table. If a checkbox is checked, a username will be placed in textarea along with line breaks. If unchecked, it will remove from textarea. After that, a button will submit every string inside the textarea.
#using (Html.BeginForm())
{
#Html.AntiForgeryToken();
<td align="center">
<form>
<div style="max-width:50%" class="form-group #if (ViewBag.ErrorMessageDelete != null)
{ <text>has-error</text> } ">
#Html.TextArea("deleteRequest", string.Empty, 5, 100, null)
#if (ViewBag.ErrorMessageDelete != null)
{
<span class="help-block">#ViewBag.ErrorMessageDelete</span>
}
</div>
<button class="btn btn-primary" onclick="return confirm ('Removing these members, are you sure?')">Remove User(s)</button>
</form>
</td>
}
and this is my current checkbox
<td align="center" style="width:5%">
#Html.CheckBox("toBeDeleted", new { onclick = "deleteRequest = deleteRequest + item.username + <br>" });
</td>
I used textarea because I want users to be able to input usernames on their own without using checkboxes. Is it possible to do it in MVC ASP.NET in Visual Studio?
Don't do this with click event. Use change event on checkbox.
You can also try with the array. If the checkbox is checked add item to array, if not remove it. After that convert array to string, and set a value to textarea. Delete and push logic you have to implement by your own.
<td align="center" style="width:5%">
#Html.CheckBox("toBeDeleted", new { onchange="testfunc(this)", data_username = item.username });
</td>
<script>
var result = [];
function testfunc(elem) {
var username = $(elem).attr("data-username");
if($(elem).is(':checked')) {
result.push(username )
}
else{
var index = result.indexOf(username);
if (index > -1) {
result.splice(index, 1);
}
}
$("#deleteRequest").val(result.join("\n"));
}
</script>

C#: HttpCookie Content Setted in Index Page and then desapeared on all over the WebSite

I'm facing an issue using HttpCookie.
I'm building an Ecommerce and I'm trying to set Cookies for my Website.
There are three Cookies defined as describe bellow:
private static string cookienameCampanha = "WLC-Ecommerce-Campanha";
private static string cookienameUsuario = "WLC-Ecommerce-Usuario";
private static string cookienameRodape = "WLC-Ecommerce-Rodape";
All of then is used when HttpCookie is instantiated as bellow:
HttpCookie cookiehelper = new HttpCookie(cookienameCampanha);
HttpCookie cookiehelper = new HttpCookie(cookienameUsuario);
HttpCookie cookiehelper = new HttpCookie(cookienameRodape);
When user access index page for my Ecommerce, all those Cookies were setted. And they have specific definitions.
The third one, cookienameRodape, is used for setting content in footer and my web site shows those contents.
But after index page got fully reload and my footer listed all content, My Web Browser shows that all cookienameRodape content was erased and when I redirect the page to any other pages inside my site, all content is empty.
Something is weird, in DevTools using Chrome I can see other two Cookies Content setted and not erased, but "WLC-Ecommerce-Rodape" is not there even all content is listed in index page.
See the print below:
We can check My Index page and all content in my footer:
But when I try to navegate through my webSite, suddenly all content desapeared:
Additionaly, When always returned to index page, all Cookies are setted again and then all content inside footer are listed.
I tryed to search all over the SO and none of then suited me.
All Cookies are removed when user logged out. There is no any moment this happend.
After All content came from DataBase, Footer Contents were defined as below:
public static class AuxiliarCookieBLL
{
public static List<CampanhaInstitucionalModel> GetCampanhaInstitucional()
{
CookieHelper cookiehelper = new CookieHelper(cookienameRodape);
if (!String.IsNullOrEmpty(cookiehelper.GetFromCookie("CampanhaInstitucional")))
{
List<CampanhaInstitucionalModel> CampanhaInstitucional = JsonConvert.DeserializeObject<List<CampanhaInstitucionalModel>>(HttpUtility.UrlDecode(cookiehelper.GetFromCookie("CampanhaInstitucional")));
return CampanhaInstitucional;
}
else
return new List<CampanhaInstitucionalModel>();
}
public static void SetCampanhaInstitucional(List<CampanhaInstitucionalModel> CampanhaInstitucional)
{
CookieHelper cookiehelper = new CookieHelper(cookienameRodape);
if (CampanhaInstitucional != null)
{
string campanha = JsonConvert.SerializeObject(CampanhaInstitucional);
cookiehelper.AddToCookie("CampanhaInstitucional", HttpUtility.UrlEncode(campanha));
}
}
public static List<CampanhaAtendimentoModel> GetCampanhaAtendimento()
{
CookieHelper cookiehelper = new CookieHelper(cookienameRodape);
if (!String.IsNullOrEmpty(cookiehelper.GetFromCookie("CampanhaAtendimento")))
{
List<CampanhaAtendimentoModel> CampanhaAtendimento = JsonConvert.DeserializeObject<List<CampanhaAtendimentoModel>>(HttpUtility.UrlDecode(cookiehelper.GetFromCookie("CampanhaAtendimento")));
return CampanhaAtendimento;
}
else
return new List<CampanhaAtendimentoModel>();
}
public static void SetCampanhaAtendimento(List<CampanhaAtendimentoModel> CampanhaAtendimento)
{
CookieHelper cookiehelper = new CookieHelper(cookienameRodape);
if (CampanhaAtendimento != null)
{
string campanha = JsonConvert.SerializeObject(CampanhaAtendimento);
cookiehelper.AddToCookie("CampanhaAtendimento", HttpUtility.UrlEncode(campanha));
}
}
public static List<CampanhaCentralAtendimentoModel> GetCampanhaCentralAtendimento()
{
CookieHelper cookiehelper = new CookieHelper(cookienameRodape);
if (!String.IsNullOrEmpty(cookiehelper.GetFromCookie("CampanhaCentralAtendimento")))
{
List<CampanhaCentralAtendimentoModel> CampanhaCentralAtendimento = JsonConvert.DeserializeObject<List<CampanhaCentralAtendimentoModel>>(HttpUtility.UrlDecode(cookiehelper.GetFromCookie("CampanhaCentralAtendimento")));
return CampanhaCentralAtendimento;
}
else
return new List<CampanhaCentralAtendimentoModel>();
}
public static void SetCampanhaCentralAtendimento(List<CampanhaCentralAtendimentoModel> CampanhaCentralAtendimento)
{
CookieHelper cookiehelper = new CookieHelper(cookienameRodape);
if (CampanhaCentralAtendimento != null)
{
string campanha = JsonConvert.SerializeObject(CampanhaCentralAtendimento);
cookiehelper.AddToCookie("CampanhaCentralAtendimento", HttpUtility.UrlEncode(campanha));
}
}
public static List<CampanhaCertificadoModel> GetCampanhaCertificado()
{
CookieHelper cookiehelper = new CookieHelper(cookienameRodape);
if (!String.IsNullOrEmpty(cookiehelper.GetFromCookie("CampanhaCertificado")))
{
List<CampanhaCertificadoModel> CampanhaCertificado = JsonConvert.DeserializeObject<List<CampanhaCertificadoModel>>(HttpUtility.UrlDecode(cookiehelper.GetFromCookie("CampanhaCertificado")));
return CampanhaCertificado;
}
else
return new List<CampanhaCertificadoModel>();
}
public static void SetCampanhaCertificado(List<CampanhaCertificadoModel> CampanhaCertificado)
{
CookieHelper cookiehelper = new CookieHelper(cookienameRodape);
if (CampanhaCertificado != null)
{
string campanha = JsonConvert.SerializeObject(CampanhaCertificado);
cookiehelper.AddToCookie("CampanhaCertificado", HttpUtility.UrlEncode(campanha));
}
}
public static List<CampanhaFormaPagamentoModel> GetCampanhaFormaPagamento()
{
CookieHelper cookiehelper = new CookieHelper(cookienameRodape);
if (!String.IsNullOrEmpty(cookiehelper.GetFromCookie("CampanhaFormaPagamento")))
{
List<CampanhaFormaPagamentoModel> CampanhaFormaPagamento = JsonConvert.DeserializeObject<List<CampanhaFormaPagamentoModel>>(HttpUtility.UrlDecode(cookiehelper.GetFromCookie("CampanhaFormaPagamento")));
return CampanhaFormaPagamento;
}
else
return new List<CampanhaFormaPagamentoModel>();
}
public static void SetCampanhaFormaPagamento(List<CampanhaFormaPagamentoModel> CampanhaFormaPagamento)
{
CookieHelper cookiehelper = new CookieHelper(cookienameRodape);
if (CampanhaFormaPagamento != null)
{
string campanha = JsonConvert.SerializeObject(CampanhaFormaPagamento);
cookiehelper.AddToCookie("CampanhaFormaPagamento", HttpUtility.UrlEncode(campanha));
}
}
}
And now I can show my script footer page when I call all content and list then:
<div class="limite-layout">
<nav>
<ul class="col-xs-12 no-padding" id="navFooter">
<li class="col-md-2 institucional">
<div>
#{
rodape.Rodape.ListInstitucional = BLL.AuxiliarCookieBLL.GetCampanhaInstitucional();
if (rodape.Rodape.ListInstitucional.Count > 0)
{
<h6><i class="icon icon-right visible-xs-inline-block visible-sm-inline-block"></i>Institucional</h6>
<ul id="Institucional" class="collapse">
#foreach (var item in rodape.Rodape.ListInstitucional)
{
#*<li>#item.Nome</li>*#
<li>#item.Nome</li>
}
</ul>
}
}
</div>
</li>
<li class="col-md-2 atendimento">
<div>
#{
rodape.Rodape.ListAtendimento = BLL.AuxiliarCookieBLL.GetCampanhaAtendimento();
if (rodape.Rodape.ListAtendimento.Count > 0)
{
<h6><i class="icon icon-right visible-xs-inline-block visible-sm-inline-block"></i>Atendimento</h6>
<ul id="Atendimento" class="collapse">
#foreach (var item2 in rodape.Rodape.ListAtendimento)
{
<li>#item2.Nome</li>
}
</ul>
}
}
</div>
</li>
<li class="col-md-2 central-atendimento">
<div>
#{
rodape.Rodape.ListCentralAtendimento = BLL.AuxiliarCookieBLL.GetCampanhaCentralAtendimento();
if (rodape.Rodape.ListCentralAtendimento.Count > 0)
{
<h6><i class="icon icon-right visible-xs-inline-block visible-sm-inline-block"></i>Central de Atendimento</h6>
<ul id="CentralAtendimento" class="collapse">
#foreach (var item3 in rodape.Rodape.ListCentralAtendimento)
{
<li>#item3.Nome</li>
}
</ul>
}
}
</div>
</li>
<li class="col-md-3">
<div class="certificados">
#{
rodape.Rodape.ListCertificado = BLL.AuxiliarCookieBLL.GetCampanhaCertificado();
if (rodape.Rodape.ListCertificado.Count > 0)
{
<h6><i class="icon icon-right visible-xs-inline-block visible-sm-inline-block"></i>Certificados</h6>
<ul id="Certificados" class="collapse list-horizontal">
#foreach (var item5 in rodape.Rodape.ListCertificado)
{
#*<li><img alt="#item5.Nome" src="#item5.Descricao" /></li>*#
<li><img alt="#item5.Nome" src="~/Content/img/conteudo/certificados/ligodaddy.png" /></li>
<li><img alt="#item5.Nome" src="~/Content/img/conteudo/certificados/clearsale_logo.jpg" /></li>
<li><img alt="#item5.Nome" src="~/Content/img/conteudo/certificados/ABComm.png" /></li>
}
</ul>
}
}
</div>
<div class="pagamentos">
#{
rodape.Rodape.ListFormaPagamento = BLL.AuxiliarCookieBLL.GetCampanhaFormaPagamento();
if (rodape.Rodape.ListFormaPagamento.Count > 0)
{
<h6><i class="icon icon-right visible-xs-inline-block visible-sm-inline-block"></i>Formas de Pagamento</h6>
<ul id="FormaPagamento" class="collapse">
#foreach (var item4 in rodape.Rodape.ListFormaPagamento)
{
#*<li><img alt="Formas de Pagamento" src="#item4.URL" /></li>*#
<li><img alt="Formas de Pagamento" src="#item4.URL" /></li>
#*<li><img alt="Formas de Pagamento" src="~/Content/img/conteudo/formas-pagamento/mastercard.jpg" title="Mastercard"/></li>
<li><img alt="Formas de Pagamento" src="~/Content/img/conteudo/formas-pagamento/itau.jpg" title="Itaú"/></li>
<li><img alt="Formas de Pagamento" src="~/Content/img/conteudo/formas-pagamento/elo.jpg" title="Elo"/></li>
<li><img alt="Formas de Pagamento" src="~/Content/img/conteudo/formas-pagamento/boleto.jpg" title="Boleto"/></li>*#
}
</ul>
}
}
</div>
#if(BLL.AuxiliarCookieBLL.GetExibirRegulamento() == true)
{
<div class="footer-social">
<h6><i class="icon icon-right visible-xs-inline-block visible-sm-inline-block"></i>Redes Sociais</h6>
<ul id="Social" class="collapse list-horizontal">
<li>
<i class="icon icon-facebook"></i>
<i class="icon icon-twitter2"></i>
<i class="icon icon-gplus"></i>
</li>
</ul>
<div class="clearfix"></div>
</div>
}
</li>
<li class="col-md-3 ofertas">
<div>
<h6>Receba ofertas exclusivas no seu e-mail</h6>
<div class="form-group col-xs-12 no-padding">
<input type="text" name="Email" id="txtEmailNewsLetter" placeholder="Digite seu e-mail" />
</div>
<div class="form-group col-md-8 input-left">
<input type="text" name="Nome" id="txtNomeNewsLetter" placeholder="Digite seu nome" />
</div>
<div class="form-group col-md-4 no-padding container-btn-enviar text-right">
Enviar
</div>
<p id="erroNewsLetter" class="display-none no-margin-top">Verifique os campos digitados!</p>
<p id="msgOkNewsLetter" class="display-none no-margin-top">E-mail cadastrado.</p>
<p id="msgErroRequest" class="display-none no-margin-top">Erro ao cadastrar. Por favor tente novamente.</p>
<div class="clearfix"></div>
</div>
</li>
</ul>
</nav>
<div class="clearfix"></div>
</div>
EDIT
Here is CookieHelper code content:
There are two constructors and Property
//Constructors
public CookieHelper(String CookieName)
{
myCookieName = CookieName;
}
public CookieHelper(String CookieName, DateTime ExpiresDate)
{
myCookieName = CookieName;
dtExpires = ExpiresDate;
}
//Property
private static HttpCookie myCookie
{
get
{
return HttpContext.Current.Request.Cookies[myCookieName] != null ? HttpContext.Current.Request.Cookies[myCookieName] : NewCookie();
}
set
{
HttpContext.Current.Response.Cookies.Add(value);
}
}
And here is basic methods:
public void AddToCookie(String FieldName, String Value)
{
HttpCookie myHttpCookie = myCookie;
myHttpCookie[FieldName] = Value;
myCookie = myHttpCookie;
}
public void RemoveCookie()
{
HttpCookie myHttpCookie = myCookie;
myHttpCookie.Value = null;
myHttpCookie.Expires = DateTime.Now.Date.AddDays(-1);
myCookie = myHttpCookie;
}
private static HttpCookie NewCookie()
{
HttpCookie newcookie = new HttpCookie(myCookieName);
if(dtExpires != null)
newcookie.Expires = (DateTime)dtExpires;
HttpContext.Current.Response.Cookies.Add(newcookie);
return newcookie;
}
What could be happening?
All content inside this Cookie worked fine, and suddenly all Browsers are not stored this specific one.
Can any one help me with this problem?
If needs any further information, please advise me.
Not sure about your code in CookieHelper but I can't see that you'r setting an expiration or adding the cookie to the response. Those are two very important parts to using cookies so if they are missing, try to do something like this
public static void SetCampanhaInstitucional(List<CampanhaInstitucionalModel> CampanhaInstitucional)
{
if (CampanhaInstitucional != null)
{
string campanha = JsonConvert.SerializeObject(CampanhaInstitucional);
HttpCookie theCookie = new HttpCookie(cookienameRodape, campanha);
theCookie.Expires.AddDays(7); //Keep the cookie alive for 7 days
HttpContext.Response.Cookies.Add(theCookie); //add the cookie to the response
//alternatively you could also use
//HttpContext.Response.SetCookies(theCookie);
}
}
If your having trouble with using HttpContext.Response.Cookies.Add(theCookie); you might need to set the cookie using Response.SetCookie(theCookie) as per This answer to a cookie problem
Also, how much data are you storing?
Cookies are not ment to store alot of data and most browsers support up to 4096 bytes or 4kb. This is however for all cookies on the domain, so if you have 2 cookies using the 4kb, then adding another cookie will result the loss off cookies.
I can see from the screenshot that you are not using all of the available space, but as the Rodape cookie is not present, I have now idea of how big than one might be. Try just adding the Rodape cookie and see if it persists and check the size of it.
There are numerous possible causes for what you're seeing. To find the specific cause for your case, I would start by setting breakpoints in the code in any place where the value of the cookie gets set or reset. By running the code in a debugger, you can see if that code gets called at unexpected times. Repeat the steps to reproduce the problem, taking note of any unexpected calls to the code that change the cookie value. See what this reveals.

Switching Language in C#.NET has no impact to site

I have implemented Multi Language Support into my ASP.NET C# followed by this tutorial and set english to my Default language as can be seen here:
When switching to german nothing happens:
In my App_GlobalResource Folder I have to files:
de.language.resx and en.language.resx
My mls.cs file (In the tutorial named BasePage.cs) contains the following code:
public class mls : System.Web.UI.Page
{
public void setLang() {
InitializeCulture();
}
protected override void InitializeCulture()
{
if (!string.IsNullOrEmpty(Request["lang"]))
{
Session["lang"] = Request["lang"];
}
string lang = Convert.ToString(Session["lang"]);
string culture = string.Empty;
// In case, if you want to set vietnamese as default language, then removing this comment
if (lang.ToLower().CompareTo("en") == 0 || string.IsNullOrEmpty(culture))
{
culture = "en-US";
}
if (lang.ToLower().CompareTo("en") == 0 || string.IsNullOrEmpty(culture))
{
culture = "en-US";
}
if (lang.ToLower().CompareTo("de") == 0)
{
culture = "de-AT";
}
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(culture);
Thread.CurrentThread.CurrentUICulture = new CultureInfo(culture);
base.InitializeCulture();
}
}
And here is my Login.aspx page:
public partial class WebForm3 : mls
{
protected void Page_Load(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(Convert.ToString(Session["lang"])))
{
if (Convert.ToString(Session["lang"]) == "en")
{
lbl_Debug.Text = "lang=en";
Session["lang"] = null;
Session["lang"] = "en";
}
else if(Convert.ToString(Session["lang"]) == "de")
{
lbl_Debug.Text = "lang=de";
Session["lang"] = null;
Session["lang"] = "de";
}
}
else
{
lbl_Debug.Text = "nothing";
}
}
}
Here is my aspx code:
<asp:Content ID="Content2" ContentPlaceHolderID="ph_RowMain" runat="server">
<div class="login-box">
<div class="login-logo">
<a href="Start.aspx"><b>
<asp:Literal ID="lt_adminInterfaceHeader" runat="server" Text="<%$Resources:en.language, lt_adminHeader%>"></asp:Literal></b></a>
</div>
<!-- /.login-logo -->
<div class="login-box-body">
<p class="login-box-msg">
<asp:Literal ID="lt_adminInterfaceBox" runat="server" Text="<%$Resources:en.language, lt_adminBox%>"></asp:Literal>
</p>
<div class="form-group has-feedback">
<asp:TextBox ID="tb_email" runat="server" type="email" class="form-control" placeholder="<%$Resources:en.language,tb_email%>"></asp:TextBox>
<span class="glyphicon glyphicon-envelope form-control-feedback"></span>
</div>
<div class="form-group has-feedback">
<asp:TextBox ID="tb_password" runat="server" type="password" class="form-control" placeholder="<%$Resources:en.language, tb_password%>"></asp:TextBox>
<span class="glyphicon glyphicon-lock form-control-feedback"></span>
</div>
<div class="row">
<div class="col-xs-12">
<asp:Button ID="btn_signIn" runat="server" Text="<%$Resources:en.language, btn_signIn%>" type="submit" class="btn btn-primary btn-block btn-flat" />
</div>
<!-- /.col -->
</div>
</div>
<!-- /.login-box-body -->
</div>
<!-- /.login-box -->
Hope somebody can help.
You have the name of the files wrong. you have de.lanuage.rex but, according to the article you referred, it requires format anyname.language.de.resx.
When you are trying to localize your website, name of the resourse files matters a lot.
your resourse files name should follow the below format
FileNameYouWant.language-culture.resx
in case you do not specify the language-culture i.e if your filename is like this
FileNameyouWant.resx then by default it will take english language(language-culture='en').
example:German language resourse file name=FileNameYouWant.de.resx
French language resourse file name=FileNameYouWant.fr.resx

How To Hide Input Fields Based On Enum Radio Button Selection .NET MVC

I have developed a page in C# using the MVC Razor technique,My code is following
<div class="col-md-4">
<label for="" class="control-label">
IDMS Reference <font color="red">*</font></label><br />
<div class="radio col-md-4">
#Html.EnumRadioButtonFor(m => m.enumIDMSReference, false).DisableIf(() =>
Model.IdmsRef == 2)
</div>
<div class="col-md-4">
<label for="" class="control-label">
IDMS Reference Number <font color="red">*</font></label><br />
#Html.TextBoxFor(m => m.IdmsRefNo, new { #class = "form-control"
}).DisableIf(() => Model.InwardBranch == true)
</div>
And my Enum class is following,
public enum enumIDMSReference
{
[Description("Applicable")]
Applicable = 1,
[Description("Not Applicable")]
NotApplicable = 2,
}
Here if we click the Not Applicable radio button, then the IDMS Reference Number input field should be disable and the input IdmsRefNo field should enable only for the applicable radio button so how to do it in JQuery or any other solution?.
Please anybody help
If I understood your question right, you want to disable a field based on the selected radio option. You can bind the change event of your radio buttons and disable/enable your textbox accordingly. [.NET FIDDLE]
$('input[name="enumIDMSReference"]').change(function() {
if ($(this).is(':checked') && $(this).val() == 'NotApplicable') {
$('#IdmsRefNo').attr('disabled', 'disabled');
} else {
$('#IdmsRefNo').removeAttr('disabled');
}
});
You can iterate across your enum values like so (I'm not checking for nulls, which you can do on your own):
#foreach (var value in Enum.GetValues(Model.enumIDMSReference.GetType()))
{
var memInfo = Model.enumIDMSReference.GetType().GetMember(value.ToString());
var attributes = memInfo[0].GetCustomAttributes(typeof(DescriptionAttribute), false);
var description = ((DescriptionAttribute)attributes[0]).Description;
#Html.RadioButtonFor(m => m.enumIDMSReference, value);
#Html.Label(description);
#Html.Raw("<br />");
}
You can expand this further by disabling/enabling the textbox by default server-side depending on the value of enumIDMSReference.

Categories