如何创建我的自定义控件C#?

时间:2011-02-25 08:41:07

标签: c# .net asp.net html visual-studio-2008

我尝试编写自己的控件。但我的控件'html有问题:看起来像

enter image description here

我的搜索按钮如上图所示。但我需要在下面:我该怎么做?

enter image description here


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
    }
}


1 个答案:

答案 0 :(得分:0)

您可以将每个控件放在表格单元格中。或者你可以将所有东西放在一个固定高度的div中。