Answered by:
generate onClientClick in Render(HtmlTextWriter writer)

Question
-
User430679683 posted
Hi all,
I'm writing a "toolbar" component below is one of my button 's example.
- The question is, I want to generate out the onclientclick event in this function. But onclientclick is not available in HtmlTextWriterAttribute. May I know how am i going to code it?
- second question is, if I want to public out this compoent's "add button" able to access from outside like example:
toolbarcomponent.BtnAdd;
how am i going to do it? because the render is only render out in html, but i can't access it.
protected override void Render(HtmlTextWriter writer)
{
writer.AddAttribute(HtmlTextWriterAttribute.Type, "button");
writer.AddAttribute(HtmlTextWriterAttribute.Name, this.UniqueID + "_tb_add");
if ((bool)ViewState["AddDisable"])
writer.AddAttribute(HtmlTextWriterAttribute.Disabled, "true");
writer.AddAttribute(HtmlTextWriterAttribute.Value, addText);
writer.AddAttribute(HtmlTextWriterAttribute.Width, "100");
writer.AddAttribute(HtmlTextWriterAttribute.Src, "/Images/add.ico");writer.AddAttribute(HtmlTextWriterAttribute.Onclick, Page.GetPostBackClientEvent(this, "OnAddButtonClick"));
writer.RenderBeginTag(HtmlTextWriterTag.Input);
writer.RenderEndTag();base.Render(writer);
}
Thank you
Regards,
TanMH
Monday, October 19, 2009 12:12 AM
Answers
-
User430679683 posted
thx steelymar,
I think the method I can't use is because i already have one server post back for onclick. which I want is onclientclick.
but any way. i found a solution.
add those code at constructor:
buttonAdd = new Button();
buttonAdd.Text = addText;
buttonAdd.ID = this.UniqueID + "_tb_add";
buttonAdd.Width = 100;buttonAdd.OnClientClick = "My javascript function();";
this.Controls.Add(buttonAdd);
buttonAdd.Click += new EventHandler(OnAddButtonClick);then in the render it should come out the thing you wan.
if no then we can actually use buttonAdd.RenderControl(writer);
These method will able to solved my first and second question. :)
Thank you for reply.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, October 19, 2009 8:28 PM
All replies
-
User191633014 posted
for the first question:
writer.AddAttribute(HtmlTextWriterAttribute.Onclick, "MyFunction");
Monday, October 19, 2009 2:17 AM -
User430679683 posted
thx steelymar,
I think the method I can't use is because i already have one server post back for onclick. which I want is onclientclick.
but any way. i found a solution.
add those code at constructor:
buttonAdd = new Button();
buttonAdd.Text = addText;
buttonAdd.ID = this.UniqueID + "_tb_add";
buttonAdd.Width = 100;buttonAdd.OnClientClick = "My javascript function();";
this.Controls.Add(buttonAdd);
buttonAdd.Click += new EventHandler(OnAddButtonClick);then in the render it should come out the thing you wan.
if no then we can actually use buttonAdd.RenderControl(writer);
These method will able to solved my first and second question. :)
Thank you for reply.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, October 19, 2009 8:28 PM