DatailView,如果使用BoundField,将自动将该列和数据源字段进行绑定.现在使用模板列,模板列中为一用户控件,如何将用户控件的某个属性和数据源字段也进行绑定呢?这样在编辑删除的时候,系统将自动用用户控件的值更新数据源中对应字段.不知道ASP.NET有没有提供这样的机制,或者还需要写代码来解决?如果需要写代码,该如何写

解决方案 »

  1.   

    protected void DetailsView1_ItemInserting(object sender, DetailsViewInsertEventArgs e)
        {
            // Get the state value from the DropDownList control in the 
            // DetailsView control.
           
                String type = GetDptType();
                e.Values["dpt_type"] = type;   //将部门类型赋给插入参数
                String name = GetDptName();
               
                e.Values["dpt_name"] = name;
     String GetDptType()  //从DropDownList2获取部门类型
        {
            String type;       
            DropDownList list = (DropDownList)DetailsView1.Rows[2].FindControl("DropDownList2");        if (list != null)
            {
                // Get the selected value of the DropDownList control.
                type = list.SelectedItem.Value;
            }
            else
            {
                // Set the state to an empty string ("").
                type = "";
            }        return type;
        }
    这样可以获取模板列的值
    绑定的话用<%# DataBinder.Eval(Container.DataItem,("字段名"))%>
      

  2.   

    public partial class WebUserControl_A : System.Web.UI.UserControl
    {
         public string TestTest
        {
            get { return "ABC"; }
            set { Response.Write(value); }    }
    }<asp:TemplateField>
                        <ItemTemplate>
                            <uc1:WebUserControl_A ID="WebUserControl_A1" runat="server" TestTest='<%# DataBinder.Eval(Container.DataItem,("字段名"))%>'/>
                        </ItemTemplate>
                    </asp:TemplateField>
      

  3.   

    模板列 = BoundField??!