ASP.NET c# - RadioButtons not grouped - c#

I have a GridView and in its OnRowDataBound callback I am adding a new cell and to that cell I'm adding a RadioButton. I've set the GroupName on the radio button, but I can still select more than one button at a time.
How can I make them grouped so that only one radio button can be selected within the group.
EDIT - This is my code:
// Add the radio button to select if this purchased address
// is set the the home address of the contact
e.Row.Cells.AddAt(0, new TableCell());
RadioButton rbSetAddress = new RadioButton();
rbSetAddress.ID = "rdSetAddress" + e.Row.Cells[2].Text;
rbSetAddress.GroupName = "SetAddress";
e.Row.Cells[0].Controls.Add(rbSetAddress);
So I'm setting a unique ID and the same GroupName. ASP.NET is NOT setting the name attribute of the input to the GroupName. So I can select as many radio buttons as I want. This is not what I want to happen. I want to be able to select one and only one in the group identified by the GroupName.

http://www.codeproject.com/KB/webforms/How_group_RButtons.aspx
http://www.developer.com/net/asp/article.php/3623096/ASPNET-Tip-Using-RadioButton-Controls-in-a-Repeater.htm
Basically, when in a repeater of any kind (which a GridView is), the names are no longer all the same from the group property as a result of the way asp.net names things inside a repeater (essentially each control gets a prefix to make it unique).

Are you sure you are giving different names to the radiobuttons on codebehind?
for (int i = 0; i < 10; i++)
{
RadioButton rb = new RadioButton();
rb.ID = "rb"+i;
rb.GroupName="rbGroup";
GridView1.Rows[i].Cells[3].Controls.Add(rb);
}

Related

WPF drop-down list for auto generated column in datagrid

How to make a drop-down list when you click on an element of a specific table column in which you can select an element for this cell? Column is auto generated.
A Combobox in xaml/wpf is used like this:
<ComboBox x:Name="some Name" SelectionChanged="comboboxChanged">
<ComboBoxItem>The Content of your Combobox</ComboBoxItem>
</Combobox>
ComboBoxItems are essentially the dropdown part. You can add as many as you want.
In your back end (c#) you can get the selected Value as soon as the "SelectionChanged"-event is triggered. The code for getting the selected Value can be done multiple ways. Example:
private void comboboxChanged(object sender, SelectionChangedEventArgs e){
string comboboxvalue = comboboxname.Text;
//Then set associated textblock or label
labelname.Content = comboboxvalue;
}
The code above would be static though. Dynamically generating these elements could look like this for instance.
When auto-generating, using an inline function for the event is easy.
for (int i = 0; i < 10; i++){
ComboBox comboboxname = new ComboBox();
comboboxname.SelectionChanged += (ss,ee) { string comboBoxValue = comboboxname.Text; labelname.Content = comboBoxValue;}
}
Labelname being the name of the Label you want to set. In that loop you will need to implement a way of giving each box a unique name and getting the name of the associated label in there as well. That you will have to figure out on your own as i do not know how and what exactly is generated and what is static.
You will also need to add your dynamically created combobox to your listpanel or grid or whatever you are using. This works like this:
listpanelname.Children.Add(comboboxname);
Just add that to the "for" loop.

dynamic checkboxlist to be selected and passed the value of selected item on button click

I have created a checkboxlist and dynamically it displays value of the list.
But I need to design as per the image shown. What css class or which option to be used to achieve this.
listcontents.Add(thisQ.Option.ToString());
//CheckBoxList CB = new CheckBoxList();
CheckBoxList1.DataSource = listcontents;
CheckBoxList1.DataBind();

Filling two nested drop dwon list when editing an entry in c#

i have two nested drop down list.
one drop down show group,the other show sub group.
i have coded data source of subgroup drop down list on group drop down list selected indexed change event.
What I'm trying to do is set a drop down list to be whatever its value is in the database when editing an entry.
i have used data row.
i have two tables.documentgroup(GroupId,parentId,groupTitle,subGroupTitle) and documents(DocID,GroupID,Title,url)
in my drop down lists i have added list item like this
<asp:ListItem Text = "--select group--" Value = ""></asp:ListItem>
when i click on editing a document,i have this.
protected void grdDocuments_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "DoEdit")
{
GC.Collect();
GC.WaitForPendingFinalizers();
int DocID = Convert.ToInt32(e.CommandArgument);
ViewState["DocumentID"] = DocID;
ViewState["EditMode"] = "Edit";
DataTable dtDocuments = DataLayer.Documents.SelectRow(DocID).Tables["Documents"];
if (dtDocuments.Rows.Count != 0)
{
DataRow drDocuments = dtDocuments.Rows[0];
txtDocTitle.Text = drDocuments["DocTitle"].ToString();
txtDocPubYear.Text = drDocuments["DocPubYear"].ToString();
ddlDocGroupTitle.SelectedValue = drDocuments["ParentID"].ToString();
ddlDocSubGroupTitle.SelectedValue = drDocuments["DocGroupID"].ToString();
ViewState["DocCurrentUrl"] = drDocuments["DocUrl"].ToString();
mvDocuments.SetActiveView(vwEdit);
}
}
but i get this error
'ddlDocSubGroupTitle' has a SelectedValue which is invalid because it
does not exist in the list of items. Parameter name: value
and this is the same for ddldocgroupTitle.
i have made an inner join between two tables.
what should i do?
I could not really grasp your question, but it looks like you want to have 2 dropdown, and upon selection in the first, the second gets populated/filtered correct?!?
I recommend that you use javascript/Jquery to do so. Upon page load, issue a ajax get request to fill the "main" dropdown, and upon selection on the first one, you issue a second ajax request to fetch the possible values of the second one...
A alternative is to fetch the items for the first dropdown AND all the values possible for the second, storing the result in some hidden-field data. Then upon selection of the first value, you can only filter + display relevant data.

