I have the following aspx code:
<% foreach (ModelDefect modelDefect in GetCodes())
{%>
<tr>
<td><input disabled="disabled" name="<%= modelDefect.Code %>" value="<%= modelDefect.Code %>" type="checkbox" checked="<%=modelDefect.CompletedDate.HasValue? "checked":string.Empty %>" /></td>
<td><%= modelDefect.Code %></td>
<td><%= modelDefect.Description %></td><td><%= modelDefect.AddedDate %></td>
</tr>
<% } %>
I want the check box to be checked if there is a completed date, and unchecked if not. W3 doesn't appear to say how to leave it unchecked but include the attribute. I don't see a way to conditionally include that attribute on the item. I found a page that suggests that checked="yes" or checked="no" should work but it hasn't. I'd like a browser-independent and standards based solution or... a clean way to conditionally add that tag server side on my asp that is Ajax friendly? (Response.Write doesn't work in Asp.net AJax as I understand it.)
Write a helper method which can be unit tested and reused:
public static string GetCheckedAttribute(DateTime? value)
{
return value.HasValue ? "checked=\"checked\"" : string.Empty;
}
and call it in your page:
<input disabled="disabled"
name="<%= modelDefect.Code %>"
value="<%= modelDefect.Code %>"
type="checkbox"
<%= GetCheckedAttribute(modelDefect.CompletedDate) %>
/>
Take the attribute and put it as a part of the conditional output, this should work for you
<input disabled="disabled"
name="<%= modelDefect.Code %>"
value="<%= modelDefect.Code %>"
type="checkbox"
<%=modelDefect.CompletedDate.HasValue? "checked=checked":string.Empty %>"
/>
Related
I am creating an admin page for my website, and I added a button with which I can unlocked locked users (who failed to provide a valid password for 10 times).
I want to disable this button if the user is not locked. I am using a strongly typed Repeater control for that:
<asp:Repeater ItemType="Xamlalo.Pages.Admin.UserDetails" SelectMethod="GetUsers" runat="server">
<ItemTemplate>
<tr>
<td><%# Item.Name %></td>
<td><%# Item.Roles %></td>
<td><%# Item.Locked %></td>
<td><%# Item.Online %></td>
<td><button type="submit" name="unlock" value="<%# Item.Name %>" <%# Item.Locked ? "disabled" : "" %>>Feloldás</button></td>
<td><button type="submit" name="delete" value="<%# Item.Name %>">Törlés</button></td>
</tr>
</ItemTemplate>
</asp:Repeater>
The problem is that the the ternary expression doesn't bind, and I won't have the "disabled" attribute on my button. If Item.Locked evaluates to true, I want to add the disabled attribute to the button. I've tried with <%= (bool)Eval("Locked") ? "disabled" : "" >, but to no avail. I suspect the problem is that with <%# directive, I can only bind the value/properties of Item, not arbitrary string values.
<button type="submit" name="unlock" value="<%# Item.Name %>" <%# Item.Locked ? "disabled" : "" %>>Feloldás</button>
I'm trying to figure out how to use a temporary variable (created from a loop) to show/hide a button.
I have tabular data and some action buttons to the side of them. I need to be able to turn buttons off and on based on the state of that data.
f.IsFoo is a boolean
<table stuff here>
<% foreach (Foo f in listOfFoos) { %>
<tr>
<td>
<%= Fubar(f)%>
</td>
<td>
<%= Fubar1(f) %>
</td>
<td>
<%= Fubar2(f)%>
</td>
<td>
<%= Fubar3(f)%>
</td>
<td>
<%= Fuba4(f)%>
</td>
<td>
<%= Fubar5(f)%>
</td>
<td>
<asp:Button Text="Load" runat="server" OnClick="FooBar" Visible='<%= f.IsFoo%>'/>
</td>
</tr>
<%}%>
I'm pretty new to aspx and it's syntax and online searches have just confused me more.
Personally I tend to put logic like this in the code behind file - however I believe the code below should work,
I added an ID to your button - and I also removed the visibility property - I'm then referencing the button, by its ID - and setting its visibility equal to that of the f.IsFoo property
Possibly try this here:
<% btnFoo.Visible = f.IsFoo; %>
<asp:Button ID="btnFoo" Text="Load" runat="server" OnClick="FooBar" />
I have an MVC user control that displays radio buttons in my MVC app. The issue is that how do I get it to display unique q group name for each control. I need to somehow pass it a parameter to that the name is not set as the same as every other group of radio buttons that I have used the control for on the page.
If this were not MVC I would know how to do this straight away.
<%# Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
<td><input type="radio" name="need" value="4"/></td>
<td><input type="radio" name="need" value="3"/></td>
<td><input type="radio" name="need" value="2"/></td>
<td><input type="radio" name="need" value="1"/></td>
<td><input type="radio" name="current" value="4"/></td>
<td><input type="radio" name="current" value="3"/></td>
<td><input type="radio" name="current" value="2"/></td>
<td><input type="radio" name="current" value="1"/></td>
Thanks
Andy
If you pass some model data to the user control, then you will be able to use this to group your radio buttons.
Change the top line to declare the model type you are passing, such as:
<%# Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<Group>" %>
Where the class 'Group' is a class of your making containing the group's name.
e.g.
public class Group
{
public string Name { get; set; };
}
Then have your user control render the following:
<td><input type="radio" name="<%: Model.Name %>" value="4"/></td>
<td><input type="radio" name="<%: Model.Name %>" value="3"/></td>
<td><input type="radio" name="<%: Model.Name %>" value="2"/></td>
<td><input type="radio" name="<%: Model.Name %>" value="1"/></td>
You can then show this user control using the RenderPartial HTML helper from it's parent multiple times based on the group name that you require.
<% Html.RenderPartial("PartialControlName", new Group { Name = "need" }); %>
In MY MVC Application I used input Type Textbox
and i need to assign that value to the Session how?
Im using code like
<input type="text" id="textbox1" name="namebox" />
<input type="text" id="textbox2" name="agebox" />
<% HttpContext.Current.Session["Name"] =textbox1; %>
<% HttpContext.Current.Session["Age"] = textbox2; %>
But i got error pls help on this....
Why would you prefer Session instead of hidden input box? Well, I think you should use name rather than id of your textbox.
Try this and let me know if there is problem.
<input type="text" id="textbox1" name="namebox" />
<input type="text" id="textbox2" name="agebox" />
<% HttpContext.Current.Session["Name"] = namebox; %>
<% HttpContext.Current.Session["Age"] = agebox; %>
have you tryed this:
<input type="text" id="textbox1" name="namebox" />
<input type="text" id="textbox2" name="agebox" />
<% HttpContext.Current.Session["Name"] = textbox1.value; %>
<% HttpContext.Current.Session["Age"] = textbox2.value; %>
Edit: try
<% HttpContext.Current.Session["Name"] =Request.Form["textbox1"]; %>
Try this:
<input type="text" value="#Session["Name"].ToString()"/>
At the moment I have something like this
<asp:Repeater ID="rptEventsList" DataSourceID="srcQuestionList" runat="server">
<ItemTemplate>
<td><span><%# Eval("orderBy").ToString()%>)</span></td>
<td><%# Eval("question").ToString()%></td>
<td><asp:RadioButton ID="RadioButton1" runat="server" /></td>
<td><input name="question<%# Eval("orderBy").ToString()%>" type="radio" id="True" value="True" class="styled" /></td>
<td><input name="question<%# Eval("orderBy").ToString()%>" type="radio" id="False" value="False" class="styled" /></td>
</tr>
</ItemTemplate>
</asp:Repeater>
And in the code behind I capture the values as Request.Form("question1") for example and this all works fine.
Now I am wondering how to add validation to this, I think I have to apply changes to a RadioButton control but I can't see how I could add my dynamic RadioButton names in there with my id from stored procedure.
Also I would like to add the validation to the top of the screen as a validation summary.
Look into Validation Server Controls.