usercontrol event
-
hi, I am trying to use user control event in the aspx page. I have two textboxes txt1 and txt2. txt1 is in user control(ascx) page and txt2 is in aspx page. Now with ascx page, i have one popup calender say calender1 too. Now when i click that calender1, both textboxes should show the selected date. How to do that? I can show the date in the textbox txt1 but how to do it for txt2? thanks suchita
suchita
-
hi, I am trying to use user control event in the aspx page. I have two textboxes txt1 and txt2. txt1 is in user control(ascx) page and txt2 is in aspx page. Now with ascx page, i have one popup calender say calender1 too. Now when i click that calender1, both textboxes should show the selected date. How to do that? I can show the date in the textbox txt1 but how to do it for txt2? thanks suchita
suchita
If you are using JavaScript then get access to the textbox (document.getElementById(...)) and set the value. If you using a postback then you need to expose an event on the user control that the main page can subscribe to so it can receive a notification to set the text.
I know the language. I've read a book. - _Madmatt
-
If you are using JavaScript then get access to the textbox (document.getElementById(...)) and set the value. If you using a postback then you need to expose an event on the user control that the main page can subscribe to so it can receive a notification to set the text.
I know the language. I've read a book. - _Madmatt
-
hi, I am trying to use user control event in the aspx page. I have two textboxes txt1 and txt2. txt1 is in user control(ascx) page and txt2 is in aspx page. Now with ascx page, i have one popup calender say calender1 too. Now when i click that calender1, both textboxes should show the selected date. How to do that? I can show the date in the textbox txt1 but how to do it for txt2? thanks suchita
suchita
in your user control add a property like... (I assume that your user control has text box called
txt1
)private string displayText public string DisplayText { get { return \_displayText; } set { txt1.Text = \_displayText = value; } }
now suppose your user control has name
U1
, then just addU1.DisplayText = "something"
along withtxt2.Text = "something"
in your aspx page. and you are all set. :) -
in your user control add a property like... (I assume that your user control has text box called
txt1
)private string displayText public string DisplayText { get { return \_displayText; } set { txt1.Text = \_displayText = value; } }
now suppose your user control has name
U1
, then just addU1.DisplayText = "something"
along withtxt2.Text = "something"
in your aspx page. and you are all set. :) -
in your user control add a property like... (I assume that your user control has text box called
txt1
)private string displayText public string DisplayText { get { return \_displayText; } set { txt1.Text = \_displayText = value; } }
now suppose your user control has name
U1
, then just addU1.DisplayText = "something"
along withtxt2.Text = "something"
in your aspx page. and you are all set. :) -
but where to keep this code? aspx or ascx.. can you clarify it please ?? also i dont know where to define _displayText ??
suchita
ok the property (code block) you need to put inside your user control ascx.cs and the setting of the property (that U1.DisplayText = "something") should be inside your aspx.cs, in other words you add a property in the user control to just pass on the value you assign to the text box inside the user control, and then when you want to set the value, you use the user control name . dispalytext to assign the value to the text box inside the user control.
-
ok the property (code block) you need to put inside your user control ascx.cs and the setting of the property (that U1.DisplayText = "something") should be inside your aspx.cs, in other words you add a property in the user control to just pass on the value you assign to the text box inside the user control, and then when you want to set the value, you use the user control name . dispalytext to assign the value to the text box inside the user control.
so you mean In ascx page,you include the following code ? private string displayText ; public string DisplayText { get { return _displayText; } set { txt1.Text = _displayText = value; } } and what about in aspx page ?? and where to define _displayText ??
suchita
-
so you mean In ascx page,you include the following code ? private string displayText ; public string DisplayText { get { return _displayText; } set { txt1.Text = _displayText = value; } } and what about in aspx page ?? and where to define _displayText ??
suchita
SayamiSuchi wrote:
In ascx page,you include the following code ? private string displayText ; public string DisplayText { get { return _displayText; } set { txt1.Text = _displayText = value; } }
yes just after the class declaration and before any method (not inside any method).
SayamiSuchi wrote:
and what about in aspx page ??
here you define the value when you are assigning
txt2.Text = "something";
assign your user control variable name i.e.usercontrol1.DisplayText = "something";
SayamiSuchi wrote:
and where to define _displayText ??
that's typing mistake remove the _ (underscore from the code)
-
SayamiSuchi wrote:
In ascx page,you include the following code ? private string displayText ; public string DisplayText { get { return _displayText; } set { txt1.Text = _displayText = value; } }
yes just after the class declaration and before any method (not inside any method).
SayamiSuchi wrote:
and what about in aspx page ??
here you define the value when you are assigning
txt2.Text = "something";
assign your user control variable name i.e.usercontrol1.DisplayText = "something";
SayamiSuchi wrote:
and where to define _displayText ??
that's typing mistake remove the _ (underscore from the code)
-
its so confusing?? Sorry i didnt get anything... can you clearly write down in steps.. that will be more helpful ?? where to keep which part ??
suchita
modified on Monday, May 17, 2010 2:26 PM
As i assume that you want textbox1 and textbox2 to get their values during an event in your usercontrol (ascx file), you have to follow Mark Nischalkes instructions. For example: In your ascx file, you can set the Text Property of your textbox1 control (which is inside the ascx) easily by using:
TextBox1.Text = "xy";
This is pretty simple. In addition, you want to set the text of another TextBox (Textbox2)- which lives inside the aspx Page. This is more complex. You have to define an event/delegate in your .ascx file (usercontrol), which is subscribed by your page class (.aspx.cs) during page load and called when you set the textbox1.text property. This is the server-side solution. Another possibility would be to use javascript. I'm posting some example code now, which is not a 100% answer to your question, but it should help you out. I cannot modify this code more due to time restrictions: Define this delegate (maybe in a seperate file somewhere in your usercontrols namespace):
public delegate void OnSelectDelegate(string text);
In your Usercontrol:
public partial class MyUserControl: ....
{
public OnSelectDelegate OnSelect;private void SetText(string text) { TextBox1.Text = text; if (OnSelect != null) OnSelect(text); }
}
In your aspx Page during page-load:
this.MyUserControl1.OnSelect += new SomeNameSpace.OnSelectDelegate(OnSelect);
Include this method in your aspx:
private void OnSelect(string text)
{
TextBox2.Text = text;
}Hope i didnt include too many typing mistakes and this helps a bit. Good Luck
-
As i assume that you want textbox1 and textbox2 to get their values during an event in your usercontrol (ascx file), you have to follow Mark Nischalkes instructions. For example: In your ascx file, you can set the Text Property of your textbox1 control (which is inside the ascx) easily by using:
TextBox1.Text = "xy";
This is pretty simple. In addition, you want to set the text of another TextBox (Textbox2)- which lives inside the aspx Page. This is more complex. You have to define an event/delegate in your .ascx file (usercontrol), which is subscribed by your page class (.aspx.cs) during page load and called when you set the textbox1.text property. This is the server-side solution. Another possibility would be to use javascript. I'm posting some example code now, which is not a 100% answer to your question, but it should help you out. I cannot modify this code more due to time restrictions: Define this delegate (maybe in a seperate file somewhere in your usercontrols namespace):
public delegate void OnSelectDelegate(string text);
In your Usercontrol:
public partial class MyUserControl: ....
{
public OnSelectDelegate OnSelect;private void SetText(string text) { TextBox1.Text = text; if (OnSelect != null) OnSelect(text); }
}
In your aspx Page during page-load:
this.MyUserControl1.OnSelect += new SomeNameSpace.OnSelectDelegate(OnSelect);
Include this method in your aspx:
private void OnSelect(string text)
{
TextBox2.Text = text;
}Hope i didnt include too many typing mistakes and this helps a bit. Good Luck
-
hey this.MyUserControl1.OnSelect += new SomeNameSpace.OnSelectDelegate(OnSelect); hey here MyUserControl1 is the ascx class name or one of the control in ascx file ?
suchita
I think at this point you should give up and hand over the project to someone else. If this confuses you, you are most certainly over your head.
I know the language. I've read a book. - _Madmatt
-
I think at this point you should give up and hand over the project to someone else. If this confuses you, you are most certainly over your head.
I know the language. I've read a book. - _Madmatt
-
mmm! hard to understanding? let's say that you have an ascx page name Logo.ascx. Then when you add this ascx to a page named Test.aspx, your ascx User Control is named with ID="Logo1" in the Logo.ascx.cs add this:
//this is Logo.ascx
public string txt;
private string Text
{
get { return txt; }
set { txt = value; TextBox1.Text = value; }//You must have a TextBox named TextBox1
}and now you can call the public property of the ascx file! use this in Test.aspx or any page you've added the User Control
//and this is Test.aspx
protected void Page_Load(object sender, EventArgs e)
{//Once you have understand the property's world ^^ you can
Logo1.txt = "my text";//set the property value
TextBox2_GetText.Text = Logo1.txt;//or get it's value
};) good luck
-
mmm! hard to understanding? let's say that you have an ascx page name Logo.ascx. Then when you add this ascx to a page named Test.aspx, your ascx User Control is named with ID="Logo1" in the Logo.ascx.cs add this:
//this is Logo.ascx
public string txt;
private string Text
{
get { return txt; }
set { txt = value; TextBox1.Text = value; }//You must have a TextBox named TextBox1
}and now you can call the public property of the ascx file! use this in Test.aspx or any page you've added the User Control
//and this is Test.aspx
protected void Page_Load(object sender, EventArgs e)
{//Once you have understand the property's world ^^ you can
Logo1.txt = "my text";//set the property value
TextBox2_GetText.Text = Logo1.txt;//or get it's value
};) good luck
PunkIsNotDead wrote:
good luck
Good luck indeed if anyone uses this :omg: First, never expose a public variable, use proprties to expose them. Otherwise you have no way to validate and control access.
PunkIsNotDead wrote:
TextBox1.Text = value;
This is completely backward and unecessary. You have missed the OP's original request and intent and have added nothing but confusion to the subject.
I know the language. I've read a book. - _Madmatt
-
mmm! hard to understanding? let's say that you have an ascx page name Logo.ascx. Then when you add this ascx to a page named Test.aspx, your ascx User Control is named with ID="Logo1" in the Logo.ascx.cs add this:
//this is Logo.ascx
public string txt;
private string Text
{
get { return txt; }
set { txt = value; TextBox1.Text = value; }//You must have a TextBox named TextBox1
}and now you can call the public property of the ascx file! use this in Test.aspx or any page you've added the User Control
//and this is Test.aspx
protected void Page_Load(object sender, EventArgs e)
{//Once you have understand the property's world ^^ you can
Logo1.txt = "my text";//set the property value
TextBox2_GetText.Text = Logo1.txt;//or get it's value
};) good luck
Hey, thank you for the reply. Ya I can do it in page_load. That wasn't a problem but I need this with usercontrol event. Like for example, if you have one button in usercontrol(in .ascx page) and i want all those things in that button click. Can you tell further about this please??
suchita
-
mmm! hard to understanding? let's say that you have an ascx page name Logo.ascx. Then when you add this ascx to a page named Test.aspx, your ascx User Control is named with ID="Logo1" in the Logo.ascx.cs add this:
//this is Logo.ascx
public string txt;
private string Text
{
get { return txt; }
set { txt = value; TextBox1.Text = value; }//You must have a TextBox named TextBox1
}and now you can call the public property of the ascx file! use this in Test.aspx or any page you've added the User Control
//and this is Test.aspx
protected void Page_Load(object sender, EventArgs e)
{//Once you have understand the property's world ^^ you can
Logo1.txt = "my text";//set the property value
TextBox2_GetText.Text = Logo1.txt;//or get it's value
};) good luck
-
PunkIsNotDead wrote:
good luck
Good luck indeed if anyone uses this :omg: First, never expose a public variable, use proprties to expose them. Otherwise you have no way to validate and control access.
PunkIsNotDead wrote:
TextBox1.Text = value;
This is completely backward and unecessary. You have missed the OP's original request and intent and have added nothing but confusion to the subject.
I know the language. I've read a book. - _Madmatt
Ok thanks mark! but that was the first thing that came to mi mind! I'll take and follow your suggestions! X|
-
Hey, I forgot to tell one more thing. I can do this in the button click of the aspx page. But I want my button in user control in ascx page for this and in that button click, i want all those things..
suchita
Ok! I know what you need! create a class (named ResponseEventArgs) that inherits the EventArgs and add a property named myValue. Like this
public class ResponseEventArgs : EventArgs
{
private String _myValue;
public String MyValue
{
get { return _myValue; }
set { _myValue = value; }
}
}Then in your ascx user control, add the event and the correct delegate
public delegate void ResponseEventHandler(object sender, ResponseEventArgs d);
public event ResponseEventHandler ResultOfMyText;
here you've added the event ResultOfMyText and the handler ResponseEventHandler. Now in the ascx user control add the event as a virtual function
[Description("Event that fires after button click of user control")]
protected virtual void OnResult_Show(ResponseEventArgs re)
{
if (ResultOfMyText != null)
ResultOfMyText(this, re);}
Now in button Click event of the user control, add the correct value to MyValue Property
//here is button_Click event of the user control
ResponseEventArgs re = new ResponseEventArgs();
re.MyValue = Txt_Value.Text;//You need a textbox named Txt_Value
OnResult_Show(re);then when you've added this user control, you'll see that it has a event named ResultOfMyText. Trigger that event, and you'll see that you have a d.MyValue in ResultEventArgs and will execute that event every time that you click on the button inside the user control. I hope this won't be confused ;)