开发者

How to write a custom Control in asp.net?

i try to write a custom user control to filter data to reflect to GridView.if you look USAGE i try to write:

How to write a custom Control in asp.net?

important: i am using DynamicLinq!!!!!

But Error return me when Clicking Search Button:

Error:

No property or field 'Chai' exists in type 'Product'


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.Reflection;// Ekle

namespace MyControl2
{
    public class TestControl : WebPart
    {
        internal List<Content> list { get; set; }
        public Button BtnSearch { get; set; }
        public event EventHandler Click;
        public TestControl()
        {
            list = new List<Content>();
            BtnSearch = new Button();
            BtnSearch.ID = "btnSearch";
            BtnSearch.Text = "Search";
            this.Controls.Add(BtnSearch);
            BtnSearch.Click += new EventHandler(BtnSearch_Click);

        }

        void BtnSearch_Click(object sender, EventArgs e)
        {
            this.Controls.Add(new LiteralControl("</tr><table>"));

            foreach (var item in this.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  });
                    }
                }
            }
            Click(this, e);

        }

        public string QueryParameters()
        {
            string Qry=String.Empty;
            int i = 0;
            foreach (var item in list)
            {
                Qry += item.FieldName + "=" + item.Data + " and ";
                i++;
            }
            Qry = Qry.TrimEnd('A', 'n', 'd',' ');
            return Qry;开发者_如何学运维
        }
        protected override void CreateChildControls()
        {
                base.CreateChildControls();
                this.Controls.Add(new LiteralControl("<table><tr>"));
        }
        public string id { get; set; }
        public void AddItem(ContentItem content )
        {
            this.Controls.Add(new LiteralControl("<td>"));
            this.Controls.Add(new Label() { ID = Guid.NewGuid().ToString(), Text = content.LabelName });
            this.Controls.Add(new LiteralControl(":</td>"));
            this.Controls.Add(new LiteralControl("<td>"));
            this.Controls.Add(new MyTextBoxControl() { ID = content.FieldName, DataType= content.DataType });
            this.Controls.Add(new LiteralControl("</td>"));
       }
        private void Add(Content content)
        {
            list.Add(new Content() { ID = content.ID, Data = content.Data, FieldName = content.FieldName });
        }
    }

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

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

    }

    public class MyTextBoxControl : TextBox
    {
        public string DataType { get; set; }
    }
}

USAGE


namespace WebApplication1
{
    public partial class _Default : System.Web.UI.Page
    {
        protected override void OnInit(EventArgs e)
        {
                base.OnInit(e);
                FilterControl1.AddItem(new FlyAntFilterControl.ContentItem() { LabelName = "Product", FieldName = "ProductName" });
                FilterControl1.Click += new EventHandler(FilterControl1_Click);

        }

        void FilterControl1_Click(object sender, EventArgs e)
        {
            using (DataClasses1DataContext DataCtx = new DataClasses1DataContext())
            {
                var qry = DataCtx.Products.Where(FilterControl1.QueryParameters());
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {


        }
    }
}

ScreenShoots:

How to write a custom Control in asp.net?

How to write a custom Control in asp.net?


@Penguen -

You have a Product class with properties or attributes called:

  • ProductID
  • ProductName
  • CategoryID

And actual Products with VALUES e.g.:

ProductId: 1 ProductName: Boston Crabmeat CategoryID: 1

ProductId: 2 ProductName: Chai CategoryID: 2

ProductId: 3 ProductName: Chocolate Biscuits CategoryID: 3

This isn't exactly what your code is trying to do, but should help you see what's wrong. Let's create an instance of a Product:

myProduct = new Product(2,"Chai", 2);

To access that product's values, you should do something like this:

myProduct.ProductName;  //Correct :-)

But your code is doing something like this:

myProduct.Chai;         //Wrong :-(    

"Chai" is not a property - ProductName is the property. One of your Product instances has a value of "Chai".

In your GridView you might be doing something like:

<%# Eval("Chai") %> 

when it should be:

<%# Eval("ProductName") %> 

Hard to tell for sure until you post a fuller code example ;-)

Hope this helps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