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


A

adiel_g

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
 
Ad

Advertisements

H

Harv

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
 
A

adiel_g

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
 
H

Harv

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.

Sorry meant to reply to post not just you...

Would you post what I sent you?

Thanks

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 -
 
H

Harv

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.

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.

Sorry meant to reply to post not just you...
Would you post what I sent you?

On Sep 17, 12:21 pm, (e-mail address removed) 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 -
 
Ad

Advertisements

A

adiel_g

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
 
Ad

Advertisements

H

Harv

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...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top