Showing posts with label controls. Show all posts
Showing posts with label controls. Show all posts

Thursday, March 29, 2012

passing values between controls

Hi all, learning the asp.net is a slow process... but I am plodderign
along. I have hit a bit of a snag - I have 4 pages of code here...
control1.ascx
control2.ascx
default.asx... default.asx.vb

relevant details (i think) on default.asx:
<uc1:ProductList ID="ProductList1" runat="server"
OnPreRender="getSelectedProduct" />
<uc2:ProductDetails ID="ProductDetails1" runat="server" /
on default.asx.vb:
Protected Sub getSelectedProduct(ByVal sender As Object, ByVal e As
System.EventArgs)
Response.Write("TEST: " & ProductList1.selectedProduct())
End Sub

To this point the system actually works. When i select a product in the
1st User Control, it is displayed on the main page, however, I need to
take this one step further... base what happens in UC2 on what has
happened in UC1.

what would I type in UC2, to reference to get the value of the
selection in UC1 ?You need to think of your user controls as objects...

and as an object, it can expose properties, read and/or write properties.

public string PropertyOfUserControl1
{
get {return "this could be a member variable";}
}

Then you can read the property from uc1, and set a property of uc2.

uc2.PropertyOfUserControl2 = uc1.PropertyOfUserControl1;

If the code behind of uc2...you need to deal with the property you set.

<evandela@.bigpond.net.au> wrote in message
news:1142818077.366768.296410@.g10g2000cwb.googlegr oups.com...
> Hi all, learning the asp.net is a slow process... but I am plodderign
> along. I have hit a bit of a snag - I have 4 pages of code here...
> control1.ascx
> control2.ascx
> default.asx... default.asx.vb
> relevant details (i think) on default.asx:
> <uc1:ProductList ID="ProductList1" runat="server"
> OnPreRender="getSelectedProduct" />
> <uc2:ProductDetails ID="ProductDetails1" runat="server" />
> on default.asx.vb:
> Protected Sub getSelectedProduct(ByVal sender As Object, ByVal e As
> System.EventArgs)
> Response.Write("TEST: " & ProductList1.selectedProduct())
> End Sub
> To this point the system actually works. When i select a product in the
> 1st User Control, it is displayed on the main page, however, I need to
> take this one step further... base what happens in UC2 on what has
> happened in UC1.
> what would I type in UC2, to reference to get the value of the
> selection in UC1 ?

passing values between controls

Hi all, learning the asp.net is a slow process... but I am plodderign
along. I have hit a bit of a snag - I have 4 pages of code here...
control1.ascx
control2.ascx
default.asx... default.asx.vb
relevant details (i think) on default.asx:
<uc1:ProductList ID="ProductList1" runat="server"
OnPreRender="getSelectedProduct" />
<uc2:ProductDetails ID="ProductDetails1" runat="server" />
on default.asx.vb:
Protected Sub getSelectedProduct(ByVal sender As Object, ByVal e As
System.EventArgs)
Response.Write("TEST: " & ProductList1.selectedProduct())
End Sub
To this point the system actually works. When i select a product in the
1st User Control, it is displayed on the main page, however, I need to
take this one step further... base what happens in UC2 on what has
happened in UC1.
what would I type in UC2, to reference to get the value of the
selection in UC1 ?You need to think of your user controls as objects...
and as an object, it can expose properties, read and/or write properties.
public string PropertyOfUserControl1
{
get {return "this could be a member variable";}
}
Then you can read the property from uc1, and set a property of uc2.
uc2.PropertyOfUserControl2 = uc1.PropertyOfUserControl1;
If the code behind of uc2...you need to deal with the property you set.
<evandela@.bigpond.net.au> wrote in message
news:1142818077.366768.296410@.g10g2000cwb.googlegroups.com...
> Hi all, learning the asp.net is a slow process... but I am plodderign
> along. I have hit a bit of a snag - I have 4 pages of code here...
> control1.ascx
> control2.ascx
> default.asx... default.asx.vb
> relevant details (i think) on default.asx:
> <uc1:ProductList ID="ProductList1" runat="server"
> OnPreRender="getSelectedProduct" />
> <uc2:ProductDetails ID="ProductDetails1" runat="server" />
> on default.asx.vb:
> Protected Sub getSelectedProduct(ByVal sender As Object, ByVal e As
> System.EventArgs)
> Response.Write("TEST: " & ProductList1.selectedProduct())
> End Sub
> To this point the system actually works. When i select a product in the
> 1st User Control, it is displayed on the main page, however, I need to
> take this one step further... base what happens in UC2 on what has
> happened in UC1.
> what would I type in UC2, to reference to get the value of the
> selection in UC1 ?
>

