简体   繁体   中英

how to create my custom control C#?

i try to write my own control.But my control' html has problem: Look like

在此处输入图片说明

在此处输入图片说明


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls.WebParts; // Ekle
using System.Web.UI.WebControls; // Ekle
using System.Web.UI;
using System.Collections;
using System.Threading;

namespace MyFilterControl
{

    public class FilterControl : WebPart
    {
        internal List<Content> list { get; set; }
        public Button BtnSearch { get; set; }
        public event EventHandler Click;
        public string FilterButtonText { get; set; }
        public String PhID { get; set; }
        internal PlaceHolder PlhControl { get; set; }
        private string controlWidth = "10";
        public FilterControl()
        {

            list = new List<Content>();
            BtnSearch = new Button();
            PlhControl = new PlaceHolder();
            PlhControl.ID = Guid.NewGuid().ToString();
            if (string.IsNullOrEmpty(PhID))
            {
                BtnSearch.ID = "btnSearch";
            }
            else
                BtnSearch.ID = PhID;
            if (string.IsNullOrEmpty(FilterButtonText))
            {
                 BtnSearch.Text = "Search";
            }
            else
                BtnSearch.Text = FilterButtonText;
            BtnSearch.Click += new EventHandler(BtnSearch_Click);
        }
        private Table mainTable = new Table();
        protected override void CreateChildControls()
        {
            base.CreateChildControls();
            TableRow tblRow = new TableRow();
            TableCell tblCell1 = new TableCell();

            PlhControl.Controls.Add(new LiteralControl("<table><tr>"));
            tblCell1.Controls.Add(PlhControl);
            TableCell tblCell2 = new TableCell();
            tblCell2.Controls.Add(BtnSearch);
            tblRow.Cells.Add(tblCell1);
            tblRow.Cells.Add(tblCell2);
            mainTable.Rows.Add(tblRow);
            this.Controls.Add(mainTable);
        }
        void BtnSearch_Click(object sender, EventArgs e)
        {
            PlhControl.Controls.Add(new LiteralControl("</tr><table>"));

            foreach (var item in PlhControl.Controls)
            {
                if (item is MyTextBoxControl)
                {
                    MyTextBoxControl t1 = (MyTextBoxControl)item;
                    if (t1.Text != "")
                    {
                        Add(new Content() { FieldName = t1.ID, Data = t1.Text, ID = Guid.NewGuid().ToString(), dataType = t1.dataType, filter=t1.filter });
                    }
                }
            }
            Click(this, e);

        }

        public string Query()
        {
            string Qry = String.Empty;
            int i = 0;
            foreach (var item in list)
            {
                switch (item.filter)
                {
                    case Filter.BeginsWith: Qry += String.Format(item.FieldName.ToString() + ".StartsWith({0}) && ", "@" + i.ToString());
                        break;
                    case Filter.EndsWith: Qry += String.Format(item.FieldName.ToString() + ".EndsWith({0}) && ", "@" + i.ToString());
                        break;
                    case Filter.Contains: Qry += String.Format(item.FieldName.ToString() + ".Contains({0}) && ", "@"+i.ToString());
                        break;
                    case Filter.Default: Qry += String.Format("{0}=={1} && ", item.FieldName, "@" + i.ToString());
                        break;
                    default:
                        break;
                }

                i++;
            }
            Qry = Qry.TrimEnd('&', '&', ' ');
            return Qry;
        }

        public object[] QueryParams()
        {
            ArrayList arrList = new ArrayList();
            foreach (var item in list)
            {
                if (item.dataType == DataType.String) 
                arrList.Add(item.Data);
                else if (item.dataType == DataType.Int32)
                {
                    int intVal = Convert.ToInt32(item.Data);
                    arrList.Add(intVal);
                }
            }
            object[] strArray = arrList.ToArray(typeof(object)) as object[];
            return strArray;
        }

        public string id { get; set; }
        public void AddItem(ContentItem content)
        {
            PlhControl.Controls.Add(new LiteralControl("<td  style=\"text-align:left\">"));
            PlhControl.Controls.Add(new Label() { ID = Guid.NewGuid().ToString(), Text = content.LabelName });
            PlhControl.Controls.Add(new LiteralControl(":</td>"));
            PlhControl.Controls.Add(new LiteralControl("<td  style=\"text-align:left\">"));
            PlhControl.Controls.Add(new MyTextBoxControl() { ID = content.FieldName, dataType = content.dataType, filter = content.filter });
            PlhControl.Controls.Add(new LiteralControl("</td>"));
        }
        private void Add(Content content)
        {
            list.Add(new Content() { ID = content.ID, Data = content.Data, FieldName = content.FieldName, dataType=content.dataType, filter= content.filter });
        }
    }

    internal class Content
    {
        internal string ID { get; set; }
        public string Data { get; set; }
        public string FieldName { get; set; }
        public DataType dataType { get; set; }
        public Filter filter { get; set; }
    }

    public class ContentItem
    {
        private string ID { get; set; }
        public string LabelName { get; set; }
        public string FieldName { get; set; }
        public DataType dataType { get; set; }
        public Filter filter { get; set; }
    }

    public class MyTextBoxControl : TextBox
    {
        public DataType dataType { get; set; }
        public Filter filter { get; set; }
    }

    public enum DataType
    {
        String,Int32
    }

    public enum Filter
    {
        BeginsWith,EndsWith,Contains,Default
    }
}


You can place each control in a table cell. Or you can place everything in a div with a fixed heigh.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM