HTML Radio button inside ASP.NET repeater C# - html

I am trying to simply bind a HTML Input radio button inside a repeater :
<asp:Repeater ID="Outlets" runat="server" >
<ItemTemplate>
<input type="radio" name="Proposal" value="Test1" id="Radio1" checked="
<%#`GetSelectedValue(DataBinder.Eval(Container.DataItem,"IsDefaultSelection"))%>" />`
<label for="Test1">
<%#DataBinder.Eval(Container.DataItem, "Label")%>
</label>
</ItemTemplate>
</asp:Repeater>
The GetSelectedValue method simply returns a string "checked" for the radio button to be selected, lets say the repeater had 3 items to loop and when the markup renders and If I inspect the element I see the correct value for the first radio button i.e. checked="checked" but on the UI the 3rd radio button gets selected even though its checked value="false", can someone guide me as to what i am doing wrong?

Give this a try
<%# (bool)Eval("IsDefaultSelection") ? " checked=\"checked\" " : string.Empty %>
Basically the inclusion on the checked attribute is causing the RadioButton to be checked. You want to pull it out so the entire thing is rendered or the entire thing is absent.
Also keep in mind all your RadioButtons are going to be named Test1 It might be a good idea to use a server side RadioButton.

The CHECKED attribute in HTML has no value associated with it. You should only use this attribute if you want to select it.
You want something like:
<asp:Repeater ID="Outlets" runat="server" >
<ItemTemplate>
<input type="radio" name="Proposal" value="Test1" id="Radio1" <%
if (GetSelectedValue(DataBinder.Eval(Container.DataItem,"IsDefaultSelection"))
response.write("CHECKED");
%> />
<label for="Test1">
<%#DataBinder.Eval(Container.DataItem, "Label")%>
</label>
</ItemTemplate>
</asp:Repeater>
Otherwise you could simply use the ASP.NET INPUT controls.

Related

How to bypass required fields when asp button and html text both present

In my case i have both ASP button and html text field which is required and two buttons.
Like this
<input type='text' runat='server' id='textfield' required/>
<asp:Button Text='submit' runat='server' onClick='clickEvent' id='submitButton'/>
<asp:button text='export' runat='server' onClick='exportEvent' id='exportButton'/>
Here when
i click submitButon and textfield empty then it triggers validation
and its good.
But when
i click exportButton that textfield triggers which is unwanted.
Notes:
1) i have tried EnableClientScript="False" and ValidateRequestMode='disabled' both of them didn't work
2) tried giving validation group in exportbutton didn't work and i couldn't apply that on pure html field
ASP Button has UseSubmitBehavior property, you can set that to false to bypass submit validations for the required button like below
<asp:button text='export' runat='server' onClick='exportEvent' id='exportButton' UseSubmitBehavior="false"/>

How to allign dynamic list item in RadioButtonList VB

I have created a radiobuttonlist which items in it are generated dynamically based on my database. And I'm using VB webform.
IN HTML:
<asp:RadioButtonList ID="rdlSession" runat="server" CssClass="rdl">
</asp:RadioButtonList>
VB:
For count = 0 To someitem.Length 'databaseitem
With rdlSession.Items
.Add(someitem)
End With
Next
My question is:
How to align the rows to start from the red line ?
(I have tried using text-allign:left,but the output doesnt affect much, the first line appears after the radiobutton.)
You need css to solve that problem.
RadioButtonList is (if You see Page Source) table with rows where are RadioButtons placed.
For example, 1st item of RadioButtonList in html will be (two "elements" : one is <input type="radio" id="rdlSession_0" ...> and label for that input) :
<input id="rdlSession_0" type="radio" name="rdlSession" value="Session :0<br>Date :0<br>Coach :0<br>Available slot :0" /><label for="rdlSession_0">Session :0<br>Date :0<br>Coach :0<br>Available slot :0</label>
You need to apply some css for that label.
HTML
<asp:RadioButtonList ID="rdlSession" runat="server" CssClass="rdl">
</asp:RadioButtonList>
CSS
.rdl label
{
display:inline-block;
vertical-align:top;
}
If You remove vertical-align:top;, radio button will be aligned at bottom.
I hope so this solution will work for You.

asp Textbox & HTML Input - Values always empty

I have an asp Textbox for a username and a HTML input for a password, these are both server controls and are contained in the master page (in a modal popup).
<input runat="server" Class="login-input" id="txtPassword" placeholder="Password" type="password"
<asp:Textbox style="float:left; margin-right:25px;" runat="server" id="txtUsername" Class="login-input" Placeholder="Username" name="txtUserName"/>
I also have a login button hooked up to a handler in the code behind. However the value of the 2 input boxes is always empty.
I had to include UseSubmitBehavior="False" on my button as this was the only way i could get my click event to
<asp:Button UseSubmitBehavior="False" runat="server" Class="login-button" ID="btnLogin" Text="Login" />
Not sure what's going on here, i have checked my code and definitely only have 1 form tag, which i heard can cause this.
It turned out that at runtime my modal was not contained within the tags.
Therefore i had to use
$('#divname').parent().appendTo($("form:first"));
within my script to move the elements back within the form tags.
(There were 2 modal elements outside) I found this out by using f12 dev tools/firebug.

POST Checkbox value in HTML

I want to post checkbox values to server using html. But my code retrieves nothing. Please help.
<form action="Default2.aspx" method="post" >
<input type="checkbox" name="attempt" value="101"> I'st attempt<br>
<input type="checkbox" name="attempt" value="102" checked> 2nd attempt<br>
<input type="submit" value="Submit">
</form>
They need ID's so they can be referred to and if this is a Web Forms website you need to also have the attribute runat="server" for both of them otherwise you won't be able to access them.
ASP.NET Page already has form element in the root, and you don't have insert it by self. If you use native HTML element in your ASP.NET page or control, you can get their values by using Request Object. See simple example here How-get-input-text-value-server-side-c.aspx
In plain HTML forms, checkbox input controls don't get included in the submission at all if the user left them unchecked. See this question: Does <input type="checkbox" /> only post data if it's checked?

Is their any asp net server control which could render <label> tag?

I want to render a <label> tag. But want to set some of it's properties while it's rendering like for and text value.
Actually my real problem is I want to associate a label with a radio button and this is the code so far I have:
<asp:RadioButton ID="Option4" GroupName="A" runat="server" />
<label for='<%=Option4.ClientID %>' id="lblOption4" runat="server">4</label>
But the problem with this code is that it is not working and rendering the for attibute's value as it is i.e. <%=Option4.ClientID %>. :-(
Is their any asp net server control which would render tag?
I don't want to set the Text property of the radio button due to some CSS limitations so plz do not give answers like why don't you set the Text property of the radio button.
if this is .NET 2.0 or later, then use the ASP.NET LABEL control.
<asp:RadioButton ID="Option4" GroupName="A" runat="server" />
<asp:Label AssociatedControlId="Option4" Text="4" runat="server" />
If this is to ultimately handle accessiblity issues you could try the following tutorial: Creating an Accessible LABEL control in ASP.NET