Showing posts with label control. Show all posts
Showing posts with label control. Show all posts

Saturday, March 31, 2012

String to Control.ClientId

Hi.
Is there a way I can convert a string (let's say it's"vw501") sent by a querystring, to a Control's ClientID, like myMultiView.SetActiceView(vw501) ?

I've tried this, which fails:
Dim vName As String = TRim(Request.Querystring("vName"))
myMultiView.SetActiveView(vName)

Regards,

Roy

The FindControl method might help you there. Seehttp://msdn2.microsoft.com/de-de/library/system.web.ui.control.findcontrol.aspx

Make sure you use the FindControl method on the Control your views are nested in. Say if your Views are inside a Form named form1, like so:

myMultiView.SetActiveView( form1.FindControl(vName) )


Thanks a lot :-)
It works!

Roy

string to control name

I am looking for the way to convert a string, say it's called sTemp, to the actual control name.
I have say cMyControl as Control.
I want to set cMyControl = sTemp
I can't seem to find the right way to convert the string.

Thanks all,

ZathHi Zath,
You are trying to equal a string to a type Control. Which does not make sense. Its like equalling a line of text to your monitor. A Control has so many properties, like control ID, child controls, Parent and blah blah. So now, what you are trying to achieve and why
Thanks for the reply.

I have 4 imagas on a page. Depending on the number in the db field, I want the number of images to be visible.


'For i = 1 To intCnt 'intCnt is the number in the field
' sTmp = ("imgTest" & i) 'For the image names - imgTest1, imgTest2......
' j = CType(sTmp, Control) ' This line doesn't work
' j.Visible = True
'Next

Now it can be done with some if then statements, but if I ever increase the number above 4 in the db, then I will have to adjust the code.
And all the images are the same.
If there is a better way to do this, any suggestions are appreciated.

Zath

For i = 1 To intCnt
j = FindControl("imgTest" + i.ToString())

If Not j Is Nothing Then
j.Visible = True
End If
Next


Cool, thanks! Worked great.

Should have thought of that. I use the findcontrol for datagrid templates all the time....

Zath

Wednesday, March 28, 2012

String with double quotes

I have a label control on my form. I'm assigning a text to the label at
runtime.
The problem is that the string that I'm trying to assign also contains text
in double quotes.So when I try n assign this to the label I face a problem.
my code is using vb.net

Please help me out in getting this thing done, Is there any in-built
function or property to handle the same?

thanx
GaneshAre you getting any specific error messages?

You may need to encode your string using System.Web.HttpUtilites.HtmlEncode,
before putting it into your label.

Mun

--
Munsifali Rashid
http://www.munit.co.uk/blog/

"Ganesh" <Ganesh@.discussions.microsoft.com> wrote in message
news:AE4E9B39-B05A-4C1C-BFEC-F8AABE008407@.microsoft.com...
>I have a label control on my form. I'm assigning a text to the label at
> runtime.
> The problem is that the string that I'm trying to assign also contains
> text
> in double quotes.So when I try n assign this to the label I face a
> problem.
> my code is using vb.net
> Please help me out in getting this thing done, Is there any in-built
> function or property to handle the same?
> thanx
> Ganesh
All you need to do is use "double double" quotes (a term I just made up):

For example:

lblMain.text = """Hello World """

will show on your screen as:

"Hello World"

lblMain.text = """" + "Hello World" + """"

will produce the same result!

Hope this helps you!

Fred

"Ganesh" <Ganesh@.discussions.microsoft.com> wrote in message
news:AE4E9B39-B05A-4C1C-BFEC-F8AABE008407@.microsoft.com...
> I have a label control on my form. I'm assigning a text to the label at
> runtime.
> The problem is that the string that I'm trying to assign also contains
text
> in double quotes.So when I try n assign this to the label I face a
problem.
> my code is using vb.net
> Please help me out in getting this thing done, Is there any in-built
> function or property to handle the same?
> thanx
> Ganesh
Your best bet would probably be to HTML-encode the string. e.g.:

someLabel.Text = Server.HtmlEncode(yourString)

This will replace the quotes with " named entity, as well as escaping
other HTML that might potentially cause problems (e.g.: unexpected page
modifications or cross-site scripting).

"Ganesh" <Ganesh@.discussions.microsoft.com> wrote in message
news:AE4E9B39-B05A-4C1C-BFEC-F8AABE008407@.microsoft.com...
>I have a label control on my form. I'm assigning a text to the label at
> runtime.
> The problem is that the string that I'm trying to assign also contains
> text
> in double quotes.So when I try n assign this to the label I face a
> problem.
> my code is using vb.net
> Please help me out in getting this thing done, Is there any in-built
> function or property to handle the same?
> thanx
> Ganesh
No I don't get any specific error message.
The problem is that I'm reading this from a source where the text contains
phrases that are mentioned in double quotes.
So i dont know how do I handle this.

