My requirement is to get the latest inner text present in the anchor tag when the button in the client page is clicked.
When the client page button is clicked, CreateChildControl() refreshes every time, so the latest value present in the custom control is not retained.
when i tried to return latest text from custom control property, CreateChildControl() refreshes every time when FindControl() is issued.
How to get and set the latest text to custom control?
Below is the code i used for reference?
public class Dropdown3D : WebControl, IEditableTextControl, ITextControl
{
protected override void CreateChildControls()
{
base.CreateChildControls();
// control is created & added in controls container
}
public string Text
{
get
{
return ((FindControl("rootDiv").FindControl("anchorID") as HtmlAnchor)).InnerText;
}
set
{
((FindControl("rootDiv").FindControl("anchorID") as HtmlAnchor)).InnerText = value;
}
}
}
When i clicked the client button, i'm retreiving the default text "Select .." instead of latest value set by the javascript. How to retain the latest value set by the javascript in servercontrol?
anandhan
Member
1 Points
44 Posts
Get & Set Selected Text asp.net custom web control
Apr 23, 2012 09:20 AM|LINK
Hi,
My requirement is to get the latest inner text present in the anchor tag when the button in the client page is clicked.
How to get and set the latest text to custom control?
Below is the code i used for reference?
public class Dropdown3D : WebControl, IEditableTextControl, ITextControl { protected override void CreateChildControls() { base.CreateChildControls(); // control is created & added in controls container } public string Text { get { return ((FindControl("rootDiv").FindControl("anchorID") as HtmlAnchor)).InnerText; } set { ((FindControl("rootDiv").FindControl("anchorID") as HtmlAnchor)).InnerText = value; } } }Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: Get & Set Selected Text asp.net custom web control
Apr 25, 2012 01:35 AM|LINK
Please set ChildControlsCreated=true,this will make the control run only once;
And then plz keep the latest value into a private property in a nested ViewState to keep the value:
private string LatestValue { get { return ViewState["lvalue"]==null?string.Empty:ViewState["lvalue"].ToString(); } set { ViewState["lvalue"] = value; } } protected override void CreateChildControls() { base.CreateChildControls(); //Do what you want here…… LatestValue = inivalue;…… ChildControlsCreated=true; }anandhan
Member
1 Points
44 Posts
Re: Get & Set Selected Text asp.net custom web control
Apr 27, 2012 06:49 AM|LINK
Hi,
Eventhough i set ChildControlsCreated=true, CreateChildControls() are called each time when some button in client page is clicked.
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: Get & Set Selected Text asp.net custom web control
Apr 28, 2012 09:16 AM|LINK
Hello again:)
would you mind showing us your full codes?
anandhan
Member
1 Points
44 Posts
Re: Get & Set Selected Text asp.net custom web control
Apr 28, 2012 08:50 PM|LINK
ServerControl1.cs
[assembly: WebResource("ServerControl1.Scripts.JScript1.js", "text/javascript")] namespace ServerControl1 { [DefaultProperty("Text")] [ToolboxData("<{0}:ServerControl1 runat=server></{0}:ServerControl1>")] public class ServerControl1 : WebControl { public List<string> ListItems { get { return ViewState["items"] as List<string>; } set { ViewState["items"] = value; } } public string Text { get { return (FindControl("middleDiv").FindControl("anchorID") as HtmlAnchor).InnerText; } set { ((FindControl("middleDiv").FindControl("anchorID") as HtmlAnchor)).InnerText = value; } } protected override void CreateChildControls() { base.CreateChildControls(); HtmlGenericControl selectedTextContainer = new HtmlGenericControl("div"); selectedTextContainer.ClientIDMode = System.Web.UI.ClientIDMode.Static; selectedTextContainer.ID = "middleDiv"; HtmlAnchor selectedTextAnchor = new HtmlAnchor(); selectedTextAnchor.ClientIDMode = System.Web.UI.ClientIDMode.Static; selectedTextAnchor.ID = "anchorID"; selectedTextAnchor.HRef = ""; selectedTextContainer.Controls.Add(selectedTextAnchor); HtmlGenericControl unList = new HtmlGenericControl("ul"); foreach (string item in ListItems) { HtmlGenericControl li = new HtmlGenericControl("li"); HtmlAnchor anchor = new HtmlAnchor(); anchor.HRef = ""; anchor.Attributes.Add("onclick", "updateData()"); anchor.InnerText = item; li.Controls.Add(anchor); unList.Controls.Add(li); } selectedTextContainer.Controls.Add(unList); Controls.Add(selectedTextContainer); ChildControlsCreated = true; } protected override void OnPreRender(EventArgs e) { base.OnPreRender(e); string resourceName = "ServerControl1.Scripts.JScript1.js"; ClientScriptManager cs = this.Page.ClientScript; cs.RegisterClientScriptResource(typeof(ServerControl1), resourceName); } } }JScript1.js
function updateData() { var evt = window.event || arguments.callee.caller.arguments[0]; var target = evt.target || evt.srcElement; var anchor = document.getElementById("anchorID"); anchor.innerText = target.innerText; return false; }TestPage Codebehind
protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { List<string> items = GetDataSource(); ServerControl1.ListItems = items; ServerControl1.Text = "Select .."; } } protected void ClientButton_Click(object sender, EventArgs e) { string selectedText = ServerControl1.Text; }When i clicked the client button, i'm retreiving the default text "Select .." instead of latest value set by the javascript. How to retain the latest value set by the javascript in servercontrol?
anandhan
Member
1 Points
44 Posts
Re: Get & Set Selected Text asp.net custom web control
May 02, 2012 11:14 AM|LINK
Solution:
http://forums.asp.net/t/1798099.aspx/1?Retain+changes+made+by+javascript+in+custom+control