I need held with creating custom control. I have created Custom control with a textbox and button. Control has a few properties like ServicePath, ServiceMethod to hold a string variable for calling a webservice. This is done through WebServiceProxy.invoke,
but there is a problem. I need to pass these do embed javascript.
.aspx
namespace WebControls
{
[DefaultProperty("Text")]
[ToolboxData("<{0}:Text2Label runat=server></{0}:Text2Label>")]
public class Text2Label : CompositeControl, INamingContainer
{
protected TextBox txtName = new TextBox();
protected Label lblPopis = new Label();
protected ImageButton ibOK = new ImageButton();
private string _controlPopis;
private string _controlText;
private string _servicePath;
private string _serviceMethod;
private Unit _controlWidth;
private Unit _controlHeight;
txtName.Attributes.Add("style", "visibility:hidden;");
ibOK.Attributes.Add("style", "visibility:hidden;");
// Create user interface by using Controls collection
}
#region Properties
[Bindable(true)]
[Category("Appearance")]
[DefaultValue("")]
[Localizable(true)]
public string ControlText
{
get
{
return _controlText;
}
set
{
_controlText = value;
}
}
[Bindable(true)]
[Category("Appearance")]
[DefaultValue("")]
[Localizable(true)]
public string ControlPopis
{
get
{
return _controlPopis;
}
set
{
_controlPopis = value;
}
}
[Category("Appearance")]
[DefaultValue("70")]
[Localizable(true)]
public Unit ControlWidth
{
get
{
return _controlWidth;
}
set
{
_controlWidth = value;
}
}
[Category("Appearance")]
[DefaultValue("20")]
[Localizable(true)]
public Unit ControlHeight
{
get
{
return _controlHeight;
}
set
{
_controlHeight = value;
}
}
[Category("Appearance")]
[DefaultValue("")]
[Localizable(true)]
public string ServiceMethod
{
get
{
return _serviceMethod;
}
set
{
_serviceMethod = value;
}
}
[Category("Appearance")]
[DefaultValue("")]
[UrlProperty()]
public string ServicePath
{
get
{
return _servicePath;
}
set
{
_servicePath = value;
}
}
#endregion
/// <summary>
/// Override of the Control.OnPreRender method
/// </summary>
/// <param name="e">Event arguments.</param>
protected override void OnPreRender(System.EventArgs e)
{
base.OnPreRender(e);
function onOKClick() {
//Here i need to get values from property of a custom control
Store2SQL(servicepath, servicemethod);
return false;
}
function Store2SQL() {
Sys.Net.WebServiceProxy.invoke(servicepath, servicemethod, false);
}
The js file is working OK and also the webserviceproxy with hand-typed default values. Am i missing something??? Do I need to create Ajax extender control for the textbox or button to get property values??
Ghost7
0 Points
2 Posts
.NET Custom composite server control pass property to embed javascript
Mar 26, 2010 09:26 PM|LINK
Hello everybody
I need held with creating custom control. I have created Custom control with a textbox and button. Control has a few properties like ServicePath, ServiceMethod to hold a string variable for calling a webservice. This is done through WebServiceProxy.invoke, but there is a problem. I need to pass these do embed javascript.
.aspx
namespace WebControls
{
[DefaultProperty("Text")]
[ToolboxData("<{0}:Text2Label runat=server></{0}:Text2Label>")]
public class Text2Label : CompositeControl, INamingContainer
{
protected TextBox txtName = new TextBox();
protected Label lblPopis = new Label();
protected ImageButton ibOK = new ImageButton();
private string _controlPopis;
private string _controlText;
private string _servicePath;
private string _serviceMethod;
private Unit _controlWidth;
private Unit _controlHeight;
public override ControlCollection Controls
{
get
{
EnsureChildControls();
return base.Controls;
}
}
protected override void OnInit(EventArgs e)
{
EnsureChildControls();
lblPopis.Text = "<b>" + _controlPopis + ": </b>";
ibOK.ImageUrl = Page.ClientScript.GetWebResourceUrl(this.GetType(), "WebControls.Images.accept.png");
base.OnInit(e);
}
protected override void CreateChildControls()
{
Controls.Clear();
InitializeDefaultSettings();
txtName.Attributes.Add("style", "visibility:hidden;");
ibOK.Attributes.Add("style", "visibility:hidden;");
// Create user interface by using Controls collection
Controls.Add(new LiteralControl("<table>"));
Controls.Add(new LiteralControl("<tr>"));
Controls.Add(new LiteralControl("<td>"));
Controls.Add(lblPopis);
Controls.Add(new LiteralControl("</td>"));
Controls.Add(new LiteralControl("<td>"));
Controls.Add(txtName);
Controls.Add(new LiteralControl("</td>"));
Controls.Add(new LiteralControl("<td>"));
Controls.Add(ibOK);
Controls.Add(new LiteralControl("</td>"));
Controls.Add(new LiteralControl("</tr>"));
Controls.Add(new LiteralControl("</table>"));
}
private void InitializeDefaultSettings()
{
ibOK.Attributes.Add("id", "ibOK");
ibOK.Height = Unit.Pixel(26);
ibOK.AlternateText = "Potvrdiť zmenu";
ibOK.Attributes.Add("OnClick", "return onOKClick();");
txtName.Attributes.Add("id", "txtName");
txtName.BorderColor = Color.Blue;
txtName.BorderStyle = BorderStyle.Inset;
txtName.BorderWidth = Unit.Pixel(1);
txtName.Width = Unit.Pixel(0);
txtName.Height = Unit.Pixel(0);
}
#region Properties
[Bindable(true)]
[Category("Appearance")]
[DefaultValue("")]
[Localizable(true)]
public string ControlText
{
get
{
return _controlText;
}
set
{
_controlText = value;
}
}
[Bindable(true)]
[Category("Appearance")]
[DefaultValue("")]
[Localizable(true)]
public string ControlPopis
{
get
{
return _controlPopis;
}
set
{
_controlPopis = value;
}
}
[Category("Appearance")]
[DefaultValue("70")]
[Localizable(true)]
public Unit ControlWidth
{
get
{
return _controlWidth;
}
set
{
_controlWidth = value;
}
}
[Category("Appearance")]
[DefaultValue("20")]
[Localizable(true)]
public Unit ControlHeight
{
get
{
return _controlHeight;
}
set
{
_controlHeight = value;
}
}
[Category("Appearance")]
[DefaultValue("")]
[Localizable(true)]
public string ServiceMethod
{
get
{
return _serviceMethod;
}
set
{
_serviceMethod = value;
}
}
[Category("Appearance")]
[DefaultValue("")]
[UrlProperty()]
public string ServicePath
{
get
{
return _servicePath;
}
set
{
_servicePath = value;
}
}
#endregion
/// <summary>
/// Override of the Control.OnPreRender method
/// </summary>
/// <param name="e">Event arguments.</param>
protected override void OnPreRender(System.EventArgs e)
{
base.OnPreRender(e);
//Register script library.
ScriptManagerHelper.RegisterClientScriptResource(
this,
typeof(Text2Label),
"WebControls.ControlEvents.js");
}
}
}
.js file
function onOKClick() {
//Here i need to get values from property of a custom control
Store2SQL(servicepath, servicemethod);
return false;
}
function Store2SQL() {
Sys.Net.WebServiceProxy.invoke(servicepath, servicemethod, false);
}
The js file is working OK and also the webserviceproxy with hand-typed default values. Am i missing something??? Do I need to create Ajax extender control for the textbox or button to get property values??
Or do you know another approach???
thx
ghost7
Ghost7
0 Points
2 Posts
Re: .NET Custom composite server control pass property to embed javascript
Mar 29, 2010 02:43 PM|LINK
Hellooo
It is really interesting, that no one have an answer to this question.
I found one, but i dont know if it a clean solution, but here it is:
protected override void OnInit(EventArgs e)
{
this.ID = "Txt2Lbl";
this.Attributes.Add("controlText", _controlText);
//and so on...
}
js file:
function onOKClick()
{
var r = document.getElementById('Txt2Lbl');
var att = r.getAttribute('controlText');
}
So that is it...
A hope it helps...
Ghost7