gridview 下拉DropDownList 更新操作

来源:互联网 发布:nginx 不能通过ip访问 编辑:程序博客网 时间:2024/05/16 17:01

前台代码

后台代码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();
        }
       


    }

原创粉丝点击