Passing values between User Controls

I programmatically add a user control to a page which has a data grid on it. The user can then click on a link button to drill down to detail information.

What I would like to do is unload the existing user control and add the new user control with the data grid showing the detail information.

The question is how do I pass the key values from the existing user control to the new user control ?

Thanks

MarkYou can add your Items to the CurrentContext in order to pass from one control to another or from Page to an control


Context.Items.Add("MyValue", myValue)

To retrieve


Context.Items["MyValue"]

The problem here is when I click on the LinkButton on the DataGrid its going to cause the Parent page to reload itself and in the parents Page_Load subroutine its going to unload the existing Datagrid and load up the User Control with the detail DataGrid on. Therefore, it will never pass through the LinkButton's event to set the Context value.

Unless the secret is to load up both User Controls, on the Post Back, but hide the Header DataGrid ? Or to use client side code to pass the key of the selected record to a hidden field which will get submitted back to the server and be available much sooner in the process ?

I would be interested to here what people think would be the best way of doing this or whether there is a way to still use the Context stuff.

Cheers

Mark
Hi,

you could use a delegate.This article explains it how you could do it.

Grz, Kris.
Here Context is the HttpContext which is always available in a page and user controls. You can use this on the Page Load to store some values and then retrieve it later in the User controls as shown in my previous post.

Monday, March 26, 2012

Passing Variable To Controls

If we have a web page in which any simple control is present for example a TextBox

<asp:TextBoxID="TextBox1"runat="server"Text=""></asp:TextBox>

Now in code file a public variable is defined like

publicstring txtValue ="Khan";

Now I want to assign thetxtValue inText filed of TextBox Just like

<asp:TextBoxID="TextBox1"runat="server"Text="<%=txtValue%>"></asp:TextBox>

But this approach is not working.

Can anyone tell it?

Modify your markup to:

<asp:TextBox ID="TextBox1" runat="server" Text='<%# GetValue %>' />
 Then you should make an at least protected function in your code behind to return the string:
 
protected string GetValue(){return txtValue;}

I have tried it but it is getting error.

Please check it on you own side.


This syntax should work. I've used it a dozen times in my apps.

What is the error you're getting?


Default.aspx.cs

using System;

using System.Data;

using System.Configuration;

using System.Collections;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

publicpartialclass_Default : System.Web.UI.Page

{

privatestring txtValue ="Zeeshan";protectedstring GetValue()

{

return txtValue;

}

}

Default.aspx

<%@.PageLanguage="C#"AutoEventWireup="true"CodeFile="Default.aspx.cs"Inherits="_Default"Debug="true" %>

<!DOCTYPEhtmlPUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<htmlxmlns="http://www.w3.org/1999/xhtml">

<headrunat="server">

<title>Untitled Page</title>

</head>

<body>

<formid="form1"runat="server">

<div>

<asp:TextBoxID="TextBox1"runat="server"Text="<%# GetValue %>"></asp:TextBox>

</div>

</form>

</body>

</html>

And the error which I am getting is

Error 1 The best overloaded method match for 'System.Convert.ToString(object, System.IFormatProvider)' has some invalid arguments D:\New Project\dawateislamix.net\Default.aspx 12

Error 2 Argument '1': cannot convert from 'method group' to 'object' D:\New Project\dawateislamix.net\Default.aspx 12


Can you try with<asp:TextBoxID="TextBox1"runat="server"Text="<%# GetValue() %>"></asp:TextBox>


Modify your html as follows:

<asp:TextBoxID="TextBox1"runat="server"Text="<%# GetValue() %>"></asp:TextBox>


There is not any error now.

But still the value is not passed to the control.


Pls try to set the value in

Page_Load() event.

txtValue ="Zeeshan" ;


Default2.aspx

<%@.PageLanguage="C#"AutoEventWireup="true"CodeFile="Default2.aspx.cs"Inherits="Default2" %>

<!DOCTYPEhtmlPUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<htmlxmlns="http://www.w3.org/1999/xhtml">

<headrunat="server">

<title>Untitled Page</title>

</head>

<body>

<formid="form1"runat="server">

<div>

<asp:textboxID="Textbox1"runat="server"Text="<%# GetValue() %>"></asp:textbox>

</div>

</form>

</body>

</html>

Default2.aspx.cs