"Munsifali Rashid" wrote:

> Are you getting any specific error messages?
> You may need to encode your string using System.Web.HttpUtilites.HtmlEncode,
> before putting it into your label.
> Mun
> --
> Munsifali Rashid
> http://www.munit.co.uk/blog/
>
> "Ganesh" <Ganesh@.discussions.microsoft.com> wrote in message
> news:AE4E9B39-B05A-4C1C-BFEC-F8AABE008407@.microsoft.com...
> >I have a label control on my form. I'm assigning a text to the label at
> > runtime.
> > The problem is that the string that I'm trying to assign also contains
> > text
> > in double quotes.So when I try n assign this to the label I face a
> > problem.
> > my code is using vb.net
> > Please help me out in getting this thing done, Is there any in-built
> > function or property to handle the same?
> > thanx
> > Ganesh
>
Hi Ganesh,

There are two ways to have double quote sign in string:

1) Dim strQuote As String = "text""" ' It shows text"

2) Dim strQuote As String = "text" & Chr(34) ' It also
shows text"

HTH

Elton Wang
elton_wang@.hotmail.com

>--Original Message--
>I have a label control on my form. I'm assigning a text
to the label at
>runtime.
>The problem is that the string that I'm trying to assign
also contains text
>in double quotes.So when I try n assign this to the label
I face a problem.
>my code is using vb.net
>Please help me out in getting this thing done, Is there
any in-built
>function or property to handle the same?
>thanx
>Ganesh
>.

String with double quotes

I have a label control on my form. I'm assigning a text to the label at
runtime.
The problem is that the string that I'm trying to assign also contains text
in double quotes.So when I try n assign this to the label I face a problem.
my code is using vb.net
Please help me out in getting this thing done, Is there any in-built
function or property to handle the same?
thanx
GaneshAre you getting any specific error messages?
You may need to encode your string using System.Web.HttpUtilites.HtmlEncode,
before putting it into your label.
Mun
Munsifali Rashid
http://www.munit.co.uk/blog/
"Ganesh" <Ganesh@.discussions.microsoft.com> wrote in message
news:AE4E9B39-B05A-4C1C-BFEC-F8AABE008407@.microsoft.com...
>I have a label control on my form. I'm assigning a text to the label at
> runtime.
> The problem is that the string that I'm trying to assign also contains
> text
> in double quotes.So when I try n assign this to the label I face a
> problem.
> my code is using vb.net
> Please help me out in getting this thing done, Is there any in-built
> function or property to handle the same?
> thanx
> Ganesh
All you need to do is use "double double" quotes (a term I just made up):
For example:
lblMain.text = """Hello World """
will show on your screen as:
"Hello World"
lblMain.text = """" + "Hello World" + """"
will produce the same result!
Hope this helps you!
Fred
"Ganesh" <Ganesh@.discussions.microsoft.com> wrote in message
news:AE4E9B39-B05A-4C1C-BFEC-F8AABE008407@.microsoft.com...
> I have a label control on my form. I'm assigning a text to the label at
> runtime.
> The problem is that the string that I'm trying to assign also contains
text
> in double quotes.So when I try n assign this to the label I face a
problem.
> my code is using vb.net
> Please help me out in getting this thing done, Is there any in-built
> function or property to handle the same?
> thanx
> Ganesh
Your best bet would probably be to HTML-encode the string. e.g.:
someLabel.Text = Server.HtmlEncode(yourString)
This will replace the quotes with " named entity, as well as escaping
other HTML that might potentially cause problems (e.g.: unexpected page
modifications or cross-site scripting).
"Ganesh" <Ganesh@.discussions.microsoft.com> wrote in message
news:AE4E9B39-B05A-4C1C-BFEC-F8AABE008407@.microsoft.com...
>I have a label control on my form. I'm assigning a text to the label at
> runtime.
> The problem is that the string that I'm trying to assign also contains
> text
> in double quotes.So when I try n assign this to the label I face a
> problem.
> my code is using vb.net
> Please help me out in getting this thing done, Is there any in-built
> function or property to handle the same?
> thanx
> Ganesh
No I don't get any specific error message.
The problem is that I'm reading this from a source where the text contains
phrases that are mentioned in double quotes.
So i dont know how do I handle this.
"Munsifali Rashid" wrote:

