I have two different dropdown boxes with prefilled selection items. I am trying to pass all of the users selected items and return data based on it. I am able to retrieve data based on the first dropdown list but for the second dropdown 'null' gets passed in. Here is my code:
resultSummaryViewModel.ReportFrame = new FramedViewModel();
if (string.IsNullOrEmpty(resultSummaryViewModel.Value)) return;
string viewValue = resultSummaryViewModel.Value.Substring(0, resultSummaryViewModel.Value.IndexOf("|"));
string viewType = resultSummaryViewModel.Value.Substring(resultSummaryViewModel.Value.IndexOf("|") + 1);
//if (string.IsNullOrEmpty(resultSummaryViewModel.CValue)) return;
string cTypeValue = resultSummaryViewModel.CValue.Substring(0, resultSummaryViewModel.CValue.IndexOf("|"));
string cType = resultSummaryViewModel.CValue.Substring(resultSummaryViewModel.CValue.IndexOf("|") + 1);
resultSummaryViewModel.ReportFrame.SourceURL = WebPathHelper.MapUrlFromRoot(
string.Format("Reporting/ResultSummary.aspx?beginDate={0}&endDate={1}&Id={2}&viewType={3}&cTypeValue={4}&cType={5}",
resultSummaryViewModel.BeginDate,
resultSummaryViewModel.EndDate,
viewValue,viewType,
cTypeValue,cType));
If there is another way to get back selected items from lists that would be great also. Thanks.
I had to encode my selection before passing it in the url as so :
string caseType = null;
if (!String.IsNullOrEmpty(viewModel.CaseTypeValue))
{
caseType = HttpUtility.UrlEncode(viewModel.CaseTypeValue, System.Text.Encoding.Default);
}
that worked well for me from there
Related
I have a long list of check boxes that I need to display on a form. I get the list of check boxes from a table in the database and render them on the page. When the page is saved, I am saving the field name and value (as they are stored in the database) of each selected item.
I also have a model class that all of these check box fields map to, but I was thinking, since I have the field name and value, I would be able to set only the properties for which I have a field/value.
I was thinking I could just do something like this, but I know the syntax isn't correct:
private void CreatePPAIndication(ReferralFormViewModel viewModel, Guid personId, Guid encId)
{
var ppaIndication = new PPAIndication();
ppaIndication.enterprise_id = "00001";
ppaIndication.practice_id = "0001";
ppaIndication.person_id = personId;
ppaIndication.enc_id = encId;
// set more properties...
// Here is where I would set a property for each selected check box.
foreach (var indication in viewModel.Indications.Where(o => o.IsSelected))
{
var result = "ppaIndication." + indication.FieldName + "(" + indication.FieldValue + ")";
}
// How can I essentially "execute" the string in the line above?
_context.PPAIndications.Add(ppaIndication);
}
So, basically, I am wondering how I can essentially "execute" the "result" string as real code. Thank you in advance for any advice!
you can use reflection for that
something like
GetType().GetProperty(o.FieldName).GetValue(indication, null);
to get the value
or
GetType().GetProperty(o.FieldName).SetValue(indication, o.Value);
to set the value
In asp.net , I need to assign back the values to textbox,dropdown and combobox values back to respective controls. I already prepopulated those dropdown and combobox with their respective values. I can see value in dropdown but not not in combobox since combobox is multiselector with Comma (',').
Note : I m getting comma seperated values as one single string and assigning to combobox .
I tried with the below code, but the combobox is not getting its value back.
if (oDs.Tables[0].Rows.Count > 0)
{
if (oDs.Tables[0].Rows.Count == 1)
{
textbox1.Text = oDs.Tables[0].Rows[0].ItemArray[0].ToString();
textbox2.Text = oDs.Tables[0].Rows[0].ItemArray[1].ToString();
textbox3.Text = oDs.Tables[0].Rows[0].ItemArray[2].ToString();
dropdown1.SelectedItem.Text = oDs.Tables[0].Rows[0].ItemArray[4].ToString();
dropdown2.SelectedItem.Text = oDs.Tables[0].Rows[0].ItemArray[5].ToString();
combbox1.SelectedText =oDs.Tables[0].Rows[0].ItemArray[6].ToString();**
}
}
}
Try something like this:
string[] csvFields = csvString.Split(',');
comboBox1.ItemsSource = csvFields;
I am trying to get selected name of state in my drop down list on fetch operation. It is able to populate names of state and as well as names of city on the selection of country/state.
Problem: At the data entry time when we select the 2/3 index value of list. then on the fetch operation it won't show as the selected.
To populate the list code is as follows.
private DataSet StateNames(string country)
{
prop.DS = objBL.PopStates(country);
if (prop.DS.Tables[0].Rows.Count > 0)
{
ddlStates.DataSource = prop.DS;
ddlStates.DataTextField = "ColName";
ddlStates.DataValueField = "Col_ID";
ddlStates.DataBind();
ddlStates.Items.Insert(0, new ListItem(" Select", "-1"));
}
else { }
return prop.DS;
}
To show the already selected & saved value I have wrote the following code:
ds2 = StateNames(ds.Tables[0].Rows[0]["country"].ToString());
if (ds2.Tables[0].Rows.Count > 0)
{
string state_name_value = ds.Tables[0].Rows[0]["BirthLocCol"].ToString();
ddlSates.SelectedValue = Convert.ToString(ddlStates.Items.FindByValue(state_name_value));
}
---
this code gives the following error:
ddlState has a SelectedValue which is invalid because it doesn't exist in the list of items.
Please guide me, where I am doing wrong to show the selected value in the dropdownlist.
Thank you!
You have to use below mentioned code for it, You are trying to convert object to string rather value of it.
ddlSates.SelectedValue = Convert.ToString(ddlStates.Items.FindByValue(state_name_value).Value);
I use MVC HTML Helper to render a Html DropDownList:
#Html.DropDownListFor(m => m.ParentId, menuSelectList)
I put this DropDownList in an HTML form. When press the button Submit in this form, a HTML Form Data will be created and sent in POST request. This data will contain key name ParentId with key value is DropDownList's selected value. How I can include DropDownList's selected text in this data with key name is ParentName.
1) You could use an hidden "ParentName" input, and populate it with the selected text (javascript / jQuery) when an change event is fired on your combo (DropDownList).
2) Just get the value from the key after submit (in the POST action), it should be possible (db query, for example).
3) Build your selectList in a way that the key contains key AND value (separated by a ~ for example, and split the "key~value" in the POST action)
I would personnaly go for second solution, but...
You can do it with JavaScript, as the browser is not sending the selected text anywhere.
One trick is placing such code in the very bottom of your .aspx page: (before the </body>)
var arrDDLs = document.getElementsByTagName("select");
for (var i = 0; i < arrDDLs.length; i++) {
var oDDL = arrDDLs[i];
if (!oDDL.name || oDDL.name.length === 0)
continue;
var inputName = oDDL.name + "_text";
var input = document.createElement("input");
input.type = "hidden";
input.name = inputName;
input.id = inputName;
oDDL.form.appendChild(input);
oDDL.onchange = function () {
var index = this.selectedIndex;
var text = (index >= 0) ? this.options[index].text : "";
var inputName = this.name + "_text";
var input = document.getElementById(inputName);
input.value = text;
};
//populate initial text:
oDDL.onchange();
}
This code is essentially implementing the first idea in this other answer and to take the selected text you just have to read Request value of the select key plus "_text" e.g.:
string selectedText = Request.Form[m.ParentId + "_text"];
Live test case. (JavaScript side only)
I have user submitted content that is loaded into c# winform in our office for processing before officially added to database. The user can submit a 'Referrer' as two text fields-first and last name. In the office I want to have a combobox will all existing referrers loaded in, then the first couple letters of the name to advance the combobox down to the area it needs to be at. I want to do something like this, taking the first two letters of the name and use that to initialize the combobox.
if (txtrefFirstName.TextLength > 2)
{
string firstStart = "" + txtrefFirstName.Text[0] + txtrefFirstName.Text[1];
firstStart = firstStart.ToUpper();
ddlReferring.SelectedText.StartsWith(firstStart);
}
else
ddlReferring.Text = "";
Any ideas or suggestions to get this to work?
Thanks
David K.
You could write something like this...
foreach (string item in ddlReferring.Items)
{
if (item.StartsWith(firstStart))
{
ddlReferring.SelectedText = item;
break;
}
}
Assuming the ddl's datasource is a List of String objects, you should be able to do some comparison on the datasource itself. I tend to use Linq for things like this but it isn't strictly necessary, just shorter.
if (txtrefFirstName.TextLength > 2)
{
string firstStart = txtrefFirstName.Text.Substring(0,2).ToUpper();
string Selection = ddlReferring.DataSource.Where(a=>a.StartsWith(firstStart)).FirstOrDefault();
ddlReferring.SelectedText = Selection ?? "";
}
else
ddlReferring.Text = "";
The selection line can also come from the items collection directly
string Selection = ddlReferring.Items.OfType<string>().Where(a=>a.StartsWith(firstStart)).FirstOrDefault();
Or if you REALLY dont want to use Linq...
string Selection = "";
foreach (object item in ddlReferring.Items)
if (item.ToString().StartsWith(firstStart))
{
Selection = item.ToString();
break;
}
Similar methods can be used even if the ddl's data is not a list of strings, just make sure to cast the items appropriately and compare the correct values.