populating HTML Radio buttons from code behind in C# - c#

I am generating radio buttons from Code Behind in the following code
var licRB = questions[QNo - 1].AnswerRadioButton.optionRB;
for (int i = 0; i < licRB.Length; i++)
{
if (licRB[i]._checked == "true")
{
stringBuilder.Append(String.Format("<input type=\"radio\" name=\"Q" + (QNo) + "AnswerRBOption\" value='{0}' title='{1}' checked=\"checked\" />{1} <br />", licRB[i].value, licRB[i].text));
}
else
{
stringBuilder.Append(String.Format("<input type=\"radio\" name=\"Q" + (QNo) + "AnswerRBOption\" value='{0}' title='{1}' />{1} <br />", licRB[i].value, licRB[i].text));
}
}
(FindControl("divQ" + QNo + "AnswerRBL") as HtmlGenericControl).InnerHtml = stringBuilder.ToString();
On submit button click the value selected is stored in the db as follows:
ARBValue = Request.Form["Q" + QNo + "AnswerRBOption"],
Now I need to retrieve the value from the DB and show the radio buttons with the selected value. Pls let me know best way to do this?

Below text resolves the issue of showing the selected radio button in the HTML5 radio button list
(FindControl("divQ" + q.QNo + "AnswerRBL") as HtmlGenericControl).InnerHtml = q.ARBOption
.Replace("checked=\"checked\"","")
.Replace("value='"+ q.ARBValue + "'" , "value='" + q.ARBValue + "' checked=\"checked\"");

Related

changing two dropdownlists selected item from sql table but both get same selected value

I have two dropdowns initializing from same sql table:
ddlETCsc1.Items.Clear();
ddlETCsc2.Items.Clear();
foreach (var PSiteContacts in ContactsAdapter.GetPSiteContacts(Cus_Id))
{
var item = new System.Web.UI.WebControls.ListItem();
item.Text = PSiteContacts.name + " / " + PSiteContacts.phone;
item.Value = PSiteContacts.name + " / " + PSiteContacts.phone;
item.Attributes.Add("data-subtext", PSiteContacts.con_type);
ddlETCsc1.Items.Add(item);
ddlETCsc2.Items.Add(item);
}
ddlETCsc1.Items.Insert(0, new System.Web.UI.WebControls.ListItem("Nothing Selected", "0"));
ddlETCsc2.Items.Insert(0, new System.Web.UI.WebControls.ListItem("Nothing Selected", ""));
I am changing there selected item from sql table columns SContact1 and Scontact2 these have different data but both get same selected item:
ddlETCsc1.SelectedValue = reader["SContact1"].ToString();
ddlETCsc2.SelectedValue = reader["SContact2"].ToString();
I can see by addiong breakpoints that ddlETCsc1 gets correct value first but when ddlETCsc2 value changes the ddlETCsc1 get same value as ddlETcsc2.
It works fine for first time(i have dropdowns in bootstrap modal) but when i adds new contact(button on modal that opens another modal to add contact and after adding opens previous modal and also initialize the dropdowns again with new values and fetch values from sql) then the selected value not work as expected
Any help will be appreciated. Thanks in advance
The reason is because when you add item from your ContactsAdapter iteration, you refer to the same item (same ListItem) reference for each item in ddlETCsc1 and ddlETCsc2 (except for your "0" and "" later on). Try to change your code to:
ddlETCsc1.Items.Clear();
ddlETCsc2.Items.Clear();
foreach (var PSiteContacts in ContactsAdapter.GetPSiteContacts(Cus_Id))
{
var item1 = new System.Web.UI.WebControls.ListItem();
item1.Text = PSiteContacts.name + " / " + PSiteContacts.phone;
item1.Value = PSiteContacts.name + " / " + PSiteContacts.phone;
item1.Attributes.Add("data-subtext", PSiteContacts.con_type);
ddlETCsc1.Items.Add(item1);
var item2 = new System.Web.UI.WebControls.ListItem();
item2.Text = PSiteContacts.name + " / " + PSiteContacts.phone;
item2.Value = PSiteContacts.name + " / " + PSiteContacts.phone;
item2.Attributes.Add("data-subtext", PSiteContacts.con_type);
ddlETCsc2.Items.Add(item2);
}
ddlETCsc1.Items.Insert(0, new System.Web.UI.WebControls.ListItem("Nothing Selected", "0"));
ddlETCsc2.Items.Insert(0, new System.Web.UI.WebControls.ListItem("Nothing Selected", ""));
To create two different references (albeit having the same initial value) for each item inserted in the ddlETCsc1 and ddlETCsc2

Load a specific row as default item in combobox

I fill combo-box with a data from database Access but my question is how can I put a specific row in database filled as a Default item in combo-box with C#?
gerant remplirlistgerant = new gerant();
foreach (gerant ligne in remplirlistgerant.getinfogerant())
{
cmbgerant.Items.Add(ligne.CIN_GERANT + " - " + ligne.NOM_GERANT + " - " + ligne.PRENOM_GERANT);
}
Hope I know yours,
Example if ligne.CIN_GERANT = "aaa" the current row will be selected.
gerant remplirlistgerant = new gerant();
foreach (gerant ligne in remplirlistgerant.getinfogerant())
{
cmbgerant.Items.Add(ligne.CIN_GERANT + " - " + ligne.NOM_GERANT + " - " + ligne.PRENOM_GERANT);
// Example if ligne.CIN_GERANT = "aaa" then select this row.
if (ligne.CIN_GERANT == "aaa" )
{
cmbgerant.SelectedIndex = cmbgerant.Items.Count - 1;// Item just added
}
}

