gridview 下拉DropDownList 更新操作

前台代码

后台代码public partial class GridView_DropDownList : System.Web.UI.Page
{

    private DataAccess da = SingleTon<DataAccess>.Instance;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.Page.IsPostBack)
        {
            BindGridView();

        }
    }


    private void BindGridView()
    {
        DataTable dt = da.GetTable("select name,address,age,country_id,country_name ,id from userinfo  ,country where country_id=country  ");
        grid.DataSource = dt;
        grid.DataBind();
    }


    /// <summary>
    /// command命令操作gridview
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void grid_RowCommand(object sender, GridViewCommandEventArgs e)
    {

       
        if (e.CommandName == "test")
        {
          string id=  e.CommandArgument.ToString();

          Response.Write(" <script> javascript:alert( '" + id + "'); </script> ");
          Response.Flush();
        }
    }

    private void BindDropDownListCountry(DropDownList drp)
    {
        DataTable dt = da.GetTable("select country_id,country_name  from country");
        drp.DataSource = dt;
        drp.DataValueField="country_id";
        drp.DataTextField = "country_name";
        drp.DataBind();
    }
    protected void grid_RowEditing(object sender, GridViewEditEventArgs e)
    {
        grid.EditIndex = e.NewEditIndex;
        BindGridView();
    }
    protected void grid_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        grid.EditIndex = -1;
        BindGridView();
    }
    protected void grid_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow && ((e.Row.RowState & DataControlRowState.Edit) > 0))
        {
            DropDownList drp = e.Row.FindControl("dd_country") as DropDownList;
            BindDropDownListCountry(drp);
            drp.SelectedValue = (e.Row.FindControl("hdd_countryid") as HiddenField).Value ;
        }
    }


    /// <summary>
    /// 更新
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void grid_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {

        DropDownList drp = grid.Rows[e.RowIndex].FindControl("dd_country") as DropDownList;
        string country_ID=drp.SelectedValue;
        string id = grid.DataKeys[e.RowIndex].Value.ToString();
        string msg = string.Empty;
        if (da.ExcuteSql("update userinfo set country='"+country_ID+"' where id='" + id + "'", out msg))
        {
            grid.EditIndex = -1;
            BindGridView();
        }
       


    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值