Bind selected radio button value in KnockOut - ASP.net project - asp.net

I'm new to KnockOut and I just wanted to bind selected radio button value to td. For my knowledge I didn't do any mistake but my following code doesn't work anymore. Please help me to solve this minor mistake.
Other code parts are working fine in the same page, So nothing wrong other than this code part.
<td data-bind="text: Sex"></td>
<td>
<asp:RadioButton ID="RadioButtonMale" value="Male" GroupName="RadioGroup1" data-bind="checked: Sex" runat="server" Text="Male"/>
<asp:RadioButton ID="RadioButtonFemale" value="Female" GroupName="RadioGroup1" data-bind="checked: Sex" runat="server" Text="Female"/>
</td>
<script type="text/javascript">
var ViewModel = {
Sex : ko.observable("Male"),
};
ko.applyBindings(ViewModel);
</script>

The html that asp .net is generating is incorrect. It needs to have data-bind="checked: Sex" on the input instead of the span. Instead of using <asp:RadioButton do the following:
<input type="radio" ID="RadioButtonMale" value="Male" Name="RadioGroup1" data-bind="checked: Sex" runat="server"><label for="RadioButtonMale">Male</label>
<input type="radio" ID="RadioButtonFemale" value="Female" Name="RadioGroup1" data-bind="checked: Sex" runat="server" Text="Male"><label for="RadioButtonFemale">Female</label>

Related

How do I attach datepickers to text boxes with the same ID?

I have an ASPX that has a repeater. Inside the repeater is a text box with a JQuery datepicker attached to it. The text box's ID is static, so it is the same for all of those items within the repeater. However, when I make a change to the second or subsequent repeater item's date box using a datepicker, it changes the first item's date box and leaves the one actually attached to the selected datepicker alone.
Other than the obvious solution of "don't define items with the same ID," which results in another problem that will probably end up in another question, is there a way to fix this problem?
This example
<asp:Repeater runat="server"
ID="rptWhatever"
ClientIDMode="Predictable">
<ItemTemplate>
<asp:TextBox runat="server"
ID="tbxWhoCares"
CssClass="ImaDatePicker">
</asp:TextBox>
</ItemTemplate>
</asp:Repeater>
Will render the textbox to something like:
<input id="tbxWhoCares" type="text" class="ImaDatePicker"/>
So regardless of the id you should be able to do this in your script:
$(".ImaDatePicker").datepicker();
The problem with non-unique ID's is a big one but should be an easy fix if you set the attributes in the Repeater to ClientIDMode="Predictable" and leave the same attribute in the TextBox to its default.
Update
It seems jquery UI datepicker requires a unique id.
So you can select multiple inputs using a common css class but the id's must be unique otherwise you run into that problem.
It shouldn't work that way...but it does.
Alternatively you could just use the native <input type="date" ... />
$(function() {
$(".ImaDatePicker").datepicker();
});
<link href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.13.2/themes/base/theme.min.css" rel="stylesheet"/>
<link href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.13.2/themes/base/jquery-ui.min.css" rel="stylesheet"/><script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.13.2/jquery-ui.min.js"></script>
<input id="tbxWhoCares" type="text" class="ImaDatePicker" />
<input id="tbxWhoCares" type="text" class="ImaDatePicker" />
<input id="tbxWhoCares" type="text" class="ImaDatePicker" />
<br><br><br>
<input id="tbxWhoCares_1" type="text" class="ImaDatePicker" />
<input id="tbxWhoCares_2" type="text" class="ImaDatePicker" />
<input id="tbxWhoCares_3" type="text" class="ImaDatePicker" />

How can I use the css from an input tag in an asp button?

Hello I changed the the html Code of a bootstrap template,
Here is the old line
<input type="submit" id="AbteilungSearch" class="form-control" value="Suche starten" style="height:25px;">
and here is the new one
<asp:button Text="Suche starten" id="AbteilungSearch" cssclass="form-control" runat="server" style="height:25px;" />
Well my problem is that the Form-control does not get used anymore.

using CompareValidator compare 2 times