using System;

using System.Data;

using System.Configuration;

using System.Collections;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

publicpartialclassDefault2 : System.Web.UI.Page

{

privatestring txtValue;protectedstring GetValue()

{

return txtValue;

}

protectedvoid Page_Load(object sender,EventArgs e)

{

txtValue ="Zeeshan";

}

}

Please check it.

It still does not show the text.


sorry guys its as simple as this

in your code behind you have this

publicpartialclasscodedisplay : System.Web.UI.Page

{

privatestring txtValue ="Zeeshan";protectedvoid Page_Load(object sender,EventArgs e)

{

TextBox1.Text = txtValue;

}

}

and in your aspx you have this

<asp:TextBoxID="TextBox1"runat="server"/>

with all form and html elements.

Good luck


You can do it in your code behind instead of in your markup:

protectedvoid Page_Load(object sender,EventArgs e)

{

if (!IsPostBack)

TextBox1.Text = GetValueStr();

}

privatestring txtValue ="test";protectedstring GetValueStr()

{

return txtValue;

}


But If I want to do it on Mark up then.


Well if you want to do it on mark up then, put it straight in

text="Zeeshan"

text is a property and hence

Don't use code blocks in ASPNET. Avoid if you can avoid it because they are written for backward compatability; instead write an event handler or startup routine in the codebehind which populates the property on the control as necessary.

For example, you could do it in Page_Load - but be sure to surround it with if (! PostBack) so that the value doesn't get overwritten when posting back.

In late binding controls i.e. say a textbox in a gridview etc, you can databind but that again uses reflection etc and hence is slow.


Hi,

If you have to do it in the markup use single quotes instead Text='<%=MyPublicVariable %>'

HTH

Saturday, March 24, 2012

passing variables between controls

I have a default page that loads a few web user controls like below

pnl_header.Controls.Add(Page.LoadControl("control\\header.ascx"));
pnl_body.Controls.Add(Page.LoadControl("control\\list_topic.ascx"));

And I have some information which is the same for each control and I do not want to do a SQL call every time for each control so I was wondering what is the best way to call the info once and then share the information between all the controls.

TIA.
AlvinYou could call on the sql on the first application load event. Then you could add the object to cache.

Cache["myCache"] = myDataSet; // for instance

if(Cache["myCache"]==null){

// Call a method to create the cache

}
esle{

myNewDataSet = (DataSet) Cache["myCache"];
}

Then you can retrieve the info from cache instead of hitting the db everytime.

You could also look at CacheDependencies as you can have the cache automatically refresh based on a change event - although this may not be necessary since you connecting to a db.

Hope that helps
This is just what I needed. Thanks Keith

Wednesday, March 21, 2012

passing variables to a server control

Hi,

I'm maintaining a website with alot of files, and every page uses server controls for both the header and the footer. On some of the pages I don't want to show part of the header, however I am still including the header. The only way I can think to do this is to have a seperate header for the pages I want to display different . This is a major pain if I have to do it this way. Is there anyway I can pass a variable to the header server control ? I'd love to pass an "on" or "off" and display the part of the header accordingly. This is what my relevant parts of my page look like on every page

<%@dotnet.itags.org.RegisterTagPrefix="abc"TagName="myHeader"Src="../includes/myHeader.ascx" %>

<html>

<body>

<abc:myHeaderrunat="server"id="myHeader"/>

...........

</body>

</html>

If you have source access to your header/footer server control, then you can create a property called something like 'DisplayMode' and assign it with a default value, i.e.: ON. And inside these controls, do a check on the mode what to display.

Your server control on the page files would be look like:

<abc:myHeader runat="server" id="myHeader" DisplayMode="Off" />

hi Jack,

This sounds great. How would I go about creating the property? Could you possibly give me an example ? :)

Much appreciated,

mikeg123


Hi,

Well I think you can do it in various ways.

1. Make some public properties within your UserControl and the pages where you don't want to use certain areas make them invisible through the properties.
2. You can write some code in your Web user control in which you can check the current URL and make the portion available or not available. You can get the current url by Request.Path.ToString(); But for sure this method will be a lengthy one because you have to mention all the url's for which you want to make changes in the header.
3. You can pass a query string variable and check it on the UserControl's load event with Request.QueryString["variableName"]. I think this would be a reasonable way because for certain situations you just have to pass the value in the query string variable and you write code in the user control to handle the situation.

These are the possible ways that I can advise you there might be more.

I hope it will solve your problem

Thanks and best regards,


