Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

CheckBox list custom web server control;

Posted on 2005-04-18
8
Medium Priority
?
198 Views
Last Modified: 2010-04-16
Hello,

I'm creating cutom webserver control (similar to checkbox list) usiing the following code:
      public class TestControl : System.Web.UI.WebControls.WebControl
      {
            public ArrayList TestList = new ArrayList();
            protected override void Render(HtmlTextWriter output)
            {
                  try
                  {
                        for (int i=0; i<TestList.Count; i++)
                        {
                        string name = TestList[i].ToString();
                        output.Write("<input type=\"checkbox\" name=\"");
                        output.Write(name);
                        output.Write("\" value=\"");
                        output.Write(i);
                        output.Write("\"");
                        output.Write(">");
                        }
                  }
                  catch(Exception ex)
                  {
                        output.Write("Errror building CheckBox List:<br>");
                        output.Write(ex.InnerException);
                  }
            }

      }

I'm passing values to this from main page using:

            private void Page_Load(object sender, System.EventArgs e)
            {
                  ArrayList test = new ArrayList();
                  test.Add("val1");
                  test.Add("val2");
                  this.TestControl1.TestList = test;

            }

It is creating the checkboxes fine with the values  I have given . I have a text box on the main page. When i check the checkbox, its corresponding name should populate in the text box.(by raising a postback event) How can I do this?

Thanks
0
Comment
Question by:praveenuni
  • 4
  • 4
8 Comments
 
LVL 13

Expert Comment

by:dungla
ID: 13812314
Here is my example:
// write a javascript function in default.aspx page (html mode)
// TextBox1 is an Asp:TextBox control of default.aspx page
function SetValue(strValue)
{
      document.all("TextBox1").value = document.all(strValue).name;
}
// user control
using System.Collections;

namespace WebApp
{
      using System;
      using System.Data;
      using System.Drawing;
      using System.Web;
      using System.Web.UI.WebControls;
      using System.Web.UI.HtmlControls;
      using System.Data.OleDb;

      /// <summary>
      ///            Summary description for MyControl.
      /// </summary>
      public class MyControl : System.Web.UI.UserControl
      {
            public ArrayList TestList = new ArrayList();
            private void Page_Load(object sender, System.EventArgs e)
            {
            }

            protected override void Render(System.Web.UI.HtmlTextWriter writer)
            {
                  try
                  {
                        for (int i = 0; i < TestList.Count; i++)
                        {
                              string strName = TestList[i].ToString();
                              writer.Write("<input type=\"checkbox\" name=\"");
                              writer.Write(strName);
                              writer.Write("\" value=\"");
                              writer.Write(i);
                              // add javascript function
                              writer.Write("\" onclick=\"javascript:SetValue('" + strName + "');\">");
                        }
                  }
                  catch (Exception ex)
                  {
                        writer.Write("Error building checkbox list:<br>");
                        writer.Write(ex.InnerException);
                  }
            }


            #region Web Form Designer generated code
            override protected void OnInit(EventArgs e)
            {
                  //
                  // CODEGEN: This call is required by the ASP.NET Web Form Designer.
                  //
                  InitializeComponent();
                  base.OnInit(e);
            }
            
            /// <summary>
            ///            Required method for Designer support - do not modify
            ///            the contents of this method with the code editor.
            /// </summary>
            private void InitializeComponent()
            {
                  this.Load += new System.EventHandler(this.Page_Load);

            }
            #endregion
      }
}
0
 
LVL 13

Expert Comment

by:dungla
ID: 13812318
If you want to rising postback event, you shoud use writer to write down Asp:CheckBox not html checkbox
0
 

Author Comment

by:praveenuni
ID: 13817215
Can we write asp controls using output.write/writer.write. I think we can write only html controls. Correct me if I'm wrong

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:praveenuni
ID: 13818988
Can I raise the event without using javascript? (event when I check/uncheck checkbox)

thanks
0
 
LVL 13

Expert Comment

by:dungla
ID: 13820905
Yes, we can write only html controls (sorry for my wrong solution about write down asp:checkbox). Therefore I think that we could not raise the event without using javascript
0
 

Author Comment

by:praveenuni
ID: 13820909
Could you please tell me how to call the function in C# code-behind using javascript. (like raising event)
0
 
LVL 13

Accepted Solution

by:
dungla earned 2000 total points
ID: 13820939
// write a javascript function in default.aspx page (html mode)
// TextBox1 is an Asp:TextBox control of default.aspx page
function SetValue(strValue)
{
     <%SetValue();%>;
     alert('<%=strName%>');
}
// default.aspx.cs
// public variable
public string strName = string.Empty;
public void SetValue()
{
strName = "dungla";
}
0
 

Author Comment

by:praveenuni
ID: 13826078
Hello dungla,

In the above example you are passing value from C# to javascript. But as you can see in the code above, I want to raise an event (simply call a function) from javascript. Could you please tell me how can I achieve this?

Thanks
Praveen
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?

571 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question