I have 2 inputs:
<label for="timeStart">Start Time:</label>
<input id="timeStart" value="10:00" type="text" runat="server" clientidmode="static" />
<label for="timeEnd">End Time:</label>
<input id="timeEnd" value="16:00" type="text" runat="server" clientidmode="static" />
and comparer validator inline with submit button
<input class="btn" id="save" value="CREATE Object" type="submit" runat="server"
ValidationGroup="save"/>
<asp:CompareValidator id="CompareTimes"
runat="server"
ControlToCompare="timeStart"
ControlToValidate="timeEnd"
ForeColor="Red" Display="Dynamic"
ErrorMessage="The End Time must be later than the Start Time."
Type="Date"
Operator="GreaterThan"
ValidationGroup="save"/>
in all case it gave me error message, what I do wrong?
Your inputs are client side controls. If you would like to use <asp:CompareValidator.../> you have to use server side label controls such: <asp:Lable runat="server".../>.
If you would like to use these (client side) input labels you have to compare them by JavaScript on the client side.

Getting the same name for HTMLInputCheckbox

Super newbie question here. I'm trying to teach myself Web Forms, so I'm experimenting with the runat="server" attribute. So here's my code:
<p>
Number of items:
<input type="checkbox" name="custom_name_qty" value="25" runat="server" />25
<input type="checkbox" name="custom_name_qty" value="50" runat="server" />50
<input type="checkbox" name="custom_name_qty" value="75" runat="server" />75
<input type="checkbox" name="custom_name_qty" value="100" runat="server" />100
<input type="checkbox" name="custom_name_qty" value="Other" runat="server" />Other
</p>
And here's what gets generated:
<p>
Number of items:
<input name="ctl01" type="checkbox" value="25" />25
<input name="ctl02" type="checkbox" value="50" />50
<input name="ctl03" type="checkbox" value="75" />75
<input name="ctl04" type="checkbox" value="100" />100
<input name="ctl05" type="checkbox" value="Other" />Other
So, it took my group of checkboxes that had the same name, and gave them separate names. Why is this? Even when I give them separate names (add an integer to the end of each one), they still get renamed.
What am I doing wrong here? How can I get the checkboxes to output the same name? How can I even get the checkboxes to keep the names that I give them? I'd rather work with a name I give than "ctl01".
You are not doing anything wrong, that's the default behaviour of asp.net. That is otherwise achieved with <asp:CheckListBox /> control: CheckListBox
<asp:CheckBoxList id="checkboxlist1"
AutoPostBack="True"
CellPadding="5"
CellSpacing="5"
RepeatColumns="2"
RepeatDirection="Vertical"
RepeatLayout="Flow"
TextAlign="Right"
OnSelectedIndexChanged="Check_Clicked"
runat="server">
<asp:ListItem>Item 1</asp:ListItem>
<asp:ListItem>Item 2</asp:ListItem>
<asp:ListItem>Item 3</asp:ListItem>
<asp:ListItem>Item 4</asp:ListItem>
<asp:ListItem>Item 5</asp:ListItem>
<asp:ListItem>Item 6</asp:ListItem>
</asp:CheckBoxList>
That shows how to add items manually. You can also populate the check list from database
System.Data.DataTable dtOptions = GetValuesFromDataBase();
checkboxlist1.DataSource = dtOptions;
checkboxlist1.DataBind();
Update
In Asp.Net 4 and above, you are given more control on how control ID are generated (e.g. AutoID, Static, Predictable, Inherit)

Change the value of a selected radio button on pageload

I have the following code that renders two radio buttons for the selections of Yes and No. On pageload, No is always selected, but I need Yes to be the default (I'm not the best at this type of coding, I've tried some things and my research hasn't clued me in). How can I adjust it? Thanks in advance.
<tr>
<td> Residential?
<input type="radio" name="Residential" value="0" onclick="document.QuoteInfo.save.value='0';document.OrderPanel.submit();" <%If Not Session("oDE").Fieldvalue("QUOTES","Residential") Then Response.Write " checked "%> />
No
<input type="radio" onclick="document.QuoteInfo.save.value='0';document.OrderPanel.submit();" name="Residential" value="1" <%If Session("oDE").Fieldvalue("QUOTES","Residential") Then Response.Write " checked "%> />
Yes
</td>
</tr>
Okay. If I understand correctly you don't need asp blocks to handling default checked attribute. Try to add manually.
<input type="radio" name="Residential" value="0" onclick="document.QuoteInfo.save.value='0';document.OrderPanel.submit();" /> No
<input type="radio" onclick="document.QuoteInfo.save.value='0';document.OrderPanel.submit();" name="Residential" value="1" checked="checked" /> Yes
Edit:
I edited because you need to asp blocks. onclick="document.QuoteInfo.save.value='0';document.OrderPanel.submit();" this is for both of no and yes. At least, for checkbox yes's onclick attribute could be onclick="document.QuoteInfo.save.value='1';document.OrderPanel.submit();" ?

Resources