如何绑定Gridview下拉列表以及如何在asp.net中获取Dropdown选择值

时间:2011-06-23 05:25:13

标签: asp.net gridview c#-4.0

我开发了一个asp.net webapplication,我有gridview控件,当我加载表格gridview hava填充从sqlserver数据库获取的数据。在gridview我有下拉当我点击编辑我有的表格从下拉列表中选择值并保存到数据库但是下拉值始终取第一个索引值没有取选择值请帮助我....我怎么能解决这个问题我已经在这里发布了我的代码我做了什么。

    protected void grd_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
            TextBox txt = null;
            TextBox clientpmtdate = null;
            TextBox amt = null;
            TextBox PaymentID = null;
       try
            {
                clientpmtdate = ((TextBox)(grdviewPayments.Rows[e.RowIndex].FindControl("txtEditpmtdate")));
                int ddldmthd = Convert.ToInt32(((DropDownList)(grdviewPayments.Rows[e.RowIndex].FindControl("ddldebitmethodedit"))).SelectedValue);
                int ddlpmtype = Convert.ToInt32(((DropDownList)(grdviewPayments.Rows[e.RowIndex].FindControl("ddlpmttypeedit"))).SelectedValue);
                txt = ((TextBox)(grdviewPayments.Rows[e.RowIndex].FindControl("txtEditmtrsvrdate")));
                amt = ((TextBox)(grdviewPayments.Rows[e.RowIndex].FindControl("txtEditammount")));
                PaymentID = ((TextBox)(grdviewPayments.Rows[e.RowIndex].FindControl("txtEditPaymentID")));

                string clienteditpmtdate = clientpmtdate.Text;
                // string debitmethod = ddldmthd.SelectedItem.Value;
                //string pmttype = ddlpmtype.SelectedItem.Value;
                string txtEditmtrsvrdate = txt.Text;
                string txtEditammount = amt.Text;
                string txtEditPaymentID = PaymentID.Text;
                int pmtid = Convert.ToInt32(txtEditPaymentID);
                string pmtcmnts = txtpmtcmnts.Text;
                if (txtEditPaymentID != null)
                {
                    var Editpmt = (from k in mortgageentity.Payments where k.Pmt_ID == pmtid select k).First();
                    Editpmt.Client_Pmt_Date = Convert.ToDateTime(clienteditpmtdate);
                    Editpmt.MtgSvr_Pmt_Start_Date2 = txtEditmtrsvrdate;
                    Editpmt.Amt = Convert.ToDecimal(txtEditammount);
                    Editpmt.Pmt_Comments = pmtcmnts;
                    Editpmt.Payment_Type_ID = ddlpmtype;
                    Editpmt.Debit_Method_ID = ddldmthd;
                    mortgageentity.SaveChanges();
                }
                grdviewPayments.EditIndex = -1;
                //  bindGrid(e.RowIndex + 1, txt.Text);
                //bindGrid(0, null);
                BindData();
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
            finally
            {
                if (txt != null) txt = null;
            }

    }

2 个答案:

答案 0 :(得分:0)

在RowEditing中设置下拉列表的索引:

protected void grdviewPayments_RowEditing(object sender, GridViewEditEventArgs e)
{
    grdviewPayments.EditIndex = e.NewEditIndex;

    GridViewRow editingRow = grdviewPayments.Rows[e.NewEditIndex];

    DropDownList ddl = (editingRow.FindControl("ddlS") as DropDownList);
    if (ddl != null)
    {
        //set index
    }


}

答案 1 :(得分:0)

你启用了DropdownList的Postback属性吗? 如果否,则设置Postback = True。 你编码是对的。