if I have a datalist with textbox and a button inside and i want to access the value of the textbox and pass it to the listener of the button, anyone knows how to do that?
I know how.
Let's say you have a DataList name myDataList, and a TextBox in it named by myTextBox.
foreach (DataListItem item in myDataList.Items)
{
TextBox myTextBox = (TextBox)item.FindControl("myTextBox");
string text = myTextBox.text;
// Do whatever you need with that string value here
}
This loops through all of the items in your DataList and places the value of your TextBox into a local string variable named "text". From there, you can do whatever else needs to be done.
Related
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 am building a String and the code looks like
String status = "The status of my combobox is " + comboBoxTest.SelectedText
I am using WinForm in VS2010
The result looks like
"The status of my combobox is "
I think you want to use
String status = "The status of my combobox is " + comboBoxTest.Text
SelectedText property from MSDN
Gets or sets the text that is selected in the editable portion of a
ComboBox.
while Text property from MSDN
Gets or sets the text associated with this control.
From the documentation:
You can use the SelectedText property to retrieve or change the currently selected text in a ComboBox control. However, you should be aware that the selection can change automatically because of user interaction. For example, if you retrieve the SelectedText value in a button Click event handler, the value will be an empty string. This is because the selection is automatically cleared when the input focus moves from the combo box to the button.
When the combo box loses focus, the selection point moves to the beginning of the text and any selected text becomes unselected. In this case, getting the SelectedText property retrieves an empty string, and setting the SelectedText property adds the specified value to the beginning of the text.
I face this problem 5 minutes before.
I think that a solution (with visual studio 2005) is:
myString = comboBoxTest.GetItemText(comboBoxTest.SelectedItem);
Forgive me if I am wrong.
I think you dont need SelectedText but you may need
String status = "The status of my combobox is " + comboBoxTest.Text;
To get selected item, you have to use SELECTEDITEM property of comboBox. And since this is an Object, if you wanna assign it to a string, you have to convert it to string, by using ToString() method:
string myItem = comboBox1.SelectedItem.ToString(); //this does the trick
Try this:
String status = "The status of my combobox is " + comboBoxTest.text;
Here's how I would approach the problem, assuming you want to change the text of say, a label
private void comboBoxtest_SelectedIndexChanged(object sender, EventArgs e)
{
var combotext = comboBoxtest.Text;
var status = "The status of my combo box is" + combotext;
label1.Text = status;
}
All of the previous answers explain what the OP 'should' do. I am explaining what the .SelectedText property is.
The .SelectedText property is not the text in the combobox. It is the text that is highlighted. It is the same as .SelectedText property for a textbox.
The following picture shows that the .SelectedText property would be equal to "ort".
If you bind your Combobox to something like KeyValuePair, with properties in the constructor like so...:
DataSource = dataSource,
DisplayMember = "Value",
ValueMember = "Key"
so dataSource is of type KeyValuePair...
You end up with having to do this...
string v = ((KeyValuePair)((ComboBox)c).SelectedItem).Value;
(I had a Dynamic form - where c was of type Control - so had to cast it to ComboBox)
If you just want to know the text in the ComboBox with the editable text box (or the ComboBoxStyle.DropDown style) you can use this:
string str = comboBox.SelectedItem != null ?
comboBox.GetItemText(comboBox.SelectedItem) : comboBox.Text;
or try this code
String status = "The status of my combobox is " + comboBoxTest.SelectedItem.ToString();
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?