Monday, March 26, 2012
pass value to user control from aspx
- using LoadControl("MyCtrl.ascx")
MyCtrl _mycontrol = (MyCtrl)LoadControl("MyCtrl.ascx")
Page.Controls.Add(_mycontrol);
_mycontrol.Text
it will be loaded when some events fired.
so how can I pass value to this user control from aspx template file.Hi,
Create a property in user control and set the property in template file or
code file.
Prakash.C
"pei_world" wrote:
> I have a user control:
> - using LoadControl("MyCtrl.ascx")
> MyCtrl _mycontrol = (MyCtrl)LoadControl("MyCtrl.ascx")
> Page.Controls.Add(_mycontrol);
> _mycontrol.Text
> it will be loaded when some events fired.
> so how can I pass value to this user control from aspx template file.
>
>
pass value to user control from aspx
- using LoadControl("MyCtrl.ascx")
MyCtrl _mycontrol = (MyCtrl)LoadControl("MyCtrl.ascx")
Page.Controls.Add(_mycontrol);
_mycontrol.Text
it will be loaded when some events fired.
so how can I pass value to this user control from aspx template file.Hi,
Create a property in user control and set the property in template file or
code file.
Prakash.C
"pei_world" wrote:
> I have a user control:
> - using LoadControl("MyCtrl.ascx")
> MyCtrl _mycontrol = (MyCtrl)LoadControl("MyCtrl.ascx")
> Page.Controls.Add(_mycontrol);
> _mycontrol.Text
> it will be loaded when some events fired.
> so how can I pass value to this user control from aspx template file.
>
Pass values from one user control to a user control on a different aspx page
I have an aspx page titled Search.aspx. Within this page, I have a user control titled Search.ascx. I want a user to input search terms, then on the click of a button, I want to pass the values to another page (SearchResults.aspx)...which will then display the results in a user control.
Can someone please give me some suggestions on how to do this?
Thanks a lot!Hello, what you can do is:
inside ur usercontrol, create a method that returns a dataset or datareader. that is, since u are searching using the usercontrol, then the result of ur search will be returned by a method of type either dataset or datareader.
and in ur code-behind of the aspx page u can use it as follows:
protected Myusercontrol user;
private DataSet ds = user.GetResults();
and then u might want to put it in a session variable or xml file and then u will be able to access it from the SearchResults.aspx
but why do u want to show results in another page ?
Saturday, March 24, 2012
Pass values to .ascx
Simple question really, how do i pass values to a .ascx file from .aspx file in c#?
ThanksYou can set property values from your ASPX code.
This site has ALL in the info you'll need, I just found it myself the other day.
Mastering Page-UserControl Communication
I tried this once with a page with a tabstrip + multipage setup. Each tab contained it's own ascx file.
I never got it to work the way I thought that it should. Check this newsgroup thread of mine to see if it applies to you:
http://msdn.microsoft.com/newsgroups/default.aspx?pg=35&lang=en&cr=US&guid=&sloc=en-us&dg=microsoft.public.dotnet.framework.aspnet.webcontrols&fltr=
I came accross the following code that solved my problem, might help others also.
in myuc.aspx
----------
<%-- declare any namespaces --%>
<%@. Register Tagprefix="Channel" Tagname="Title" src="http://pics.10026.com/?src=myuc.ascx"%>
<Channel:Title MyTitle="ABC" /
In myuc.ascx
----------
<script language="C#" runat="server">
public String MyTitle ="Not Set";
void Page_Load(Object Sender, EventArgs e)
{
if (Page.IsPostBack == false)
{
DisplayTitle.Text = MyTitle.ToString();
}
}
</Script>
<asp:Label id="DisplayTitle" runat="server" />
Check out the MSDN article by Scott Mitchell: An extensive Examination of User Controls
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnaspp/html/usercontrols.asp
Hello, you might check :An Extensive Examination of UserControls
regards
pass vaules between user control (ascx) and aspx page?
I've been able to pass a value from an aspx page to a user control called in it by adding the variable name = "somevalue" inside the user control tag.
But now I want to have a value from a user control's textbox after a button is clicked, availble to the aspx page.
This seems so simple... and yet I am so lost :)Hi,
In the user Control you can make public properties that will return the string writtin in the TextBox of User Control.
And then in the webform.apsx page you can make an instance of the UserControl and use that property to get the value which is returned from the TextBox.
Thank you!
I did a search on google for those terms and found this article ( http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconexposingpageletproperties.asp ), and it looks like it goes into detail on public properties of a user control.
Thanks. :)
Am I missing something?
Can the user control properties only be changed from the page that calls the user control, is it possible the other way around, so that in the user control the properties are changed and the page that calls the uc can access those? Two way street or only one way? :)
Friday, March 16, 2012
Passing a Value to a User Control
In the aspx page I have the Register directive:
<%@dotnet.itags.org. Register TagPrefix="Employees" TagName="uc" src="http://pics.10026.com/?src=uc.ascx" %>
and
<Employees:uc id="uc" Runat="server" />
In the user control, I have a dropdownlist named "MySelect", and in the codebehind, a string variable named "MySelectValue".
In the aspx page, I have a value that will determine the selected value of MySelect.
Here are some code snippets:
detail.aspx.cs:
private void Page_Load(object sender, System.EventArgs e) {
uc.MySelectValue = "SomeValue";
}uc.ascx.cs:
public class uc : System.Web.UI.UserControl {
public System.Web.UI.WebControls.DropDownList MySelect;public string MySelectValue {
get {return MySelect.SelectedValue;}
set {MySelect.SelectedValue = MySelectValue;}
}
}
When I build the solution, I receive a CS0120 error ("An object reference is required for the nonstatic field, method or property 'xxx.Employees.uc.MySelectValue'") on the 'uc.MySelectValue = "SomeValue";' line in detail.aspx.cs.
I've tried everything I can think of, and I am at a total loss. I just can't figure this one out.
Thanks for your help.Are you using the ID as the same name of the user control class name?
I am. Would that confuse the compiler?
it could, I guess :-P, unless you use fully qualified name when declaring & instanciating classes of that type. I would rather don't use the same name however
Thanks, but I'm still getting the same error after renaming the ID.
For anyone having the same issue, a user on the WROX forum, Imar, provided the solution. I neglected to add a declaration for the control in my aspx page.
protected Your.Name.Space.YourControl uc;
However, I still received the same error until I set the ID of the control *back* to "uc".