> Are you getting any specific error messages?
> You may need to encode your string using System.Web.HttpUtilites.HtmlEncod
e,
> before putting it into your label.
> Mun
> --
> Munsifali Rashid
> http://www.munit.co.uk/blog/
>
> "Ganesh" <Ganesh@.discussions.microsoft.com> wrote in message
> news:AE4E9B39-B05A-4C1C-BFEC-F8AABE008407@.microsoft.com...
>
>

String.split to a 2d array?

I have a list of states and abbreviations that I want to be able to pull
into a 2d array to bind to a control. The list looks like this:

AL:Alabama,AK:Alaska,AZ:Arizona,...

So If I take the starting and split it on the comma, it gives me a 1d
array with XX:Name in each element. Is there any easy way (like split)
to do this into a 2d array, so I end up with:

aStates(0,0)="AL"
aStates(0,1)="Alabama"

and so on...

I know a number of ways I could code this up to make it work but I'm
wondering if there's a nice quick n' dirty way using split or something
similar. Thanks!

MattString.Split allows you to pass in more than one delimiter. Maybe what you
could do is to figure that all even indexes are the state code and all odd
indexes are the state name.

"MattB" <somedudeus@.yahoo.com> wrote in message
news:352b8dF4fa2viU1@.individual.net...
> I have a list of states and abbreviations that I want to be able to pull
> into a 2d array to bind to a control. The list looks like this:
> AL:Alabama,AK:Alaska,AZ:Arizona,...
> So If I take the starting and split it on the comma, it gives me a 1d
> array with XX:Name in each element. Is there any easy way (like split)
> to do this into a 2d array, so I end up with:
> aStates(0,0)="AL"
> aStates(0,1)="Alabama"
> and so on...
> I know a number of ways I could code this up to make it work but I'm
> wondering if there's a nice quick n' dirty way using split or something
> similar. Thanks!
> Matt
Thanks. I considered that but decided on making a DataTable, looping
through the first array from splitting on the comma, and splitting a
second time on the colon and loading the table row that way.
Works well, I just wanted to see if I was overlooking a really easy
shortcut.

Matt

Peter Rilling wrote:
> String.Split allows you to pass in more than one delimiter. Maybe what you
> could do is to figure that all even indexes are the state code and all odd
> indexes are the state name.
> "MattB" <somedudeus@.yahoo.com> wrote in message
> news:352b8dF4fa2viU1@.individual.net...
>>I have a list of states and abbreviations that I want to be able to pull
>>into a 2d array to bind to a control. The list looks like this:
>>
>>AL:Alabama,AK:Alaska,AZ:Arizona,...
>>
>>So If I take the starting and split it on the comma, it gives me a 1d
>>array with XX:Name in each element. Is there any easy way (like split)
>>to do this into a 2d array, so I end up with:
>>
>>aStates(0,0)="AL"
>>aStates(0,1)="Alabama"
>>
>>and so on...
>>
>>I know a number of ways I could code this up to make it work but I'm
>>wondering if there's a nice quick n' dirty way using split or something
>>similar. Thanks!
>>
>>Matt
>

Monday, March 26, 2012

String.split to a 2d array?

I have a list of states and abbreviations that I want to be able to pull
into a 2d array to bind to a control. The list looks like this:
AL:Alabama,AK:Alaska,AZ:Arizona,...
So If I take the starting and split it on the comma, it gives me a 1d
array with XX:Name in each element. Is there any easy way (like split)
to do this into a 2d array, so I end up with:
aStates(0,0)="AL"
aStates(0,1)="Alabama"
and so on...
I know a number of ways I could code this up to make it work but I'm
wondering if there's a nice quick n' dirty way using split or something
similar. Thanks!
MattString.Split allows you to pass in more than one delimiter. Maybe what you
could do is to figure that all even indexes are the state code and all odd
indexes are the state name.
"MattB" <somedudeus@.yahoo.com> wrote in message
news:352b8dF4fa2viU1@.individual.net...
> I have a list of states and abbreviations that I want to be able to pull
> into a 2d array to bind to a control. The list looks like this:
> AL:Alabama,AK:Alaska,AZ:Arizona,...
> So If I take the starting and split it on the comma, it gives me a 1d
> array with XX:Name in each element. Is there any easy way (like split)
> to do this into a 2d array, so I end up with:
> aStates(0,0)="AL"
> aStates(0,1)="Alabama"
> and so on...
> I know a number of ways I could code this up to make it work but I'm
> wondering if there's a nice quick n' dirty way using split or something
> similar. Thanks!
> Matt
Thanks. I considered that but decided on making a DataTable, looping
through the first array from splitting on the comma, and splitting a
second time on the colon and loading the table row that way.
Works well, I just wanted to see if I was overlooking a really easy
shortcut.
Matt
Peter Rilling wrote:
> String.Split allows you to pass in more than one delimiter. Maybe what yo
u
> could do is to figure that all even indexes are the state code and all odd
> indexes are the state name.
> "MattB" <somedudeus@.yahoo.com> wrote in message
> news:352b8dF4fa2viU1@.individual.net...
>
>
>

