ClientID messes with my textbox - please help
-
Hi everybody. Does anybody even IMAGINE why writing this in a RegisterStartupScript block would cause my textbox to lose its value upon postback???
Page.ClientScript.RegisterStartupScript(GetType(),"document.getElementById('" + txtp.ClientID + "').value; "...... etc
txtp.Text = "-1";Let me say that one more time. I am reffering to the ClientID of "txtp" in a RegisterStartupScript. I merely take the value from it, that's all. And when the page retuns, txtp always has its default value, the one I populate in it, see the next line, when I set -1. But if I comment out that line with the .ClientID, then when the pages posts back, after I repopulate, even if I set -1 to the textbox, I DO GET THE CORRECT number in it, which was entered client-side, before the user presses the submit button. With .Client ID line uncommented: 1. Load dynamic user control in which txtp is. 2. Set -1, register startup script 3. User writes 2 in the textbox 4. Submit 5. Value in textbox is -1. WRONG With .Client ID line commented: 1. Load dynamic user control in which txtp is. 2. Set -1, register startup script 3. User writes 2 in the textbox 4. Submit 5. Value in textbox is 2. CORRECT Somebody please help me out here....... I'm way over my head, I don't understand a bit of what is happening, even though I have studied the lifetime of the page and I am loading the control dinamically at the master's page Init. But even if I messed something up in the lifetime, THIS STILL DOESN'T explain why a silly .Client ID messes everything up! Weird!
-= E C H Y S T T A S =- The Greater Mind Balance Blending C++ with COM ^ Have a break on my website! Spread the word? Please?
-
Hi everybody. Does anybody even IMAGINE why writing this in a RegisterStartupScript block would cause my textbox to lose its value upon postback???
Page.ClientScript.RegisterStartupScript(GetType(),"document.getElementById('" + txtp.ClientID + "').value; "...... etc
txtp.Text = "-1";Let me say that one more time. I am reffering to the ClientID of "txtp" in a RegisterStartupScript. I merely take the value from it, that's all. And when the page retuns, txtp always has its default value, the one I populate in it, see the next line, when I set -1. But if I comment out that line with the .ClientID, then when the pages posts back, after I repopulate, even if I set -1 to the textbox, I DO GET THE CORRECT number in it, which was entered client-side, before the user presses the submit button. With .Client ID line uncommented: 1. Load dynamic user control in which txtp is. 2. Set -1, register startup script 3. User writes 2 in the textbox 4. Submit 5. Value in textbox is -1. WRONG With .Client ID line commented: 1. Load dynamic user control in which txtp is. 2. Set -1, register startup script 3. User writes 2 in the textbox 4. Submit 5. Value in textbox is 2. CORRECT Somebody please help me out here....... I'm way over my head, I don't understand a bit of what is happening, even though I have studied the lifetime of the page and I am loading the control dinamically at the master's page Init. But even if I messed something up in the lifetime, THIS STILL DOESN'T explain why a silly .Client ID messes everything up! Weird!
-= E C H Y S T T A S =- The Greater Mind Balance Blending C++ with COM ^ Have a break on my website! Spread the word? Please?
Are you sure of this. I did this myself
this.ClientScript.RegisterStartupScript(typeof(Page),"scriptblock", "var x = document.getElementById('" + txtp.ClientID + "').value; ");
txtp.Text = "-1";and found 2 on txtp.Text when form is posted back. I think the problem is every time you post back the form it resets the
txtp.Text
to-1
as you wrote this inPage_Load
event I think. So please writeIf(IsPostBack)return;
at the beginning, I think it will work properly.. :rose::rose:Abhishek Sur
My Latest Articles **Create CLR objects in SQL Server 2005 C# Uncommon Keywords Read/Write Excel using OleDB
**Don't forget to click "Good Answer" if you like to.
-
Are you sure of this. I did this myself
this.ClientScript.RegisterStartupScript(typeof(Page),"scriptblock", "var x = document.getElementById('" + txtp.ClientID + "').value; ");
txtp.Text = "-1";and found 2 on txtp.Text when form is posted back. I think the problem is every time you post back the form it resets the
txtp.Text
to-1
as you wrote this inPage_Load
event I think. So please writeIf(IsPostBack)return;
at the beginning, I think it will work properly.. :rose::rose:Abhishek Sur
My Latest Articles **Create CLR objects in SQL Server 2005 C# Uncommon Keywords Read/Write Excel using OleDB
**Don't forget to click "Good Answer" if you like to.
Hi and thanks for answering Abhishek! No, this is not an IsPostBack problem. I'm not a beginner. Simply uncommenting that line will make the value reach the server again! That's the weird thing. And it works for you perhaps because you don't have that textbox in a dinamically created User Control. What I'm wondering is WHY EXACTLY uncommenting that shitty line makes everything work! I can't believe it!
-= E C H Y S T T A S =- The Greater Mind Balance Blending C++ with COM ^ Have a break on my website! Spread the word? Please?
-
Hi and thanks for answering Abhishek! No, this is not an IsPostBack problem. I'm not a beginner. Simply uncommenting that line will make the value reach the server again! That's the weird thing. And it works for you perhaps because you don't have that textbox in a dinamically created User Control. What I'm wondering is WHY EXACTLY uncommenting that shitty line makes everything work! I can't believe it!
-= E C H Y S T T A S =- The Greater Mind Balance Blending C++ with COM ^ Have a break on my website! Spread the word? Please?
Ya.. Is it so.. This is really weird. Love to see the exact scenario. If possible can you share the code with me. We can also place a bug report to Microsoft about that if it really happens... :rose::rose:
Abhishek Sur
My Latest Articles **Create CLR objects in SQL Server 2005 C# Uncommon Keywords Read/Write Excel using OleDB
**Don't forget to click "Good Answer" if you like to.