How to check only one radio button at a time (others disable)

I am working in silverlight c# and i have situation where i create radio buttons programatically. My code to do so is this:
Grid childGrid = CreateChildGrid();
int NumberOfRadioButton =0;
RadioButton[] RadioBut= new RadioButton[5];
int count = 0;
foreach (var item in param.Component.Attributes.Items)// this param.Component.Attributes.Items value is 4 in fact.
{
NumberOfRadioButton++;
RadioBut[count] = new RadioButton();
RadioBut[count].GroupName = item;
RadioBut[count].Content = item;
sp.Children.Add(RadioBut[count]);
count++;
}
Problem: The problem is it checks all the button where as i want only one button checked at a time . I mean if one checked the others must be disable.
Could some one please help me to achieve my target ? Thanks a lot.
Note: I am using silverlight to do so.
You should set the GroupName property of all RadioButtons to the same, so they will be "grouped", meaning only one of them can be selected at a time.
So this line:
rbs[count].GroupName = item;
Should be something like this:
rbs[count].GroupName = "MyRadioButtonGroup";
Of course the string can be anything, as long as it is the same for all RadioButtons.
You'll have to assign these radio buttons to a common group. Check Radio Button Group Name
RadioButton[] rbs = new RadioButton[5];
rbs.GroupName = "Add your common groupname here"; // added code
int count = 0;

Populate textbox from checkbox list using ASP.NET and AJAX

I have a textbox control and a checkbox list control on my page.
I am first populating my checkbox list with a list of employee objects in the code-behind. An employee has an id and a name. Both are displayed in the checkbox list like so:
Jim (1)
Alex (2)
Gary (3)
When a user checks one of the boxes, the employee's name needs to be populated in the textbox. So if Jim is selected, the textbox value is "Jim". It also needs to support multiple values, so if Jim and Gary are selected, the textbox value is "Jim, Gary".
Also, if a user enters a valid value in the textbox, the correct employee should be checked. This needs to support names and id's. So if I enter "1,Alex" in the textbox and then click outside the textbox, Jim and Alex should be selected.
I'm using ASP.NET and I need to do this using AJAX, but I have no experience with using jQuery and AJAX. Could someone show me a simple example of how to do this?
Here is something I through together that might help get you started. It's not tested and not complete but I don't have time to do it all right now, so thought this might help. There definitely needs to be a lot more checks and paring done that I left out for you to implement.
$(function() {
// gets all checkbox items by a common class you will put on all of them
$('.checkboxlistclass').click(function(e) {
var txt = $(this).text();
var employeeName = txt; // change this to parse out the name part and remove the id
var currentTxtVal = $('#textboxid').val();
var newVal = currentTxtVal + ',' + employeeName; // check if you need the comma or not
$('#textboxid').val(newVal);
});
// textboxid is the id of the textbox
$('#textboxid').change(function(e) {
var textboxVal = $(this).val();
// split on the comma and get an array of items
// this is dummy data
var items = [];
items.push('Jim');
items.push(2);
// go through all the items and check if it's a number or not
// if it's a number do a selection based on the id in parenthesis
// if not then check first part for name match
for (var i=0; i < items.length; i++) {
if (isNaN(parseInt(items[i])) {
$(".checkboxlistclass:contains('" + items[i] + " ('").attr('checked', true);
}
else {
$(".checkboxlistclass:contains(' (" + items[i] + ")'").attr('checked', true);
}
}
});
});

Categories

Resources