无法在Gridview中找到TemplateField控件

时间:2013-01-29 17:01:40

标签: asp.net gridview

我开始这个混乱因为GridViewInsertEventArgs.NewValues没有从TemplateField

返回值

我尝试访问我的DropDownList ctl00$MainContent$GridView1$ctl13$ctl07我找到了它周围的所有项目,但是

没有e.Row.Controls[7].Controls[0];

e.Row.Controls[1].Controls[0] returns ctl00$MainContent$GridView1$ctl13$ctl01
e.Row.Controls[2].Controls[0] returns ctl00$MainContent$GridView1$ctl13$ctl02
e.Row.Controls[3].Controls[0] returns ctl00$MainContent$GridView1$ctl13$ctl03

等等只有7个不起作用

 also tried 

e.Row.Cells[7].Controls[0]

似乎服务器根本没有;看到控件

<table class="table" cellspacing="0" cellpadding="30" rules="all" border="1" id="MainContent_GridView1" style="color:#333333;border-color:Silver;border-style:Double;width:100%;border-collapse:collapse;">
    <tr style="color:White;background-color:#507CD1;font-weight:bold;">
        <th scope="col">Action</th>
        <th scope="col">SystemIdentifier_1</th>
        <th scope="col">SystemName_1</th>
        <th scope="col">Description_1_1</th>
        <th scope="col">DefaultValue_1</th>
        <th scope="col">DefaultDeviceTypeRef_1</th>
        <th scope="col">SqlDbType_1</th>
        <th scope="col">MaxParamSize_1</th>
    </tr>
    <tr style="background-color:White;">
        <td><a href="javascript:__doPostBack(&#39;ctl00$MainContent$GridView1$ctl13$ctl00&#39;,&#39;&#39;)" style="color:#333333;">Insert</a>&nbsp;</td>
        <td><input name="ctl00$MainContent$GridView1$ctl13$ctl03" type="text" title="SystemIdentifier_1" /></td>
        <td><input name="ctl00$MainContent$GridView1$ctl13$ctl04" type="text" title="SystemName_1" /></td>
        <td><input name="ctl00$MainContent$GridView1$ctl13$ctl05" type="text" title="Description_1_1" /></td>
        <td><input name="ctl00$MainContent$GridView1$ctl13$ctl06" type="text" title="DefaultValue_1" /></td>
        <td>
            <select name="ctl00$MainContent$GridView1$ctl13$ctl07">
                <option value="1">TC4020G</option>
                <option value="2">Warehouse Station</option>
                <option value="3">IssueStation</option>
                <option value="4">PersonalizationStation</option>
                <option value="5">Smart Card</option>
                <option value="6">Loader</option>
                <option value="7">Server Service</option>
            </select>
        </td>
        <td><input name="ctl00$MainContent$GridView1$ctl13$ctl08" type="text" title="SqlDbType_1" /></td>
        <td><input name="ctl00$MainContent$GridView1$ctl13$ctl09" type="text" title="MaxParamSize_1" /></td>
    </tr>

我的组合框模板:

  public class ComboBoxTemplate : ITemplate
    {

        ListItemType _type;
        string _colName;
        Dictionary<string, string> _items;

        public ComboBoxTemplate(ListItemType type, string colname,Dictionary<string,string> items)
        {
            _type = type;
            _colName = colname;
            _items = items;
        }

        public void InstantiateIn(Control container)
        {
            try
            {
                switch (_type)
                {
                    case ListItemType.Item:

                        Label l = new Label();
                        l.DataBinding += l_DataBinding;
                        container.Controls.Add(l);

                        break;
                    case ListItemType.EditItem:

                        DropDownList ddl = new DropDownList();
                        foreach (var obj in _items)
                        {
                            ddl.Items.Add(new ListItem(obj.Value, obj.Key));
                        }
                        ddl.DataBinding += l_DataBinding;
                        container.Controls.Add(ddl);


                        break;
                }
            }
            catch (Exception ex)
            {

                return;
            }

        }

        void l_DataBinding(object sender, EventArgs e)
        {                 
            try
                    {
            GridViewRow container;
            object dataValue;
            switch (sender.GetType().ToString())
            {
                case "System.Web.UI.WebControls.Label":
                    Label label = (Label)sender;
                    container = (GridViewRow)label.NamingContainer;
                    dataValue = DataBinder.Eval(container.DataItem, _colName);
                    if (dataValue != DBNull.Value)
                    {
                        if (_items.ContainsKey(dataValue.ToString()))
                        {
                            label.Text = _items[dataValue.ToString()];
                        }
                        else
                        {
                            label.Text = dataValue.ToString();
                        }
                    }
                    break;
                //use the DatePickerControl.DatePicker type instead of ddl
                case "System.Web.UI.WebControls.DropDownList":




                    DropDownList ddl = (DropDownList)sender;
                    container = (GridViewRow)ddl.NamingContainer;

                    dataValue = DataBinder.Eval(container.DataItem, _colName);

                    if (dataValue != DBNull.Value)
                    {
                        ddl.SelectedValue = dataValue.ToString();

                    }
                    break;
                    }    


            }
            catch (Exception ex)
            {

                return;
            }
        }
    }

像这样创建组合框(On Data Binding):

                var fkdata = Model.GridProvider.GetForgienKeyData(x);
                templateColumn.EditItemTemplate = new ComboBoxTemplate(ListItemType.EditItem, column.ColumnName, fkdata);
                templateColumn.ItemTemplate = new ComboBoxTemplate(ListItemType.Item, column.ColumnName, fkdata);

0 个答案:

没有答案