Jquery dynamic TextBoxes with Request.form Iteration and Save to DB

i am Adding multiple TextBoxes with Jquery in my Application, then in code behind file i want can access the values by Request.form[name]. I want to iterate these textboxes and read values of whatever Text is entered by the user, so i can store it in database.
any idea how can i save the value of these textboxes in Database spliting each textbox values by a Comma(,)
Please guide me how to get all these textbox values in loop and then save them in DB Table
$(document).ready(function () {
var counter = 2;
$(\"#addButton\").click(function () {
if (counter > 10) {
alert(\"Only 10 textboxes allow\");
return false;
}
var newTextBoxDiv = $(document.createElement('div'))
.attr(\"id\", 'TextBoxDiv' + counter);
newTextBoxDiv.html('<table><tr><td><input type=\"text\" name=\"textbox' + counter +
'\" id=\"textbox' + counter + '\" value=\"\" ></td><td><input type=\"text\" name=\"textbox' + counter +
'\" id=\"textbox' + counter + '\" value=\"\" ></td><td><input type=\"text\" name=\"textbox' + counter +
'\" id=\"textbox' + counter + '\" value=\"\" ></td></tr></table>');
newTextBoxDiv.appendTo(\"#TextBoxesGroup\");
return false;
counter++;
});
Probably you have do something like this, you could easily debug by using chrome debugger tool to retrieve all values from your input boxes.
$('table tr td').each(function() {
var values = "";
values = $(this).find('input').val() + ",";
});

Click Event for Dynamically Generate Linkbutton

i created a simple registration page which have fields Name,Username and Password.. actually am checking the username with database whether that username already had or not...
suppose if that username already taken,then i display"Username Already Taken" message in label,and also i create dynamically 4 link buttons for username suggestions like
now when i click on linkbuttons,that are bind to username textbox.. here is my code
give me some idea how to bind that linkbutton to textbox..
You should minimize server requests from client, especially if you want to update UI controls. I suggest to use java script to update text box with new value.
for (int i = 0; i < 4; i++)
{
LinkButton lbtn = new LinkButton();
lbtn.OnClientClick = "document.getElementById('" + txtuname.ClientID + "').value = '"+txtuname.Text + i+"'; return false;";
lbtn.Text = txtuname.Text + i;
phlinks.Controls.Add(lbtn);
phlinks.Controls.Add(new LiteralControl(" "));
}
Try this
for (int i = 0; i < 4; i++)
{
LinkButton lbtn = new LinkButton();
lbtn.OnClientClick = "document.getElementById('" + txtuname.ClientID + "').value = this.innerText; return false;";
lbtn.Text = i.ToString();
phlinks.Controls.Add(lbtn);
}

Creating jquery object array and posting its values on form submit

I have dynamicly added html elements(selectlists) in a form :
//Dynamicly adding selectlist elements
function newshit() {
i = i + 1
$("#mytable").append("<tr><td><div id='div" + i + "'><select id='elem" + i + "' name='elem" + i + "' class='ted'></select><input type='button' value='-' id='buttonminus" + i + "' style='width:5%;' onclick='removeelem(elem" + i + ",buttonminus" + i + "," + i + ")'/></div></td></tr>")
getitems("elem" + i)
}
//filling lists
function getitems(item) {
$.getJSON('#Url.Content("~/Stok/Items/")', {}, function (data) {
$.each(data, function (i, c) {
$("#" + item).append("<option value='" + c.Value + "' title='" + c.Text + "' label='" + c.Text + "'>" + c.Text + "</option>")
})
})
}
//removing element, when button next to it used
function removeelem(elem,buttonminus,i) {
if ($("select").length > 1) {
$("#div" + i).closest('tr').empty().remove()
} else if ($("select").length <= 1) {
alert("At least 1 of items must be chosen to create a group!")
}
}
//checking elements and values existence
function check() {
var slcts = $("select").serialize();
alert(slcts)
}
im trying to get the value of each selectlist's selected option value and put them into an array than send them to my controller on form submit.
How can i achive this?
Need to check this, but I think that the following should work:
Change your code so that the format of your ids is something like:
id="elem[0]"
Then if your controller has a signature something like this:
public ActionResult Something(IEnumerable<string> elem)
{
}
Then his should "just work".
You could use something like -
var selectdata = $("select").serialize()
This will return a string in the form <select name attribute1>=<chosen value1>&<select name attribute2>=<chosen value2> etc. You'd need to add a 'name' attribute to your select HTML when you create it for this to work.
Demo - http://jsfiddle.net/ipr101/fZXha/
You could then put the selectdata variable in a hidden field before the form was posted or send it via AJAX using the ajax or post methods.

Categories