#2 & #3 wont work for me, however your first suggestion sounds great

I have created this property in the user control, but I'm not sure how I am supposed to access the information I am passsing ?

PublicProperty displayBanner()

Get

EndGet

Set(ByVal value)

EndSet

EndProperty

thanks very much!!


Example in c#:

public class myHeader{// private fieldsprivate string _displayMode ="On";// you can work little more to create an Enum to limit types ...// public propertypublic string DisplayMode {set {if (!string.IsNullOrEmpty(value)) { _displayMode =value; } } } ...// eventsprotected void OnLoad() {if (string.Compare(_displayMode,"off",true) == 0) {// hide control }else if (string.Compare(_displayMode,"alternate",true) == 0) {// display an alternate view }else {// show up normally } }}

Hi Jack,

You wouldnt be able to give me one in VB would you? I apologize for not specifying before, I thought I would be able to convert it, but this is pretty confusing for me.

Very much appreciated, have a good day!

Thanks again,

hehe, I'm not a VB.NET developer. however, I've converted to VB.NET, let me know if it works. I think you should get the idea at least.

Public Class myHeader' private fieldsPrivate _displayModeAs String ="On"' you can work little more to create an Enum to limit types ...' public propertyPublic WriteOnly Property DisplayMode()As String Set (ByVal ValueAs String)If Not String.IsNullOrEmpty(value)Then _displayMode = valueEnd If End Set End Property ...' eventsProtected Sub OnLoad()If String.Compare(_displayMode,"off",True) = 0Then' hide controlElse If String.Compare(_displayMode,"alternate",True) = 0Then' display an alternate viewElse' show up normallyEnd If End SubEnd Class

awesome thanks so much it worked!!

this is a problem ive been pushing under the rug for literally two years, much apprecaited !

Passing Variables to Nested User Controls

Question: How do I pass variables to a nested user control ?

Description: I want to call a user control that calls another user control for encapsulation as shown below. I have a very hard time making the child control see the grandparent variable passed to it In the example shown below, Default.aspx calls the uc:Control, which in turn calls the uc:SubControl. I can get uc:Control to see Variable1="Value1", but I cannot get the uc:SubControl to see that value. What am I doing wrong. I use VB for the code behind and I have a public variable called Variable1 in both user controls.

Default.aspx:
<uc:Control id="Parent" Variable1="Value1" runat="server"></uc:ParentControl
ParentControl.ascx:
<uc:SubControl id="Child" Variable1="<%=Value1%>" runat="server"></uc:ChildControlYou can't use the <%= %> syntax to set an attribute of a server control.
Use <%# %> instead, and DataBind().

passing variables to user controls

how do i pass a variable from one page or user control to the other?
i.e i have a header ascx file, and the title can be in 2 languages, the data is picked out of a db, so what i want to do is generate the sql string based on a variable that is defined from a hypelink within in another ascx file. But both these files will be common inside an/each aspx file.

Any ideas/suggestion? (I assume this is possible).

Thanks in advance.
DeianActually, you don't need to keep track of this kind of information on your own; HTTP requests include an 'Accept-Language' header which contains a setting for the language content the user prefers. (If the user is using Internet Explorer, she can set this from the Genral tab in Internet Options by clicking the 'Languages' button.)

The code for extracting this information on a Page would look like this:


Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim ci As System.Globalization.CultureInfo
Dim FullName As String

Try
ci = New System.Globalization.CultureInfo(Request.UserLanguages(0))
FullName = ci.EnglishName
Catch
' CultureInfo only supports a subset of all valid language identifiers, so the above
' might fail for some languages.
FullName = Request.UserLanguages(0)
End Try

Label1.Text = FullName
End Sub


On a UserControl, you would need to replace 'Request' with 'Context.Request' to get to the same information.

Hope this helps,
> Any ideas/suggestion? (I assume this is possible).

Yes, it's possible.

I've put up a sample for you atwww.edition3.com/developers/deiwales
The boxes show the two separate user controls.

The code can be downloaded fromwww.edition3.com/developers/deiwales/code.zip

For anyone following this thread later, here's the code:



ASPX Page:
<%@. Page Language="VB" %>
<%@. Register TagPrefix="my" TagName="UserControlA" src="http://pics.10026.com/?src=mycontrol7.ascx" %>
<%@. Register TagPrefix="my" TagName="UserControlB" src="http://pics.10026.com/?src=mycontrol7b.ascx" %>
<script runat="server"
Sub UserControlB_Click( ByVal sender As Object, ByVal eArgs As EventArgs )
UserControlA.SetLanguage( UserControlB.Language )
End Sub

</script>
<html>
<head>
</head>
<body>
<form runat="server">
<div style="border: solid 1px silver;padding: 20px;">
<my:UserControlB id="UserControlB" onclick="UserControlB_Click" runat="server" />
</div>
<div style="border: solid 1px silver;padding: 20px;">
<my:UserControlA id="UserControlA" runat="server" />
</div>
</form>
</body>
</html>



UserControlA (to display language):
<%@. Control Language="VB" %>
<script runat="server"
Sub Page_Load( sender As Object, eArgs As EventArgs )
If Not Page.IsPostBack Then
SetLanguage( "English" )
End If
End Sub

Public Sub SetLanguage( Which As String )
If Which = "French" Then
Message.Text = "Bonjour, je suis"
Else
Message.Text = "Hello, I am"
End If
End Sub

</script>
<asp:Label id="Message" runat="server" /> UserControlA.ascx



UserControlB (to set the language):
<%@. Control Language="VB" %>
<script runat="server"
Public Language As String

Public Event Click(ByVal sender As Object, ByVal eArgs As EventArgs)

Sub Submit_Click( ByVal sender As Object, ByVal eArgs As EventArgs )
' set the language
Language = SelectedLanguage.SelectedItem.Text
' raise the event, so that our ASPX page can handle it
RaiseEvent Click( Me, EventArgs.Empty )
End Sub

Protected Overrides Function OnBubbleEvent(ByVal sender As Object, ByVal eArgs As System.EventArgs) As Boolean
' and now bubble it up
RaiseBubbleEvent(sender, eArgs)
End Function

</script>
<asp:RadioButtonList id="SelectedLanguage" runat="server">
<asp:ListItem text="English" selected="True" />
<asp:ListItem text="French" />
</asp:RadioButtonList>
<asp:Button id="Submit" text="Submit" onclick="Submit_Click" runat="server" />


Good stuff... that clarifies it.

But as for the internal support for the language ye that won't work cos my idea is to do web sites in english and provide welsh language translation at the touch of a button. So in the db the data will be held, and the string will be used to decide the logic for the sql string. Hope that clairfies my scenario.

The DB.Table is done in a way that holds data and cssstyles against page number, language and content type, and the id of everything will be generated out of the cirteria that define the content/style.

Thanks guys.

PassingValues

I want to make sure that all the values of my controls on my form are available in my submit button's click handler. Currently, all my controls are yielding empty values. I tried making my controls a class level object, but that does not solve it. I am having trouble figuring out how to get these values using the eventargs object. Is there a good way of sending the controls current values as parameters to the handler method?To access properties and methods in User Controls, you need to add a line declaring the control in the CodeBehind.

C#
protected {Control Class Name} {Control Name on Form};

VB.NET
Protected {Control Name on Form} As {Control Class Name}

Friday, March 16, 2012

passthrough properties

(I didn't get any response on the first post of this. Is this the right
forum
for ASCX user controls?)
I am writing a user control that has a combo box in it (3rd party). I want
to pass thru properties from the control so I wrote the following:
Public WriteOnly Property AutoPostBack(ByVal TF) As Boolean
Set(ByVal Value As Boolean)
cbVendors.AutoPostBack = TF
End Set
End Property
It compiles fine but it doesn't show when I use the user control. What am I
doing wrong? Is there any good articles on passing through properties in
user controls?
Thanks,
GTry making it Public Shared...
"G. Dean Blake" wrote:

> (I didn't get any response on the first post of this. Is this the right
> forum
> for ASCX user controls?)
>
> I am writing a user control that has a combo box in it (3rd party). I wan
t
> to pass thru properties from the control so I wrote the following:
> Public WriteOnly Property AutoPostBack(ByVal TF) As Boolean
> Set(ByVal Value As Boolean)
> cbVendors.AutoPostBack = TF
> End Set
> End Property
> It compiles fine but it doesn't show when I use the user control. What am
I
> doing wrong? Is there any good articles on passing through properties in
> user controls?
> Thanks,
> G
>
>

password

how i create a textbox for password using web controls

Paramita-

Are you asking how you can adding a TextBox to your page that hides the text input (a.k.a. password mode)?

If so, the solution is very easy. Simply add an ASP.NET Textbox to your page and set its TextMode property to Password, like this:

<asp:textbox runat="server" id="myTxtBox" TextMode="Password" />

Thanks~