473,320 Members | 1,884 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Access the value of a textbox from a custom control after postback

Hello Everyone, I created a custom control. On the
CreateChildControls, I added a textbox to the control as follows:

// TextBox
TextBox txtValue = new TextBox();
txtValue.ID = "txtValue";

protected override void CreateChildControls()
{
// Add the control to the form
Controls.Add(txtValue);

}

Now during postback, I am trying to access the value of the control:

// Property inside custom control that is being called from the
webform
public bool isValidMatch()
{
if (txtValue.Text == "test")
{
return true;
}
else
{
return false;
}
}

The isValidMatch method is being called from the webform during
postback. However the value of the textbox is blank no matter what
the user enters in the webform. I must be missing something in the
postback logic configuration of custom controls but cannot figure out
what the problem is and why is the textbox value always blank.

Thanks Before Hand,
Adiel
Sep 17 '08 #1
7 3047
You might want to check into creating a custom class and use the
IPostBackDataHandler interface. I use something simliar for an int
value normally in drop downs...

You can have a property on the control like:

public int PostedValue
{
get { return postedValue; }
}
private int postedValue = -1;

Your interface methods might look something like:

bool IPostBackDataHandler.LoadPostData(string postDataKey,
System.Collections.Specialized.NameValueCollection postCollection)
{
int presentValue = SelectedIndex;
postedValue = int.Parse(postCollection[postDataKey]);

if (!presentValue.Equals(postedValue))
{
return true;
}
return false;
}

void IPostBackDataHandler.RaisePostDataChangedEvent()
{
OnSelectedIndexChanged(EventArgs.Empty);
}

I'm sure this can point you in a direction at least
On Sep 17, 9:13*am, adie...@hotmail.com wrote:
Hello Everyone, I created a custom control. *On the
CreateChildControls, I added a textbox to the control as follows:

*// TextBox
TextBox txtValue = new TextBox();
txtValue.ID = "txtValue";

protected override void CreateChildControls()
* * * * {
* * * * * * // Add the control to the form
* * * * * * Controls.Add(txtValue);

* * * * }

Now during postback, I am trying to access the value of the control:

// Property inside custom control that is being called from the
webform
public bool isValidMatch()
* * * * {
* * * * * * if (txtValue.Text == "test")
* * * * * * {
* * * * * * * * return true;
* * * * * * }
* * * * * * else
* * * * * * {
* * * * * * * * return false;
* * * * * * }
* * * * }

The isValidMatch method is being called from the webform during
postback. *However the value of the textbox is blank no matter what
the user enters in the webform. *I must be missing something in the
postback logic configuration of custom controls but cannot figure out
what the problem is and why is the textbox value always blank.

Thanks Before Hand,
Adiel
Sep 17 '08 #2
Thank you for your help. This is what I have done now. I have added
the two methods you mentioned to the custom control:

bool IPostBackDataHandler.LoadPostData(string postDataKey,

System.Collections.Specialized.NameValueCollection postCollection)
{
//int presentValue = SelectedIndex;
string postedValue = postCollection[postDataKey];

//if (!presentValue.Equals(postedValue))
//{
// return true;
//}
return false;
}
void IPostBackDataHandler.RaisePostDataChangedEvent()
{
//OnSelectedIndexChanged(EventArgs.Empty);
}
I then set a breakpoint on both methods and ran the test webform.
When I hit submit on the test webform, none of the two methods above
where called. It looks like I must still be missing something?

Thanks Again,
Adiel
Sep 17 '08 #3
Sorry meant to reply to post not just you...

Would you post what I sent you?

Thanks

On Sep 17, 12:21*pm, adie...@hotmail.com wrote:
Thank you for your help. *This is what I have done now. *I have added
the two methods you mentioned to the custom control:

* * * * bool IPostBackDataHandler.LoadPostData(string postDataKey,

System.Collections.Specialized.NameValueCollection postCollection)
* * * * {
* * * * * * //int presentValue = SelectedIndex;
* * * * * * string postedValue = postCollection[postDataKey];

* * * * * * //if (!presentValue.Equals(postedValue))
* * * * * * //{
* * * * * * // * *return true;
* * * * * * //}
* * * * * * return false;
* * * * }

* * * * void IPostBackDataHandler.RaisePostDataChangedEvent()
* * * * {
* * * * * * //OnSelectedIndexChanged(EventArgs.Empty);
* * * * }

I then set a breakpoint on both methods and ran the test webform.
When I hit submit on the test webform, none of the two methods above
where called. *It looks like I must still be missing something?

Thanks Again,
Adiel
Sep 17 '08 #4
Also... the comment I made about setting AutoPostBack is not
correct... when using the TextBox and the Submit you don't want to set
that otherwise the post back will happen when you leave the control...
since you want to wait for the Submit event do not set AutoPostBack
true... which should then fire the event for the text box upon submit.

Sorry bout that.

On Sep 17, 12:39*pm, Harv <harve...@gmail.comwrote:
Sorry meant to reply to post not just you...

Would you post what I sent you?

Thanks

On Sep 17, 12:21*pm, adie...@hotmail.com wrote:
Thank you for your help. *This is what I have done now. *I have added
the two methods you mentioned to the custom control:
* * * * bool IPostBackDataHandler.LoadPostData(string postDataKey,
System.Collections.Specialized.NameValueCollection postCollection)
* * * * {
* * * * * * //int presentValue = SelectedIndex;
* * * * * * string postedValue = postCollection[postDataKey];
* * * * * * //if (!presentValue.Equals(postedValue))
* * * * * * //{
* * * * * * // * *return true;
* * * * * * //}
* * * * * * return false;
* * * * }
* * * * void IPostBackDataHandler.RaisePostDataChangedEvent()
* * * * {
* * * * * * //OnSelectedIndexChanged(EventArgs.Empty);
* * * * }
I then set a breakpoint on both methods and ran the test webform.
When I hit submit on the test webform, none of the two methods above
where called. *It looks like I must still be missing something?
Thanks Again,
Adiel- Hide quoted text -