Saturday, March 24, 2012

strip out html

Hello

anybody knows how i can strip out the html from a control's innerhtml to show only the relevant text? i.e
<span id="test1" runat="server"><a href="http://links.10026.com/?link=test1.htm">abcde</a><b>blah</b></span

so that on my server side code, somehow i would get "abcde blah"?
thanks!FYI... you wouldn't get "abcde blah" you'd get "abcdeblah"
I dont think there is an auto-way to do this but just look for the > and <
and use the Substring in a recursive function

--
Curt Christianson
Owner/Lead Developer, DF-Software
www.Darkfalz.com

"newbie" <anonymous@.discussions.microsoft.com> wrote in message
news:799A9C64-BAB4-45D1-80EC-580EA1E98D3F@.microsoft.com...
> Hello,
> anybody knows how i can strip out the html from a control's innerhtml to
show only the relevant text? i.e.
> <span id="test1" runat="server"><a
href="test1.htm">abcde</a><b>blah</b></span>
> so that on my server side code, somehow i would get "abcde blah"??
> thanks!
"newbie" <anonymous@.discussions.microsoft.com> wrote in message
news:799A9C64-BAB4-45D1-80EC-580EA1E98D3F@.microsoft.com...
> Hello,
> anybody knows how i can strip out the html from a control's innerhtml to
show only the relevant text? i.e.
> <span id="test1" runat="server"><a
href="test1.htm">abcde</a><b>blah</b></span>
> so that on my server side code, somehow i would get "abcde blah"??
> thanks!

Provided it is xhtml compliant (i.e. no unclosed tags and case sensitive),
why not read the contents of each node, with an XmlTextReader? Then
concatenate each node's value using a StringBuilder.

ms-help://MS.NETFrameworkSDK/cpref/html/frlrfSystemXmlXmlTextReaderClassctor
Topic.htm

...and see the example at bottom for how to use the XmlTextReader.

R.

Thursday, March 22, 2012

Strong typing for user controls

I'm a lot confused when using user controls in ASP.NET 2.0. How do I cast a
varaible to the type of a user control... for example:

Me.PlaceHolder1.Controls.Add(Page.LoadControl("~/App_Controls/ctlApplicationHome.ascx"))

' now, this doesn't work... why?
Dim apl As App_Controls_ctlApplicationHome = Me.PlaceHolder1.Controls(0);Ryan wrote:
> I'm a lot confused when using user controls in ASP.NET 2.0. How do I cast a
> varaible to the type of a user control... for example:
> Me.PlaceHolder1.Controls.Add(Page.LoadControl("~/App_Controls/ctlApplicationHome.ascx"))
> ' now, this doesn't work... why?
> Dim apl As App_Controls_ctlApplicationHome = Me.PlaceHolder1.Controls(0);

Dim apl As App_Controls_ctlApplicationHome =
CType(Me.PlaceHolder1.Controls(0), App_Controls_ctlApplicationHome)

That should do it.

Just curious, what .NET language are you using? It looks like VB but I
notice you have a semi-colon at the very end of your last line of code.
:)
Actually, let me clarify... the class "App_Controls_ctlApplicationHome" does
not exist... where is it!?!

"Ryan" wrote:

> I'm a lot confused when using user controls in ASP.NET 2.0. How do I cast a
> varaible to the type of a user control... for example:
> Me.PlaceHolder1.Controls.Add(Page.LoadControl("~/App_Controls/ctlApplicationHome.ascx"))
> ' now, this doesn't work... why?
> Dim apl As App_Controls_ctlApplicationHome = Me.PlaceHolder1.Controls(0);
>
Actually, here's a revised description of the issue:

I have two user controls, each of which sits in a directory called
"App_Controls" in an ASP.NET 2.0 site. When I try dynamically loading Control
#2 to a placeholder in Control #1, I cannot get a strongly typed reference to
that control, because the Type "App_Controls_ctlApplicationHome" is not
defined.

Does that make sense?

(to answer your question, I'm a C# developer working on a VB.NET project,
hence the schizophrenic coding)

"tdavisjr" wrote:

