I've been looking around a few forums and found a common problem but can't find the answer to it, so I thought I'd try here.
Basically I have a form, FormView1 with a Text box and a button amongst other controls. When the button is pressed I would like my back end C# to get the text from the textbox and assign it to a string for example. Here's what I thought could work, but didnt...
Without seeing the code for your FormView, it's tough to say what's going on. However, could this be an issue with data-binding? How are you data-binding your FormView? If you are doing this programmatically, are you making sure to only do it on the initial
page load? Doing so on each and every postbback could overwrite any entries you make within the TextBox.
Make sure the highlighted - underlined (control name) is correct as appears in your ASPX file. Debug the program and see if the
txt1 is not null. Otherwise it should work fine.
TextBox txb1 = FormView1.FindControl("txb1") as TextBox;
Cheers,
Achutha Krishnan
Marked as answer by Bluebird1984 on Nov 20, 2008 02:48 PM
I don't want my form to be in insert mode, I either want it as ReadOnly with one Editable Text box or in Edit mode so I can just edit the one text box and then get its value. I've tried getting the value with the form in all 3 modes with no luck. any ideas?
Without seeing the code for your FormView, it's tough to say what's going on. However, could this be an issue with data-binding? How are you data-binding your FormView? If you are doing this programmatically, are you making sure to only do it on the initial
page load? Doing so on each and every postbback could overwrite any entries you make within the TextBox.
which settings do I need to change so the Label or string gets the value of the Textbox BEFORE it gets posted and cleared?
You never stated how you are data-binding your FormView. If you are data-binding your FormView programmatically, then it is important that you only do so within a Not IsPostBack condition. This will prevent the data from being bound each and every postback.
Make sure the highlighted - underlined (control name) is correct as appears in your ASPX file. Debug the program and see if the
txt1 is not null. Otherwise it should work fine.
TextBox txb1 = FormView1.FindControl("txb1") as TextBox;
I ignored this advice when I first read it because I thought I'd never make a mistake like that,but you were right!!!
Bluebird1984
Member
44 Points
155 Posts
Get FormView textbox value
Nov 20, 2008 11:46 AM|LINK
hi,
I've been looking around a few forums and found a common problem but can't find the answer to it, so I thought I'd try here.
Basically I have a form, FormView1 with a Text box and a button amongst other controls. When the button is pressed I would like my back end C# to get the text from the textbox and assign it to a string for example. Here's what I thought could work, but didnt...
protected void btnReject_Click(object sender, EventArgs e){
TextBox txb1 = FormView1.FindControl("txb1") as TextBox; string test = txb1.Text;}
the value of txb1.Text is Null.
cheers
ecbruck
All-Star
98783 Points
9691 Posts
Moderator
Re: Get FormView textbox value
Nov 20, 2008 01:08 PM|LINK
Without seeing the code for your FormView, it's tough to say what's going on. However, could this be an issue with data-binding? How are you data-binding your FormView? If you are doing this programmatically, are you making sure to only do it on the initial page load? Doing so on each and every postbback could overwrite any entries you make within the TextBox.
Microsoft MVP - ASP.NET
engineerachu
Contributor
3077 Points
850 Posts
Re: Get FormView textbox value
Nov 20, 2008 01:29 PM|LINK
Make sure the highlighted - underlined (control name) is correct as appears in your ASPX file. Debug the program and see if the txt1 is not null. Otherwise it should work fine.
TextBox txb1 = FormView1.FindControl("txb1") as TextBox;Achutha Krishnan
bayonian
Member
37 Points
46 Posts
Re: Get FormView textbox value
Nov 20, 2008 01:58 PM|LINK
Hi,
First, you need to make sure that your FormView is in Insert mode. Try the follwing code :
protected void FormView1_ItemCommand(object sender, FormViewCommandEventArgs e) { TextBox DemoTexbox = (TextBox)FormView1.FindControl("TextBox1"); String demoText = DemoTexbox.Text; Label1.Text = demoText; }The property of your FormView should be something like this :
Cheer.
Bluebird1984
Member
44 Points
155 Posts
Re: Get FormView textbox value
Nov 20, 2008 02:41 PM|LINK
I don't want my form to be in insert mode, I either want it as ReadOnly with one Editable Text box or in Edit mode so I can just edit the one text box and then get its value. I've tried getting the value with the form in all 3 modes with no luck. any ideas?
Bluebird1984
Member
44 Points
155 Posts
Re: Get FormView textbox value
Nov 20, 2008 02:43 PM|LINK
which settings do I need to change so the Label or string gets the value of the Textbox BEFORE it gets posted and cleared?
thanks
ecbruck
All-Star
98783 Points
9691 Posts
Moderator
Re: Get FormView textbox value
Nov 20, 2008 02:45 PM|LINK
You never stated how you are data-binding your FormView. If you are data-binding your FormView programmatically, then it is important that you only do so within a Not IsPostBack condition. This will prevent the data from being bound each and every postback.
Microsoft MVP - ASP.NET
Bluebird1984
Member
44 Points
155 Posts
Re: Get FormView textbox value
Nov 20, 2008 02:47 PM|LINK
I ignored this advice when I first read it because I thought I'd never make a mistake like that,but you were right!!!
the control was called tb instead of txb
many thanks for your help!!
ryafel
Member
615 Points
405 Posts
Re: Get FormView textbox value
Feb 13, 2009 05:43 AM|LINK
hi. is there any way I can do this in a details view?
Ryan Felton
Cape Town, South Africa
Great programmers are not born. They are compiled