5 Replies - 1724 Views - Last Post: 16 August 2014 - 01:39 PM Rate Topic: -----

#1 riverKhan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 11-August 14

ASP.net casting usercontrol text box to web control text box

Posted 14 August 2014 - 10:29 AM

0
down vote
favorite
i have got the following user control

<uc1:TextSearch runat="server" ID="tsClientName" HistoryQueryStringParameter="CN" LabelText="Client Name" />



i am trying to retrieve value of tsClientName field with the following code

TextBox txtbox = (TextBox)pnlAdvancedSearch.FindControl("tsClientName");
string var = txtbox.Text.ToString()



but i getting the below error

Unable to cast object of type 'ASP.usercontrols_gridviewsearch_textsearch_ascx' to type 'System.Web.UI.WebControls.TextBox'

the user control field got the following code

<asp:TextBox runat="server" ID="txt" MaxLength="20" CssClass="title" />


Is This A Good Question/Topic? 0
  • +

Replies To: ASP.net casting usercontrol text box to web control text box

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 13954
  • View blog
  • Posts: 55,702
  • Joined: 12-June 08

Re: ASP.net casting usercontrol text box to web control text box

Posted 14 August 2014 - 10:56 AM

Does the user control inherit from the asp.text box? What about does the user control have a property return the value of that textbox inside of it?
Was This Post Helpful? 0
  • +
  • -

#3 Nakor  Icon User is offline

  • Professional Lurker
  • member icon

Reputation: 448
  • View blog
  • Posts: 1,504
  • Joined: 28-April 09

Re: ASP.net casting usercontrol text box to web control text box

Posted 15 August 2014 - 04:40 AM

UserControls inherit from the UserControl class. You need to get a reference to the TextBox contained within that UserControl. One way you can do this is to make a publicly accessible property on the UserControl that returns an instance of the TextBox control.

Then you don't even have to mess with doing a FindControl on it.
Was This Post Helpful? 0
  • +
  • -

#4 riverKhan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 11-August 14

Re: ASP.net casting usercontrol text box to web control text box

Posted 15 August 2014 - 04:44 AM

View PostNakor, on 15 August 2014 - 04:40 AM, said:

UserControls inherit from the UserControl class. You need to get a reference to the TextBox contained within that UserControl. One way you can do this is to make a publicly accessible property on the UserControl that returns an instance of the TextBox control.

Then you don't even have to mess with doing a FindControl on it.


Thanks Both, the public property is there in the custom control, but some how i cant access the


  public String TextboxText
        {
            get { return txt.Text; }
            
        }


tsClientName.TextboxText.ToString()



i suspect its when the way this property is populated during the page load method in the child page. will look into that
Was This Post Helpful? 0
  • +
  • -

#5 Nakor  Icon User is offline

  • Professional Lurker
  • member icon

Reputation: 448
  • View blog
  • Posts: 1,504
  • Joined: 28-April 09

Re: ASP.net casting usercontrol text box to web control text box

Posted 15 August 2014 - 04:46 AM

But if you want to do it the way you have it now, you need to pass in the ID of your textbox which is "txt", not "tsClientName".

well, you wouldn't need to call ToString since the Text property is already a string. And during which event are you trying to access the control?
Was This Post Helpful? 0
  • +
  • -

#6 riverKhan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 11-August 14

Re: ASP.net casting usercontrol text box to web control text box

Posted 16 August 2014 - 01:39 PM

View PostNakor, on 15 August 2014 - 04:46 AM, said:

But if you want to do it the way you have it now, you need to pass in the ID of your textbox which is "txt", not "tsClientName".

well, you wouldn't need to call ToString since the Text property is already a string. And during which event are you trying to access the control?



Page Load.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1