> Ryan wrote:
> > I'm a lot confused when using user controls in ASP.NET 2.0. How do I cast a
> > varaible to the type of a user control... for example:
> > Me.PlaceHolder1.Controls.Add(Page.LoadControl("~/App_Controls/ctlApplicationHome.ascx"))
> > ' now, this doesn't work... why?
> > Dim apl As App_Controls_ctlApplicationHome = Me.PlaceHolder1.Controls(0);
> Dim apl As App_Controls_ctlApplicationHome =
> CType(Me.PlaceHolder1.Controls(0), App_Controls_ctlApplicationHome)
> That should do it.
> Just curious, what .NET language are you using? It looks like VB but I
> notice you have a semi-colon at the very end of your last line of code.
> :)
>
On Fri, 13 Jan 2006 09:34:02 -0800, "Ryan"
<Ryan@.discussions.microsoft.com> wrote:

>I have two user controls, each of which sits in a directory called
>"App_Controls" in an ASP.NET 2.0 site. When I try dynamically loading Control
>#2 to a placeholder in Control #1, I cannot get a strongly typed reference to
>that control, because the Type "App_Controls_ctlApplicationHome" is not
>defined.

You need an @. Reference in your aspx page to the user control.

<%@. Reference VirtualPath="~/Products.ascx" %
That will make sure the user control type is visible in your code
file.

Example:
http://odetocode.com/Blogs/scott/ar...06/30/1889.aspx

--
Scott
http://www.OdeToCode.com/blogs/scott/
Ah, of course! Thanks!

"Scott Allen" wrote:

> On Fri, 13 Jan 2006 09:34:02 -0800, "Ryan"
> <Ryan@.discussions.microsoft.com> wrote:
> >I have two user controls, each of which sits in a directory called
> >"App_Controls" in an ASP.NET 2.0 site. When I try dynamically loading Control
> >#2 to a placeholder in Control #1, I cannot get a strongly typed reference to
> >that control, because the Type "App_Controls_ctlApplicationHome" is not
> >defined.
> You need an @. Reference in your aspx page to the user control.
> <%@. Reference VirtualPath="~/Products.ascx" %>
> That will make sure the user control type is visible in your code
> file.
> Example:
> http://odetocode.com/Blogs/scott/ar...06/30/1889.aspx
> --
> Scott
> http://www.OdeToCode.com/blogs/scott/

Strong typing for user controls

