Dropdownlist始终在Gridview中返回默认值

时间:2019-07-11 09:46:08

标签: c# sql asp.net gridview

我有一个GridView,它代表students表的所有信息。 students表与存储学生位置的表Institues有关系。为了编辑GridView,我插入了两个下拉列表ddlLocationddlInstitute,其中ddlInstituteddlInstitute选择的特定位置显示所有机构,并检索InstiuteID用作students表的外键。 基本上,两个dorpdownlists中的值都是从数据库中的表中获取的。该表如下所示:

 Inst_ID   Location      InstitueName
--------------------------------------
1001        Delhi            DIIT
1002        Delhi            JNU
1003        Mumbai           MIIT

检索位置和Institue ID的代码很好。当我进入编辑模式时,它会正常显示并相应地选择InstitueID;但是,当我尝试编辑一行并更改某些值时,ddlInstitute总是返回默认值(-1)。

我的GridView看起来像这样:

 
<asp:GridView ID="gvInternshipDetails" runat="server" AutoGenerateColumns="False" AllowPaging="True"
                        AllowSorting="true" Width="100%" CssClass="mydatagrid2" OnRowCancelingEdit="gvInternshipDetails_RowCancelingEdit" OnRowDataBound="gvInternshipDetails_RowDataBound"
                        OnRowEditing="gvInternshipDetails_RowEditing" OnRowUpdating="gvInternshipDetails_RowUpdating" OnRowCommand="gvInternshipDetails_RowCommand">

                        <Columns>

                            <asp:TemplateField HeaderText="Edit">
                                <ItemTemplate>
                                    <asp:Button ID="btn_StEdit" runat="server" CommandName="Edit" CssClass="mybtn" Text="edit" />
                                </ItemTemplate>
                                <EditItemTemplate>
                                    <asp:Button ID="btn_StUpdate" CausesValidation="true" ValidationGroup="stdGroup" runat="server" CommandName="Update" CssClass="mybtn" Text="update" />

                                    <asp:Button ID="btn_StCancel" runat="server" CommandName="Cancel" CssClass="mybtn" Text="cancel" />
                                </EditItemTemplate>

                            </asp:TemplateField>

                            <asp:TemplateField HeaderText="Location" SortExpression="Location">

                                <ItemTemplate>
                                    <asp:Label ID="lblLocation" runat="server" Text='<%# Bind("[Location]") %>'></asp:Label>
                                </ItemTemplate>
                                <EditItemTemplate>
                                    <asp:HiddenField ID="hdLocation" Value='<%# Bind("[Location]") %>' runat="server" />
                                    <asp:DropDownList runat="server" Width="200px" ID="ddlLocation" CssClass="form-control editingControls" AutoPostBack="true" OnSelectedIndexChanged="ddlLocation_SelectedIndexChanged"></asp:DropDownList>
                                </EditItemTemplate>
                                <ItemStyle HorizontalAlign="Center" Wrap="false" />
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Institue" SortExpression="Institue">

                                <ItemTemplate>
                                    <asp:Label ID="lblInstitue" runat="server" Text='<%# Bind("[Institue]") %>'></asp:Label>
                                </ItemTemplate>
                                <EditItemTemplate>
                                    <asp:HiddenField ID="hdInstitue" Value='<%# Bind("[Institue ID]") %>' runat="server" />
                                    <asp:DropDownList runat="server" Width="200px" ID="ddlInstitue" CssClass="form-control editingControls"></asp:DropDownList>
                                    <asp:RequiredFieldValidator Display="Dynamic" ID="RequiredFieldValidator8" runat="server" InitialValue="-1" ForeColor="#e31937" ControlToValidate="ddlInstitue" ErrorMessage="Please select one"></asp:RequiredFieldValidator>
                                </EditItemTemplate>
                                <ItemStyle HorizontalAlign="Center" Wrap="false" />
                            </asp:TemplateField>

                        </Columns>
                        <EmptyDataTemplate>No Record Available</EmptyDataTemplate>
                        <HeaderStyle CssClass="header" />
                        <PagerSettings FirstPageText="First" LastPageText="Last" PageButtonCount="9" Mode="NumericFirstLast" />
                        <PagerStyle CssClass="pager" />
                    </asp:GridView>

以下是我的RowDatabound的一部分:

 
 protected void gvInternshipDetails_RowDataBound(object sender, GridViewRowEventArgs e)
            {
                if (e.Row.RowType == DataControlRowType.DataRow && gvInternshipDetails.EditIndex == e.Row.RowIndex)
                {

                    try
                    {
                        String University = (e.Row.FindControl("hdLocation") as HiddenField).Value;
                        DropDownList ddlLocation = (DropDownList)e.Row.FindControl("ddlLocation");
                        mc.fillSearchDropdown(ddlLocation);
                        ddlLocation.SelectedValue = University;

                        String Institue = (e.Row.FindControl("hdInstitue") as HiddenField).Value;
                        DropDownList ddlInstitue = (DropDownList)e.Row.FindControl("ddlInstitue");
                        mc.fillDDLInstitue(ddlInstitue, University);
                        ddlInstitue.SelectedValue = Institue;
                    }
                    catch (Exception m)
                    {
                        string script = " alert('" + m.Message + " ');";
                        ScriptManager.RegisterStartupScript(this, GetType(),
                                                          "ServerControlScript", script, true);
                    }

                }      
            }

这是我的selectedIndexChanged:

 protected void ddlLocation_SelectedIndexChanged(object sender, EventArgs e)
            {
                DropDownList dr = (DropDownList)sender;
                GridViewRow row = (GridViewRow)dr.NamingContainer;
                DropDownList ddlInstitue =(DropDownList) row.FindControl("ddlInstitue");
                HiddenField hdLocation = (HiddenField)row.FindControl("hdLocation");
                HiddenField hdInstitue = (HiddenField)row.FindControl("hdInstitue");
                mc.fillDDLInstitue((DropDownList)row.FindControl("ddlInstitue"), dr.SelectedValue);

    //I guess the problem is here, the selected value doesn't get updated.
                //if(dr.SelectedValue == hdLocation.Value)
                //{

                //    ddlInstitue.SelectedValue = hdInstitue.Value;
                //}
            }

最后是RowUpdating方法:

Protected void gvInternshipDetails_RowUpdating(object sender, GridViewUpdateEventArgs e)
                {

                    GridViewRow row = gvInternshipDetails.Rows[e.RowIndex];

                    HiddenField hdInstitue = (HiddenField)row.FindControl("hdInstitue");
                    DropDownList ddlInstitue = (DropDownList)row.FindControl("ddlInstitue");

                    if (Page.IsValid)
                    {
                        try
                        {

                            SqlCommand com;

                            SqlConnection cs = new SqlConnection(ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString);

                            using (cs)
                            {

                                com = new SqlCommand("sp_editInternshipDetails", cs);
                                com.CommandType = CommandType.StoredProcedure;

                                com.Parameters.AddWithValue("@university", Convert.ToInt32(ddlInstitue.SelectedValue));

                                cs.Open();
                                var ReturnParameter = com.Parameters.Add("@compeleted", SqlDbType.Int);
                                ReturnParameter.Direction = ParameterDirection.Output;
                                com.ExecuteNonQuery();                        

                            }

                        }
                        catch (Exception m)
                        {

                            string script = " $.MessageBox(\"" + m.Message + " \");";
                            ScriptManager.RegisterStartupScript(this, GetType(),
                                                              "ServerControlScript", script, true);
                        }

                    }
                }

我希望通过更改ddlInstitue的值来更改其selectedValue。非常感谢您的帮助。

0 个答案:

没有答案