I want to get the value of disabled textbox control.
First I am finding the textbox
TextBox txt1 = (TextBox)(Page.FindControl("txt1"))
Then saving the value of the textbox
decimal val1 = Convert.ToDecimal(txt1.Text.Trim().ToString())
But I am not getting any value because it is not able to find the control because my Textbox control is disabled.
Thanks in advance
TextBox might be nested inside other control. If so, you want to find it recursively.
For example,
Helper Method
public static Control FindControlRecursive(Control root, string id)
{
if (root.ID == id)
return root;
return root.Controls.Cast<Control>()
.Select(c => FindControlRecursive(c, id))
.FirstOrDefault(c => c != null);
}
Usage
var txt1 = (TextBox)FindControlRecursive(Page, "txt1");
A few more details would be helpful. It should be as simple as:
string val1 = txt1.text;
You shouldn't even need to use FindControl unless there is a specific reason.
Are you sure you are getting the textbox in txt1
also
string val1 = Convert.ToDecimal(txt1.Text.Trim().ToString())
Your code is wrong.. how can you convert a decimal into a string
I'm assuming that you have a numeric value in your textbox, so try doing it like this:
decimal value = 0;
string money = String.Empty;
if(!string.IsNullOrEmpty(txtMoney.Text))
name = txtMoney.Text
value = Convert.ToDecimal(name);
That will validate your textbox to avoid a null, but will also convert it into a decimal value for it. Hope it helps.
You shouldn't need to FindControl before you can assign the value, you should be able to pass values in and out, as long as your markup has runat="server". The server side postback will provide access to the inner content of the control without a problem.
After testing, a disabled control won't push the value during a postback. You really should use the HiddenField.
However, a <asp:HiddenField> may be better suited to your requirements. Rather than a disabled textbox. Which would like like this:
string cash = String.Empty;
if(!string.IsNullOrEmpty(hfMoney.Value))
cash = hfMoney.Value;
The hidden field will always be hidden, thus it is always accessible.
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
I'm new with Sharepoint 2013 AND .NET c# so take it easy guys.
I'm writing a webpart which generates input forms dynamically. Each form may have different fields so field names/IDs are defined on runtime.
This is the code I generate a input text. fieldname is my variable here which is the ID and preferably the name of the input box. fieldtitle is the display name and fielddefaultvalue is the default value. This code part is in a loop and adds all inputs into PlaceHolder1 every time.
TextBox formfield = new TextBox();
formfield.ID = fieldname;
formfield.Text = fielddefaultvalue;
formfield.Attributes.Add("placeholder", fieldtitle);
PlaceHolder1.Controls.Add(formfield);
What my problem is, when I run this, name and ID of my input fields become like
ctl00$ctl39$g_76a6fb01_d2b4_4087_a988_8b34b95dc136$Email
I tried to get submitted values using
Page.Request.Form["Email"]
but no success. I guess it needs all those random characters too.
I could also use Email.Text to get value but Email part is dynamic and comes from a variable. Is there a way to use variable fieldname which contains Email and other field names as string?
How can I get submitted values and/or put clean names to my input fields.
Remember my field names are dynamic and kept in fieldname variable every round of the for loop.
Edit
After I submit form, this is the debug screen of Page.Request.Form . I don't know how/where I can find that random/unique string at the beginning of each input field. flexiforms_xxx part is the actual filed name.
You could cache the TextBoxes in a dictionary when adding them:
Dictionary<string, TextBox> textBoxesByFieldName = new Dictionary<string, TextBox>();
foreach(string fieldName in FieldNames){
TextBox formfield = new TextBox();
formfield.ID = fieldname;
formfield.Text = fielddefaultvalue;
formfield.Attributes.Add("placeholder", fieldtitle);
PlaceHolder1.Controls.Add(formfield);
textBoxesByFieldName.Add(fieldname, formfield);
}
And when you need them access the TextBox this way:
private TextBox GetTextBoxByFieldName(string fieldName){
TextBox textBox;
return textBoxesByFieldName.TryGetValue(fieldName, out textBox) ? textBox : null;
}
[edit] If I understand correctly you want to submit the form and get all the entered text for your clean field names:
void OnSubmitButtonClick(Object sender, EventArgs e)
{
foreach(KeyValuePair<string, TextBox> keyValue in textBoxesByFieldName){
string fieldName = keyValue.Key;
string fieldValue = keyValue.Value.Text;
}
}
[edit2] Above solution requires formfield.AutoPostBack=true; to be set
To avoid page refreshes, use string fieldValue = Page.Request.Form[((System.Web.UI.Control)(keyValue.Value)).UniqueID];
Have you tried: formfield.ClientId
Request.Form[...] requires the name or index of the control, so set the Name of the control
formfield.Name = fieldname;
http://msdn.microsoft.com/en-us/library/ms525985(v=vs.90).aspx
I had a databound combobox in my windows form I populate it by a function deptload() IN FORM LOAD
public void DeptcomboLoad()
{
DataTable dt = depttrans.getDeptName();
Cmb_Department.DataSource = dt;
Cmb_Department.DisplayMember = "DepartmentName"; //CHAR
Cmb_Department.ValueMember = "DepartmentPK"; //INT
}
Now when an employee of a department (say accounts DepartmentName="Accounts " , DepartmentPK=23 ) login I want the ComboBox text to be selected as "acounts "
and when I go to get the selected value of the ComboBox I should get 23
I tried
Cmb_Department.selectedtext="Accounts"
Cmb_Department.Text="Accounts"
but its not giving the selected value
Can anyone give a suggestion
Instead of trying to put a value INTO the combobox, try to GET the SelectedItem like this:
string txt= Cmb_Department.SelectedItem.Text
or just:
string txt= Cmb_Department.SelectedText
To change selected value of the combobox you can use
SelectedItem property or SelectedIndex.
Index must be exact number in your data sourse, and Item must be exact object from datasource
You can get it to select the right item by issuing something like this:
Cmb_Department.SelectedValue = 23;
Where 23 comes from some other variable, maybe on another object, maybe from a local variable, whatever works in your case.
Now, to get the selected value you can use this statement:
var val = Cmb_Department.SelectedValue;
To get the selected text (which would be the text associated with the value):
var text = ((DataRow)Cmb_Department.SelectedItem)["DepartmentName"];
The reason I'm prescribing the aforementioned is because the SelectedText property is volatile, and the Text property doesn't always work based on how the DropDownStyle is set.
However, some would probably argue to get the same as the aforementioned you could issue this statement:
var text = Cmb_Department.Text;
I know now normally you can get the value of a text input using the following:
txtName.Text
But because my input is inside of a LoginView I am using FindControl like this:
LoginView1.FindControl("txtComment")
This successfully find the text input but returns its type rather than the value. Adding the Text function at the end does not work.
Try casting that Control to TextBox. FindControl returns a Control which doesn't have the Text property
TextBox txtName = LoginView1.FindControl("txtComment") as TextBox;
if (txtName != null)
{
return txtName.Value;
}
It has been a while since I used the controls, but i believe it is:
string text = ((TextBox)LoginView1.FindControl("txtComment")).Text;
I am creating a TextBox with the following code:
TextBox textBox = new TextBox();
textBox.Name = propertyName;
textBox.Text = value;
textBox.Width = FormControlColumnWidth;
textBox.SetResourceReference(Control.StyleProperty, "FormTextBoxStyle");
sp.Children.Add(textBox); //StackPanel
FormBase.Children.Add(sp);
On a button click, I want to get the text value of that text box, but I can't specify in code:
string firstName = FirstName.Text;
since "FirstName" will be defined at runtime. So how do I get the text value of the Textbox without knowing the name of the textbox at compile time?
The following is what I have so far but it says that it can't find "FirstName" even though it gets defined at runtime:
private void Button_Save(object sender, RoutedEventArgs e)
{
using (var db = Datasource.GetContext())
{
var item = (from i in db.Customers
where i.Id == TheId
select i).SingleOrDefault();
item.FirstName = ((TextBox)FindResource("FirstName")).Text;
db.SubmitChanges();
}
}
REPRODUCABLE EXAMPLE:
I posted a full reproducable example of this problem here: Why can't I access a TextBox by Name with FindName()?, perhaps easier to analyze.
The simplest solution would probably to keep a reference to your textbox somewhere in your code. Just add a
private TextBox _textbox
at the top of your class and set it to the TextBox you add in your code. Then you can refer to it in your Button_Save event handler.
You can retrieve it like this:
TextBox tb=(TextBox)Children.First(w=>w.Name=="FirstName");
Not sure what that sp in your code is, but if you really need the 2nd level of controls, you could run a foreach loop over the first level then search by name on the second level.
The answer to this question is you have to use this.RegisterName("FirstName", textBox); which is explained here: Why can't I access a TextBox by Name with FindName()?
You can find any element using FindName:
var c = (FrameworkElement)this.FindName("somename");
I can't write comments, so this is as a reply to your comment.
Why not use a
Dictionary<string, TextBox>
as a class property?
that way you can keep references to an indefinite number of textbox instances in the class AND access them easily by name in the dictionary?