I'm a lot when using user controls in ASP.NET 2.0. How do I cast a
varaible to the type of a user control... for example:
Me.PlaceHolder1.Controls.Add(Page.LoadControl("~/App_Controls/ctlApplication
Home.ascx"))
' now, this doesn't work... why?
Dim apl As App_Controls_ctlApplicationHome = Me.PlaceHolder1.Controls(0);Ryan wrote:
> I'm a lot when using user controls in ASP.NET 2.0. How do I cast
a
> varaible to the type of a user control... for example:
> Me.PlaceHolder1.Controls.Add(Page.LoadControl("~/App_Controls/ctlApplicati
onHome.ascx"))
> ' now, this doesn't work... why?
> Dim apl As App_Controls_ctlApplicationHome = Me.PlaceHolder1.Controls(0);
Dim apl As App_Controls_ctlApplicationHome =
CType(Me.PlaceHolder1.Controls(0), App_Controls_ctlApplicationHome)
That should do it.
Just curious, what .NET language are you using? It looks like VB but I
notice you have a semi-colon at the very end of your last line of code.
:)
Actually, let me clarify... the class "App_Controls_ctlApplicationHome" does
not exist... where is it!?!
"Ryan" wrote:

> I'm a lot when using user controls in ASP.NET 2.0. How do I cast
a
> varaible to the type of a user control... for example:
> Me.PlaceHolder1.Controls.Add(Page.LoadControl("~/App_Controls/ctlApplicati
onHome.ascx"))
> ' now, this doesn't work... why?
> Dim apl As App_Controls_ctlApplicationHome = Me.PlaceHolder1.Controls(0);
>
Actually, here's a revised description of the issue:
I have two user controls, each of which sits in a directory called
"App_Controls" in an ASP.NET 2.0 site. When I try dynamically loading Contro
l
#2 to a placeholder in Control #1, I cannot get a strongly typed reference t
o
that control, because the Type "App_Controls_ctlApplicationHome" is not
defined.
Does that make sense?
(to answer your question, I'm a C# developer working on a VB.NET project,
hence the schizophrenic coding)
"tdavisjr" wrote:

> Ryan wrote:
> Dim apl As App_Controls_ctlApplicationHome =
> CType(Me.PlaceHolder1.Controls(0), App_Controls_ctlApplicationHome)
> That should do it.
> Just curious, what .NET language are you using? It looks like VB but I
> notice you have a semi-colon at the very end of your last line of code.
> :)
>
On Fri, 13 Jan 2006 09:34:02 -0800, "Ryan"
<Ryan@.discussions.microsoft.com> wrote:

>I have two user controls, each of which sits in a directory called
>"App_Controls" in an ASP.NET 2.0 site. When I try dynamically loading Contr
ol
>#2 to a placeholder in Control #1, I cannot get a strongly typed reference
to
>that control, because the Type "App_Controls_ctlApplicationHome" is not
>defined.
>
You need an @. Reference in your aspx page to the user control.
<%@. Reference VirtualPath="~/Products.ascx" %>
That will make sure the user control type is visible in your code
file.
Example:
http://odetocode.com/Blogs/scott/ar...06/30/1889.aspx
Scott
http://www.OdeToCode.com/blogs/scott/
Ah, of course! Thanks!
"Scott Allen" wrote:

> On Fri, 13 Jan 2006 09:34:02 -0800, "Ryan"
> <Ryan@.discussions.microsoft.com> wrote:
>
> You need an @. Reference in your aspx page to the user control.
> <%@. Reference VirtualPath="~/Products.ascx" %>
> That will make sure the user control type is visible in your code
> file.
> Example:
> http://odetocode.com/Blogs/scott/ar...06/30/1889.aspx
> --
> Scott
> http://www.OdeToCode.com/blogs/scott/
>

Strongly Type Web Forms

How does a User Control invoke a method on its parent page? Say I have a publicMethod in code behind for an aspx page:
...
public void SomeImportantMethod() { /* do important stuff */}
...
From an Event Handler within the User Control, I'd like to invoke a method on the page in which the User Control is embedded. If I stop in the debugger and inspect this.Page, I see that it has a type of "ASP.MyAspxPage"... where did that come from? Can I delcare it intentionally somewhere?
TIA,
Geo
Your MyAspxPage is your custom page class, which inherits from the System.Web.UI.Page class.
The thing with UserControls is though, that you don't know what parent page they could be in at run-time.
So, to be safe, if you want to call a particular method on the parentPage, I'd say let your Page implement your interface which contains themethod signature (and other stuff if you want).
In your UserControl you then do a defensive cast (using the as keyword)to that interface and check for != null. You then simply call yourmethod on your interface object.
Hope that helps.
Wim

Yes, that does help. It helped in that I was hung up in anold paradigm and should get over it and just use an interface. So, thanks, that helped me get the job done.
I guess I was stuck in theOLD ASP.NET 1.1 code behind days where an ASPX page would have a code behind page which had a namespace declaration and the web form would be a public class derived from System.Web.UI.Page. So, from any C# code I could say:
MyNamespace.MyPage myPage = this.Page as MyNameSpace.MyPage;
if ( myPage != null)
{
myPage.MyMethod("hello world");
}
else
{
Response.Write("what gives?");
}
No fuss no muss, no interfaces.
I think I better go find an article or 2 on code beside or what ever it is we call it now.
Thanks again,
geo

Hello.

I think that you can also use the @.reference directive to introduce the type of the page in the user control (haven't tried it though).

Tuesday, March 13, 2012

Struggling With Concept

Hi Folks,
As you may know I'm new to ASP.NET and Im having trouble
grappling with one concept.
If I create a user control and place it on a page I can access its public
properties through a pre-render block, But I cant access its public
properties via code. In say the Page_Load event.
Surely if the control is registered, when the Page_Load Loads, it must have
access to the UserControl Object otherwise whats the point?
Please tell me where Im going wrong !
CheersHi,
What you need to do is access the usercontrol directly, for example: -
Page_Load(...)
{
((myusercontroltype)this.FindControl("UserControlInstance1")).<Property>
}
Hth,
Phil Winstanley
Microsoft ASP.NET MVP
http://www.myservicescentral.com
Thank you so much for that, I knew it should be possible, but I must have
missed that Method somehow. I tried this instantly and it work just
absolutely fine.
Thanks Again for your help.
Best Regards - OHM
"Phil Winstanley [Microsoft MVP ASP.NET]" <phil@.winstanley.name> wrote i
n
message news:caep52$9ft@.odah37.prod.google.com...
> Hi,
> What you need to do is access the usercontrol directly, for example: -
> Page_Load(...)
> {
> ((myusercontroltype)this.FindControl("UserControlInstance1")).<Property>
> }
> Hth,
> Phil Winstanley
> Microsoft ASP.NET MVP
> http://www.myservicescentral.com
>
You can also declare the control at the module level and use the withevents
keyword to expose its events if they are needed. Then you can reference the
control via the code editor.
#Region " Web Form Designer Generated Code "
'Instantiate the control here and you can use it throughout the page by
name.
Protected WithEvents MyUserControl As New MyUserControl
'NOTE: The following placeholder declaration is required by the Web Form
Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub
#End Region
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
If Not IsPostBack Then
With Me.MyUserControl
.DataSource = SomeDataSource
.Text = "SomeValue"
.SomeOtherProperty = "SomeOtherValue"
End With
End If
End Sub
"One Handed Man ( OHM#)" <news.microsoft.com> wrote in message
news:uazb75GUEHA.1048@.tk2msftngp13.phx.gbl...
> Hi Folks,
> As you may know I'm new to ASP.NET and Im having trouble
> grappling with one concept.
> If I create a user control and place it on a page I can access its public
> properties through a pre-render block, But I cant access its public
> properties via code. In say the Page_Load event.
> Surely if the control is registered, when the Page_Load Loads, it must
> have
> access to the UserControl Object otherwise whats the point?
> Please tell me where Im going wrong !
> Cheers
>

Struggling With Concept

Hi Folks,
As you may know I'm new to ASP.NET and Im having trouble
grappling with one concept.

If I create a user control and place it on a page I can access its public
properties through a pre-render block, But I cant access its public
properties via code. In say the Page_Load event.

Surely if the control is registered, when the Page_Load Loads, it must have
access to the UserControl Object otherwise whats the point?

Please tell me where Im going wrong !

CheersYou can also declare the control at the module level and use the withevents
keyword to expose its events if they are needed. Then you can reference the
control via the code editor.

#Region " Web Form Designer Generated Code "
'Instantiate the control here and you can use it throughout the page by
name.
Protected WithEvents MyUserControl As New MyUserControl

'NOTE: The following placeholder declaration is required by the Web Form
Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub

#End Region

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
If Not IsPostBack Then
With Me.MyUserControl
.DataSource = SomeDataSource
.Text = "SomeValue"
.SomeOtherProperty = "SomeOtherValue"
End With
End If
End Sub

"One Handed Man ( OHM#)" <news.microsoft.com> wrote in message
news:uazb75GUEHA.1048@.tk2msftngp13.phx.gbl...
> Hi Folks,
> As you may know I'm new to ASP.NET and Im having trouble
> grappling with one concept.
> If I create a user control and place it on a page I can access its public
> properties through a pre-render block, But I cant access its public
> properties via code. In say the Page_Load event.
> Surely if the control is registered, when the Page_Load Loads, it must
> have
> access to the UserControl Object otherwise whats the point?
> Please tell me where Im going wrong !
> Cheers

Struggling with querystring calling in formviews

Hi,

I am having trouble getting querystrings to show up in textboxes in a formview control. I know you have to dig deeper to find the textboxes, but I thought I had done so, but it doesnt seem to work at the moment..

I am currently getting:

'Buyers_Shippingdetails' does not contain a definition for 'SellerusernameTextBox'

referring to:

Line 28: this.SellerusernameTextBox.Text = Request.QueryString["sellername"];

as an error.. my code is:

protected void FormView1_Load(object sender, EventArgs e)
{
TextBox SellerusernameTextBox = FormView1.FindControl("SellerusernameTextBox") as TextBox;

TextBox NameitemTextBox = FormView1.FindControl("NameitemTextBox") as TextBox;

TextBox prodIDTextBox = FormView1.FindControl("prodIDTextBox") as TextBox;

this.SellerusernameTextBox.Text = Request.QueryString["sellername"];
this.NameitemTextBox.Text = Request.QueryString["itemname"];
this.prodIDTextBox.Text = Request.QueryString["proID"];
}
}

Thanks if someone can help! I am stuck with the same problem on a couple of pages.

Any advice is welcome!

Cheers,

Jon

First off, remove the "this" qualifier from in fron tof your variables. These are method-level variables and don't require qualifiers. Also, I'd recommend you move your code to the FormView.DataBound event.

protected void FormView1_DataBound(object sender, EventArgs e){TextBox SellerusernameTextBox = FormView1.FindControl("SellerusernameTextBox")as TextBox;TextBox NameitemTextBox = FormView1.FindControl("NameitemTextBox")as TextBox;TextBox prodIDTextBox = FormView1.FindControl("prodIDTextBox")as TextBox;if (SellerusernameTextBox ==null) {return; }if (NameitemTextBox ==null) {return; }if (prodIDTextBox ==null) {return; }SellerusernameTextBox.Text = Request.QueryString["sellername"];NameitemTextBox.Text = Request.QueryString["itemname"];prodIDTextBox.Text = Request.QueryString["proID"];}

Just the trick.

Thanks alot, as always.

Jon

Stuck on If Then Statement With Control

I am just really getting into learning all this with ASP and have been head deap into books, lol. The thing I am not sure about is this. I am using the .NET controls on my page but I do not understand how to take what I have below and then do a if then statement on it.

So another words here is my code:

<asp:DataListID="DataList1"runat="server"DataSourceID="SqlDataSource1">

<ItemTemplate>

<asp:LabelID="SpringBCT1Label"runat="server"

Text='<%# Eval("SpringBCT1") %>'/>

<br/>

</ItemTemplate>

</asp:DataList>

Now how can I take what the results were that came back for that and do a if then statement on it? I need a way to look at the data. The data from the database will either be "On Time" or "Delayed"

So I want to do a...

If whatever = "On Time" then

Else

' do something else here

I am just not sure how to convert that control into a string of data. Code example would be great.

Big thanks in advance!

Is "whatever" the results of Eval("SpringBCT1") ?

Depending on how/when u need this value you can do two things. The first (and easiest) is just to attach an event to your SprintBCT1Label like: OnPreRender="GetMyTextValue"

protected void GetMyTextValue(object sender, EventArgs e){

Label label = sender as Label;

if (label == null)

return;

Response.Write(label.Text);//do something useful with it
}

and then you can just read out the value of the label every time the event handler fires for each label. Alternatively you can enumerate through the data lists items.

foreach (DataListItem item in DataList1.Items){

Label bct1Label = item.FindControl("SpringBCT1") as Label;

if (bct1Label == null)

continue;

Response.Write(bct1Label.Text);//do something useful
}

Hope that helps.


Yes the "watever" would be the results. I have selected to do this all in VB. The above has me lost... lol. Could you go into a bit more detail?

Thanks!


Here is how I would like to do it.. This is the way I did it in pure VB.NET

Dim MyConnAs ADODB.Connection

Dim MyRecSetAs ADODB.Recordset

Dim strTitleAsString

MyConn =New ADODB.Connection

MyConn.ConnectionString ="Provider=sqloledb;Data Source=D032379\SQLEXPRESS;Initial Catalog=CutOffGrid;Integrated Security=True"

MyConn.Open()

MyRecSet = MyConn.Execute("SELECT * FROM Grids")

DoUntil MyRecSet.EOF

strTitle = MyRecSet.Fields.Item("SpringBCT1").Value

If strTitle ="On Time"Then

SpringBCT1.Text ="On Time"

Else

SpringBCT1.Text ="Delayed"

EndIf

MyRecSet.MoveNext()

Loop

MyConn.Close()

Problem is this does not seem to work in ASP.NET. It does not like these items:

Dim MyConnAs ADODB.Connection

Dim MyRecSetAs ADODB.Recordset

MyConn =New ADODB.Connection

And I have no idea as to how to do this then :-(

Any ideas?


Because they're old ADO objects not ADO.Net...

you can look throw the datalist like this:

Dim strTitle

For Each item As DataListItem In DataList1.Items

Dim bct1Label As Label = TryCast(item.FindControl("SpringBCT1"), Label)

If bct1Label Is Nothing Then
Continue For

End If

strTitle = bct1Label.Text

if NOT strTitle = "On Time" Then 'not sure that's how you do != in vb
bct1Label.Text = "Delayed"
End If
Next

Do you actually need the datalist or have you just done that because you experimenting?

See MSDN docs on SqlDataSource, SqlConnection and SqlDataReader


Ok ffigured it out. :-)

This is how I did it with the backend VB page...

Dim conpubsAs SqlConnection

Dim cmdselectauthorsAs SqlCommand

Dim dtrauthorsAs SqlDataReader

Dim sp1AsString

Dim sp2AsString

conpubs =New SqlConnection("Data Source=D032379\SQLEXPRESS;Initial Catalog=CutOffGrid;Integrated Security=True")

conpubs.Open()

cmdselectauthors =New SqlCommand("select * from Grids", conpubs)

dtrauthors = cmdselectauthors.ExecuteReader()

While dtrauthors.Read()

sp1 = (dtrauthors("SpringBCT1")).ToString

sp2 = (dtrauthors("SpringBCT2")).ToString

SpringBCT1.Text = sp1

SpringBCT2.Text = sp2

EndWhile

dtrauthors.Close()

conpubs.Close()

Thanks for the help!


You can also do this way,

<asp:LabelID="SpringBCT1Label"runat="server" Text='<%#IIf((Eval("SpringBCT1").ToString() = "On Time"), "On Time", "Delayed") %>'/>

Thanks

-Mark post(s) as "Answer" that helped you