Gridview中的DropDownList在回发时丢失了值

时间:2018-01-27 08:10:47

标签: c# asp.net datagridview

我在gridview中有一个下拉列表,当我更改下拉列表中的值时,它会显示初始值。第二次更改值时,该值仍然存在。请帮我弄清楚我做错了什么。在发布之前我已经通过了很多解决方案。

我没有使用更新面板。我在这里仅提供相关代码。

<asp:GridView  runat="server"  ID="GridView2" Width="100%" AutoGenerateColumns="False" ShowFooter="True"
               DataKeyNames="ID"
               OnPageIndexChanging="GridView2_PageIndexChanging"
               OnRowDeleting="GridView2_RowDeleting"
               OnRowEditing="GridView2_RowEditing"
               OnRowUpdating="GridView2_RowUpdating"
               OnRowCancelingEdit="GridView2_RowCancelingEdit"
               OnRowCommand="GridView2_RowCommand" 
               HeaderStyle-CssClass="GVFixedHeader" 
               FooterStyle-CssClass="GVFixedFooter"
               OnRowDataBound="GridView2_ddl" 
               EnableViewState="true" >
<Columns>
    <asp:TemplateField HeaderText="MonitorDetailsID" SortExpression="MonitorDetailsID">
        <EditItemTemplate>
            <asp:DropDownList ID="txtMonitorDetailsID1" 
                              runat="server" 
                              DataValueField = "ID" 
                              DataTextField = "name" 
                              EnableViewState="true"
                              AppendDataBoundItems="true"
                              AutoPostBack="true"
                              OnSelectedIndexChanged="ddlMonitorDetailsID1_SelectedIndexChanged"
                              CausesValidation="false" >
            </asp:DropDownList>
        </EditItemTemplate>
        <FooterTemplate>
            <asp:TextBox ID="txtMonitorDetailsID"  
                         width="90%" Rows="3" 
                         TextMode="MultiLine" 
                         Wrap="true" 
                         runat="server" 
                         EnableViewState="true" >            
            </asp:TextBox>
        </FooterTemplate>
        <ItemTemplate>
            <asp:Label ID="Label8" 
                       runat="server" 
                       Text='<%# Bind("MonitorDetailsID") %>' 
                       EnableViewState="true">
            </asp:Label>
        </ItemTemplate>
    </asp:TemplateField>
    </Columns>
</asp:GridView>

我的aspx.cs代码

public partial class Maintenance : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        IntranetCore masterpage = (IntranetCore)(Page.Master);

        masterpage.PageTitle = "Maintain Database Tables";
        masterpage.ShowPageLastUpdated = false;
        Master.Page.Title = "test";
        this.Title = "Maintain Database Tables";

        if (!IsPostBack)
        {
            using (DataTable dt = new DataTable())
            {
                GridView2.DataSource = dt;
                GridView2.DataBind();
            }
        }
    }
    protected void ddlMonitorDetailsID1_SelectedIndexChanged(object sender, EventArgs e)
    {

    }

    protected void GridView2_ddl(object sender, GridViewRowEventArgs e)
    {
        if ((e.Row.RowState & DataControlRowState.Edit) > 0)
        {
            string constr = ConfigurationManager.ConnectionStrings["DBConnectionString"].ConnectionString;
            using (SqlConnection con = new SqlConnection(constr))
            {
                using (SqlCommand cmd = new SqlCommand(@"select top 100 ID, name from db_monitordetails;"))
                {
                    using (SqlDataAdapter sda = new SqlDataAdapter())
                    {
                        cmd.Connection = con;
                        sda.SelectCommand = cmd;
                        using (DataTable dt = new DataTable())
                        {
                            sda.Fill(dt);
                            DropDownList DropDownList1 = (e.Row.FindControl("txtMonitorDetailsID1") as DropDownList);
                            DropDownList1.DataSource = dt;
                            DropDownList1.DataBind();

                            DropDownList1.Items.Insert(0, new ListItem("--Select Monitor details--", "0"));
                            //DropDownList1.Items.FindByValue((e.Row.FindControl("txtMonitorDetailsID1") as DropDownList).Text).Selected = true;

                            DropDownList1.SelectedValue = (string)DataBinder.Eval(e.Row.DataItem, "ID").ToString();
                            //if (DropDownList1 != null)
                            //{
                            //    DropDownList1.SelectedIndexChanged += new EventHandler(ddlMonitorDetailsID1_SelectedIndexChanged);
                            //}
                            //DropDownList1.DataValueField = "ID";
                            //DropDownList1.DataTextField = "name";
                            //DropDownList1.SelectedValue = "1";
                            string value = DropDownList1.SelectedValue;
                        }
                    }
                }
            }
        }
    }
}

0 个答案:

没有答案