当前位置:编程学习 > C#/ASP.NET >>

在datalist的Edit模板中如何动态绑定dropdownlist的数据?

在datalist的Edit模板中如何动态绑定dropdownlist的数据?    就是在datalist里的edit模板中有一字段是dropdownlist类型的,我怎么在.cs文件里写代码 实现 动态的把数据库里的数据绑定到dropdownlist中?? 并在后台 获取 dropdownlist 绑定数据后的 值????   请各位高手帮帮忙啊! --------------------编程问答-------------------- 转换成模板列。。
this.DropDownList1.DataSource = mtyManager.GetMerchandiseTypeParent();
this.DropDownList1.DataBind();

mtyp.ParentID = Convert.ToInt32(this.DropDownList2.Text); --------------------编程问答-------------------- 跟这个一样,会了,你的也不难了:
http://www.cnblogs.com/insus/articles/1411016.html --------------------编程问答-------------------- 看不懂1楼的解答,能说得具体点不? --------------------编程问答--------------------
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class GridviewHtmlCheckBox : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            bind();
        }
    }
    /// <summary>
    /// 绑定数据
    /// </summary>
    public void bind()
    {
        string sqlStr = "select * from Employee";
        DataSet myds = Common.dataSet(sqlStr);
        GridView1.DataSource = myds;
        GridView1.DataKeyNames = new string[] { "ID" };
        GridView1.DataBind();
    }
    /// <summary>
    /// 在 GridView 控件中的某个行被绑定到一个数据记录时发生。此事件通常用于在某个行被绑定到数据时修改该行的内容。
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        //行绑定DropDownList
        if (((DropDownList)e.Row.FindControl("DDLSex")) != null)
        {
            DropDownList ddlSex = (DropDownList)e.Row.FindControl("DDLSex");
            ddlSex.Items.Clear();
            ddlSex.Items.Add(new ListItem("男", "男"));
            ddlSex.Items.Add(new ListItem("女", "女"));
            //ddlSex.Items.Add(new ListItem("男"));
            //ddlSex.Items.Add(new ListItem("女"));
            //DropDownList初始被选择的项
            ddlSex.SelectedValue = ((HiddenField)e.Row.FindControl("HDFSex")).Value;
        }
        if (((DropDownList)e.Row.FindControl("DDLAddress")) != null)
        {
            DropDownList ddlAddress = (DropDownList)e.Row.FindControl("DDLAddress");
            DataSet ds = Common.dataSet("select Address from Address");
            ddlAddress.DataSource = ds.Tables[0].DefaultView;
            ddlAddress.DataTextField = "Address";
            ddlAddress.DataValueField = "Address";
            ddlAddress.DataBind();
            //当更改时,绑定第一个为它所属的分类
            ddlAddress.SelectedValue = ((HiddenField)e.Row.FindControl("HDFAddress")).Value;
        }
        foreach (TableCell tc in e.Row.Cells)
        {
            tc.Attributes["style"] = "border-color:Black";
        }
    }
    /// <summary>
    /// 在单击 GridView 控件内某一行的 Update 按钮(其 CommandName 属性设置为"Update"的按钮)时发生,但在 GridView 控件更新记录之前。此事件通常用于取消更新操作。
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        string ID = GridView1.DataKeys[e.RowIndex].Value.ToString();
        string Emp_ID = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString().Trim();
        string Emp_RealName = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString().Trim();
        string Emp_Sex = ((DropDownList)GridView1.Rows[e.RowIndex].FindControl("DDLSex")).SelectedValue;
        string Emp_Address = ((DropDownList)GridView1.Rows[e.RowIndex].FindControl("DDLAddress")).SelectedValue;
        string sqlStr = "update Employee set EmpID='" + Emp_ID + "',EmpRealName='" + Emp_RealName + "',EmpSex='" + Emp_Sex + "',EmpAddress='" + Emp_Address + "' where ID=" + ID + "";
        Common.ExecuteSql(sqlStr);
        GridView1.EditIndex = -1;
        bind();
    }
    /// <summary>
    /// 在单击 GridView 控件内某一行的 Edit 按钮(其 CommandName 属性设置为“Edit”的按钮)时发生,但在 GridView 控件进入编辑模式之前。此事件通常用于取消编辑操作。
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        bind();
    }
    /// <summary>
    /// 在单击 GridView 控件内某一行的 Cancel 按钮(其 CommandName 属性设置为“Cancel”的按钮)时发生,但在 GridView 控件退出编辑模式之前。此事件通常用于停止取消操作。
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        bind();
    }
}
--------------------编程问答-------------------- 楼上的头像不错! --------------------编程问答-------------------- 如果不想看下拉联动的,还可以参考这个:
http://www.cnblogs.com/insus/articles/1654911.html --------------------编程问答-------------------- 谢谢二楼的老师,没有在datalist中的例子吗?
补充:.NET技术 ,  ASP.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,