- Show quoted text -
Sep 17 '08 #5
Not sure if he will post so I will just put basically what I said in
case someone else has this problem.

void IPostBackDataHandler.RaisePostDataChangedEvent()
{
//OnSelectedIndexChanged(EventArgs.Empty);
}

Needs to be

void IPostBackDataHandler.RaisePostDataChangedEvent()
{
OnTextChanged(EventArgs.Empty);
}

And of course the TextChanged event will need to be implemented where
ever you have the control.

On Sep 17, 12:43*pm, Harv <harve...@gmail.comwrote:
Also... the comment I made about setting AutoPostBack is not
correct... when using the TextBox and the Submit you don't want to set
that otherwise the post back will happen when you leave the control...
since you want to wait for the Submit event do not set AutoPostBack
true... which should then fire the event for the text box upon submit.

Sorry bout that.

On Sep 17, 12:39*pm, Harv <harve...@gmail.comwrote:
Sorry meant to reply to post not just you...
Would you post what I sent you?
Thanks
On Sep 17, 12:21*pm, adie...@hotmail.com wrote:
Thank you for your help. *This is what I have done now. *I have added
the two methods you mentioned to the custom control:
* * * * bool IPostBackDataHandler.LoadPostData(string postDataKey,
System.Collections.Specialized.NameValueCollection postCollection)
* * * * {
* * * * * * //int presentValue = SelectedIndex;
* * * * * * string postedValue = postCollection[postDataKey];
* * * * * * //if (!presentValue.Equals(postedValue))
* * * * * * //{
* * * * * * // * *return true;
* * * * * * //}
* * * * * * return false;
* * * * }
* * * * void IPostBackDataHandler.RaisePostDataChangedEvent()
* * * * {
* * * * * * //OnSelectedIndexChanged(EventArgs.Empty);
* * * * }
I then set a breakpoint on both methods and ran the test webform.
When I hit submit on the test webform, none of the two methods above
where called. *It looks like I must still be missing something?
Thanks Again,
Adiel- Hide quoted text -
- Show quoted text -- Hide quoted text -

- Show quoted text -
Sep 17 '08 #6
Thanks Harv, here is an update:

I added this to the custom control:

void IPostBackDataHandler.RaisePostDataChangedEvent()
{
OnTextChanged(EventArgs.Empty);
}

Then I receive an error "The name 'OnTextChanged' does not exist in
the current context.

Thanks,
Adiel
Sep 18 '08 #7
Your class looks something like:

public class SomeTextBox : System.Web.UI.WebControls.TextBox,
System.Web.UI.IPostBackDataHandler
{
public string PostedValue
{
get { return postedValue; }
}
private string postedValue = string.Empty;

public SomeTextBox() { }

bool IPostBackDataHandler.LoadPostData(string postDataKey,
System.Collections.Specialized.NameValueCollection postCollection)
{
string presentValue = this.Text;
postedValue = postCollection[postDataKey];

if (!presentValue.Equals(postedValue))
{
return true;
}

return false;
}

void IPostBackDataHandler.RaisePostDataChangedEvent()
{
OnTextChanged(EventArgs.Empty);
}
}

Make sure you have all your references of course if you are using the
"using" instead of spelling out the path...

On Sep 18, 8:02*am, adie...@hotmail.com wrote:
Thanks Harv, here is an update:

I added this to the custom control:

* * * * void IPostBackDataHandler.RaisePostDataChangedEvent()
* * * * {
* * * * * * OnTextChanged(EventArgs.Empty);
* * * * }

Then I receive an error "The name 'OnTextChanged' does not exist in
the current context.

Thanks,
Adiel
Sep 19 '08 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Ashish | last post by:
Hi All, I have a server runnable textbox control. I also have a server side button object but I hook up javascript to it to show a modal dialog. Upon return it places the value in the textbox...
9
by: Anders K. Jacobsen [DK] | last post by:
Hi I have this that adds some usercontrol (UCTodays.ascx) to a placeholder foreach(A a in B){ UCTodays ucline = (UCTodays )LoadControl("UCTodays.ascx");...
1
by: guoqi zheng | last post by:
Dear sir, I have a textbox inside a repeart control, autopostback set to true for that textbox. When the value of this textbox changes, I need to access current row and update database. ...
1
by: Sanjay Pais | last post by:
I built a custom control for all the basic web.ui.controls like textbox, label, checkbox etc etc. I added my custom attribute called ApplySecurity to the html in the page. However, when I cycle...
0
by: datakix | last post by:
After 16 hours of frustration, I've managed to solve this problem for a project I'm working on. The 'trick' is set EnableViewState="False" for the asp:textbox inside the Repeater control. The...
0
by: Paul | last post by:
Hi all, I have a simple custom control with a label and a text box. The text control's value is persisting when I do a post back, so the view state is working, but I cannot access the value in...
2
by: paulcis | last post by:
I am trying to produce a dynamic form from a database with unknown amount of records. I need to read the values and create a new textbox for each. I need to create the textboxes at page_init stage...
7
by: Andy B | last post by:
I have a class I am creating for data access. I need to access controls from inside the class that are on a particular page. How do I do this? or is creating an instance of the page class and using...
4
by: =?Utf-8?B?RHlsYW5TbWl0aA==?= | last post by:
I have a WebForm where I'm dynamically creating some controls and I'm having difficulty understanding how the state is being persisted and how to work with it. I've created a simplified example...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.