I have this custom radiobuttonlist I'm using in my .aspx page in order to be able to get the GroupName to actually work since I will have 2 RadiobuttonList controls on the same .aspx page:
public class CustRadioButtonList : RadioButtonList, IRepeatInfoUser
{
void IRepeatInfoUser.RenderItem(ListItemType itemType, int repeatIndex, RepeatInfo repeatInfo, HtmlTextWriter writer)
{
RadioButton radioButton = new RadioButton();
radioButton.Page = this.Page;
radioButton.GroupName = "radioButtonGroup";
radioButton.ID = this.ClientID + "_" + repeatIndex.ToString();
radioButton.Text = this.Items[repeatIndex].Text;
radioButton.Attributes["value"] = this.Items[repeatIndex].Value;
radioButton.Checked = this.Items[repeatIndex].Selected;
radioButton.TextAlign = this.TextAlign;
radioButton.AutoPostBack = this.AutoPostBack;
radioButton.TabIndex = this.TabIndex;
radioButton.Enabled = this.Enabled;
radioButton.RenderControl(writer);
}
}
So that's just a simple extentension where I set the GroupName to be sure that all the radiobuttons created by that RadioButtonList have the same GroupName so that now, if anyone selects a value from RadiobuttonList1, it deselects any value they have selected in Radiobutton2 and vice versa (so they are mutually exclusive sets of radiobuttons).
Note: The radiobuttonlists are definitely being binded via a method call that's wrapped in a check for !Page.IsPostBack so that is not the issue here.
Here is an example where I'm using it in my .aspx page:
<aj:CustRadioButtonList checked="false" ID="rblEmail" runat="server" />
<aj:CustRadioButtonList checked="false" ID="rblReason" runat="server" />
Here in my code-behind, I'm checking for the selectedValue from the rblEmail within an onclick even of a button on my page..but it always returns empty string even if I have selected an item in the list:
protected void btnContinue_Click(object sender, EventArgs e)
{
_actionID = rblEmail.SelectedValue;
I've spent an entire day around trying to figure out now why I keep getting an emptystring when clearly I have selected a value in rblEmail. Same holds true for the other radiobuttonlist rblReason. In either case, when checking from code-behind I get emptystring for SelectedValue.
If you look at the markup, here's how it looks:
<table id="rblEmail" checked="false" border="0">
<tr>
<td><input id="rblEmail_0" type="radio" name="radioButtonGroup" value="0" /><label for="rblEmai_0">All Offers</label></td>
</tr><tr>
<td><input id="rblEmail_1" type="radio" name="radioButtonGroup" value="1" /><label for="rblEmail_1">week</label></td>
</tr><tr>
<td><input id="rblEmail_2" type="radio" name="radioButtonGroup" value="2" /><label for="rblEmail_2">month</label></td>
</tr><tr>
<td><input id="rblEmail_3" type="radio" name="radioButtonGroup" value="3" /><label for="rblEmail_3">Holiday</label></td>
</tr>
</table>
</div>
...
<table id="rblReason" checked="false" border="0">
<tr>
<td><input id="rblReason_0" type="radio" name="radioButtonGroup" value="1" /><label for="rblReason_0">I receive</label></td>
</tr><tr>
<td><input id="rblReason_1" type="radio" name="radioButtonGroup" value="2" /><label for="rblReason_1">I have no need</label></td>
</tr><tr>
<td><input id="rblReason_2" type="radio" name="radioButtonGroup" value="3" /><label for="rblReason_2">Other</label></td>
</tr>
</table>
Don't know exactly why this isn't working, but it seems like a little javascript would provide an easy workaround. Instead of a custom control, just use regular RadioButton objects... then attach some javascript to clear the selection on List1 when something on List2 is selected, and vice versa.
Not sure why you'd use any sort of repeater here if you already know how many radiobuttons there would be? (per your comments above)
It seems that you're populating the RadioButtonList on the page load -
If so - make sure you surround your population of the RadioButtonList with an
If/Then/Postback block:
if not Page.IsPostBack then
' populate your RadioButtonList
end if
eg:
if (!IsPostBack)
{
loadradiobuttonlist();
}
Related
I'm trying to display a list of strings and I want the user to be able to edit the list to make changes to the object but when I view the list after modifying the input fields the changes haven't been made.
How do I bind a list of strings?
#foreach (var message in UserMessageService.GetSomeData())
{
<tr>
<td><input type="text" bind="#message.Username" value="#message.Username" onblur="SaveMessages"/></td>
<td><input type="text" bind="#message.Message" value="#message.Message" onblur="SaveMessages"/></td>
</tr>
}
Use right Blazor syntax:
<input
type="text" #bind="#message.Message"/>
If you need to execute function on blur:
<input value="#message.Message"
#onchange="#((ChangeEventArgs __e) =>
{ message.Message = __e.Value.ToString());
SaveMessages(); })" />
More info: https://learn.microsoft.com/en-us/aspnet/core/blazor/components?view=aspnetcore-3.0#data-binding
silly mistake on my part, the issue wasn't with my syntax. I made the mistake of binding to my service instead of a local variable so when I left focus it couldn't update and just retrieved from the service
I simply added the local variable below
#foreach (var message in dataVariable)
{
<tr>
<td><input type="text" #bind="message.Username" /></td>
<td><input type="text" #bind="message.Message" /></td>
</tr>
}
#code {
private List<someData> dataVariable;
protected override void OnInitialized()
{
TableSeats = UserMessageService.GetSomeData();
}
}
I have the following HTML
`<table id="tableRepeat44" class="scaledTableGroup">
<tbody>
<tr>
<tr class="grpTableRegRow">
<td class="borderCell" title="Strongly Disagree">
<input id="QID16108TAG1" value="1" name="QID16108TAG" type="radio">
<td class="borderCell" title="Disagree">
<input id="QID16108TAG2" value="2" name="QID16108TAG" type="radio">
<td class="borderCell" title="Somewhat Disagree">
<input id="QID16108TAG3" value="3" name="QID16108TAG" type="radio">
<td class="borderCell" title="Somewhat Agree">
<input id="QID16108TAG4" value="4" name="QID16108TAG" type="radio">
<td class="borderCell" title="Agree">
<input id="QID16108TAG5" value="5" name="QID16108TAG" type="radio">
<td class="borderCell" title="Strongly Agree">
<input id="QID16108TAG6" value="6" name="QID16108TAG" type="radio">
<td class="questionColumn">
<span id="lblsubq16108_7" class="Answer">This is a question. </span>
</td>
</tr>
<tr class="grpTableAltRow">
<tr class="grpTableRegRow">
<tr class="grpTableAltRow">
</tbody>
</table>`
Each row contains the same syntax as the expanded first row. What I'm looking to do through CodedUI is obtain all of the questions' text (e.g. td:nth-of-type(7) > span || class="Answer") and then navigate out to the parent row so that I can access the Agree/Disagree radio button tds and make a selection based off a passed in parameter.
I have tried variations of
HtmlCustom questionsTable = new HtmlCustom(browser);
HtmlControl controls = new HtmlControl(questionsTable);
if (howToAnswer.Trim().ToLower() == "correct")
{
questionsTable.SearchProperties[HtmlCustom.PropertyNames.TagName] = "Tbody";
controls.SearchProperties.Add(HtmlControl.PropertyNames.Class, "Answer");
UITestControlCollection collection = controls.FindMatchingControls();
foreach (UITestControl answers in collection)
{
Debug.Write(answers);
}
}
And it resulted in ControlType [Pane] ClassName [HtmlPane] TagName [SPAN], UniqueIdentifier [154] Id [lblsubq16172_7] Name [] for each row in the table. So I was close, but I haven't been able to figure out how to obtain the text for .InnerText is not accessible on answers.
And the last item to address would be how to navigate back and forth while maintaining reference to that particular row?
This is actually a pretty cool question. Remember that all elements in the DOM in Coded UI have a single parent and a collection of children.
In short, what you'll want to do is:
Create the Table Control
Iterate through each row
In each row, find the first HtmlSpan with class = "Answer". Add this to a collection of HtmlSpans.
Once you have this collection of Spans, you can once again iterate through this collection. From there I'd suggest creating a helper method that gets a "Sibling Control" based on an attribute. The method would look at the parent control's child collection and then get an element from there.
The last step is only necessary if you can't just select the correct answer while iterating through each row in the table. In my book, I'd rather:
Iterate through each row
Find the Answer element and its inner text
Use the inner text to choose the correct answer by checking the correct radio button in this same row(I assume this is data driven).
Does this make sense? Let me know if this helps at all or if you need more detail. I don't have my helper method code but it's exceedingly simple. Something like:
public static UiTestControl GetSiblingByClass (UiTestControl ctrl, string class) {
var parent = ctrl.Parent;
var controlToReturn = new UiTestControl(parent);
controlToReturn.SearchProperties[HtmlControl.PropertyNames.Class] = class;
return controlToReturn;
}
I have 2 dropdown lists. The first one contains the list of Users. And the second one contains the list of the Phone numbers. So when i select a name from the First dropdown list the phone number of the corresponding user should be selected from the second drop down list .When i change the selection on first dropdown the page is realoading again but the eent is not triggering. Below is the code i tried.
Thanks
ASPX code:
<table>
<tr>
<td align="right" valign="top">
<div class="fieldLbls" style="text-align: right;">
<label>Users :</label>
</div>
</td>
<td>
<asp:DropDownList ID="UserDDL" Width="175px" runat="server"
OnSelectIndexChanged="User_SelectedIndexChanged" AutoPostBack="true">
</asp:DropDownList>
<label>Phone No :</label>
<asp:DropDownList ID="PhoneNoDD1" Width="175px" runat="server">
</asp:DropDownList>
</td>
</tr>
</table>
Code on aspx.xs
protected void User_SelectedIndexChanged(object sender, EventArgs e)
{
PhoneNoDD1.ClearSelection();// giving a try whether its triggering or not to clear selection but not triggering
}
When UserDDL selected index changed update the selected value of PhoneNoDD1.
protected void User_SelectedIndexChanged(object sender, EventArgs e)
{
PhoneNoDD1.SelectedValue = value;
}
I am trying to get the selected radio button value to from html to server side so that i can save them in the data base i have done similar in php not sure how to do that in asp.net/ C#
<form runat="server" id='attendence_form' >
<table id="attendence_div" width="100%" align="center" cellpadding="2" cellspacing="2" border="0">
<tr align="left" style="background-color:#004080;color:White;">
<td>Student Name</td>
<td>Present</td>
<td>Absent</td>
<td>Leave</td>
</tr>
<tr>
<td>ANITHA S</td>
<td><input type="radio" name="Present0" value="Present"></td>
<td><input type="radio" name="Present0" value="Absent"></td>
<td><input type="radio" name="Present0" value="Leave"></td>
</tr>
<tr>
<td>ANITHA T C</td>
<td><input type="radio" name="Present1" value="Present"></td>
<td><input type="radio" name="Present1" value="Absent"></td>
<td><input type="radio" name="Present1" value="Leave"></td>
</tr>
<tr>
<td>BINDU K V</td>
<td><input type="radio" name="Present2" value="Present"></td>
<td><input type="radio" name="Present2" value="Absent"></td>
<td><input type="radio" name="Present2" value="Leave"></td>
</tr>
</table>
Hear is the php code this recives the html form values form the user side via ajax and insert i want the same in asp.net
$conn = new mysqli($servername, $username, $password, $dbname);
// ** insert data in to data base ** //
$sql = "INSERT INTO attendance_master (AttendanceDate) VALUES ";
// **hear the data or is calculated using student as a string the number of student names are passed the same number of data is inserted in to data base ** //
foreach($_GET['student'] as $i=>$student) {
// ** so the data is inserted in to data base ** //
$sql .= sprintf("%s ('%s')"
, ($i==0 ? '' : ',')
// ** data from Ajax ** //
, mysqli_real_escape_string($conn, trim($_GET['present'][$i]))
);
}
// ** on success full function ** //
if ($conn->query($sql)) {
// **can do any thing to recognise if the data is inserted if this gives the out put then the data is shorly inserted in to data base ** //
}
you can try using <asp: RadioButton id="" runat="server" > .
In the server side you can check the "Checked" property like this
if(radioBtn.Checked==true) {}
If it is a webforms project you should use
<asp:RadioButton id="someId" runat="server">
Update:
To handle when the radiobutton is clicked you need to handle the CheckedChanged event.
someId.CheckedChanged += event_CheckedChanged;
private void event_CheckedChanged(Object sender, EventArgs e)
{
... do stuff...
}
I'd like to disable all elements within a <tr> element when a checkbox inside it is checked. My code is as follows:
<tr>
<td style="width:25px" id="td_CheckBoxClickable">
<asp:CheckBox runat="server" ClientIDMode="Static" ID="CheckBox_EnableRow" OnCheckedChanged="CheckBox_EnableRow_CheckedChanged" />
</td>
<td>
<asp:TextBox runat="server" ID="textBox_Count" />
</td>
<td>
<asp:TextBox runat="server" ID="textBox_Value" />
</td>
</tr>
and currently the CheckBox_EnableRow_CheckedChanged event is empty.
I was thinking it would be possible to look at the parent of the parent of the checkbox like this:
protected void CheckBox_EnableRow_CheckedChanged(object sender, EventArgs e) {
TableRow row = (TableRow)((CheckBox)sender).Parent.Parent;
row.Enabled = false;
}
but that doesn't seem to work.
I would recommend using jquery. Using jquery you can find the parent tr element of the checkbox, then you change the attribute of all input elements inside the tr element to disabled=true. First you have to give tr a class, say class="trclass", then you have to get the parent tr for the checkbox.
html part:
<input id="Checkbox1" type="checkbox" onchange="DsiableRow(this)" />
javascript part:
function DsiableRow(MyCHeckbox)
{
var trElement = $(MyCHeckbox).parents(".trclass");
$("trElement :input").attr("disabled", true);
}
I would solve it like this:
<input id="theCheckBox" type="checkbox" onchange="DisableRow(this)" />
function DisableRow(checkBox)
{
var $trElement= $(checkBox).closest("tr");
$trElement.find("input").attr("disabled", "disabled");
}
Differences Between HTML and XHTML
In XHTML, attribute minimization is forbidden, and the disabled attribute must be defined as .
Source: http://www.w3schools.com/tags/att_input_disabled.asp
Also note, if you use the default submitting method in the browser then:
Tip: Disabled elements in a form will not be submitted.
Source: http://www.w3schools.com/tags/att_input_disabled.asp
Use javascript to get the <tr> and make it as display:none