System.Web.UI.WebControl.Textbox does not contain the definition of Text - c#

I need to get the text value from the textbox on the web form. In the documentation its mentioned that System.Web.UI.WebControl.Textbox has a property Text but I get an error when I try to use it.
string FieldName= "";
string FieldValue="";
if (inputValues[i] is System.Web.UI.WebControls.TextBox)
{
FieldName = inputValues[i].ClientID;
FieldValue = inputValues[i].Text; // error
}
The error shown is System.Web.UI.WebControl.Textbox does not contain the definition of Text. How do I get the text value from textbox control ?

Try casting inputValues[i] to a System.Web.UI.WebControl.Textbox. Then access the Text property:
FieldValue = ((System.Web.UI.WebControl.Textbox)(inputValues[i])).Text;

Related

cannot convert from 'System.Web.UI.WebControls.Label' to 'string'?

I want to insert some values from grid to db while clicking button submit. While using the given below code shows an error. The code is given below. Help me to find a proper solution.
Code:
protected void btnApprove_Click(object sender, EventArgs e)
{
ShadingAnalysisDataSetTableAdapters.tbl_ItemRequest_StatusTableAdapter rs;
rs = new ShadingAnalysisDataSetTableAdapters.tbl_ItemRequest_StatusTableAdapter();
var ItemName = (Label)GridView2.SelectedRow.Cells[1].FindControl("lblItem");
var Quantity = (Label)GridView2.SelectedRow.Cells[2].FindControl("lblQnty");
rs.testInsert(ItemName, Quantity);
}
I have modified my code based on the above suggestions. But now I am getting another error. The new error is: Object reference not set to an instance of an object.
By doing this
var ItemName = (Label)GridView2.SelectedRow.Cells[1].FindControl("lblItem");
ItemName would be a Label instead of the text value of lblItem. I would guess that both parameters of rs.testInsert method are string so you got the error because you're passing two Labels instead of two strings. You can get the text value from the .Text property of the label as below
var ItemName = ((Label)GridView2.SelectedRow.Cells[1].FindControl("lblItem")).Text;
var Quantity = ((Label)GridView2.SelectedRow.Cells[2].FindControl("lblQnty")).Text;
You are finding Label control and assigning to var ItemName and var Quantity.So You are getting error "cannot convert from 'System.Web.UI.WebControls.Label' to 'string'".
var ItemName = (Label)GridView2.SelectedRow.Cells[1].FindControl("lblItem");
var Quantity = (Label)GridView2.SelectedRow.Cells[2].FindControl("lblQnty");
So, Add Text property to label.
var ItemName = ((Label)GridView2.SelectedRow.Cells[1].FindControl("lblItem")).Text;
var Quantity = ((Label)GridView2.SelectedRow.Cells[2].FindControl("lblQnty")).Text;
var ItemName = (Label)GridView2.SelectedRow.Cells[1].FindControl("lblItem");
here you will get the label control on the ItemName and you can get the label text value on string variable as given below.
string Text_Value= ItemName.text;

How to get the value a form field which name and id is dynamic

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

C# Getting a dataset column name based upon a bound textbox

I am validating data in a textbox if nothing is entered
public bool IsPresent(TextBox textBox, string name)
{
if (textBox.Text == "")
{
MessageBox.Show(name + " is a required field.", "Entry Error");
textBox.Focus();
return false;
}
return true;
}
However, I want to pass in the name argument as the actual name from the dataset datacolumn that the textbox is bound to.
public bool isValidData(List<TextBox> textBoxList)
{
foreach (TextBox textBox in textBoxList)
{
//string name = techSupportDataSet.Technicians.NameColumn.ColumnName;
IsPresent(textBox, name);
}
return false;
}
You can see that the commented out code explicitly gets a specific datacolumn name, but I want it to be a variable depending on the bound textbox.
I've tried looking up the MSDN docs but I can't seem to find any methods that would accept a bound control as a parameter. Any points in the right direction would be much appreciated!
Displaying a field name in message box is not a good idea. for example if DBA has created a field FName for User's First Name then the message will be displayed like FName is a required field. This will confuse the operator. You can assign the Display Name in that control's AccessibleName or AccessibleDescription property and then you can use it in your code.
//Assign display name in AccessibleName property
textBox.AccessibleName= "User's First Name"
//use that field
IsPresent(textBox, textBox.AccessibleName);

ComboBox.SelectedText doesn't give me the SelectedText

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();

Get text value of input from FindControl

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;

Categories

Resources