始终选择我的下拉列表的第一个值 [英] Always select the first value of my dropdownlist

查看:72
本文介绍了始终选择我的下拉列表的第一个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



i有下拉列表来自(模型实体)这个下拉列表我有站

当我选择同一个它在apear在gridveiw列表中这个站点的参数

在我的网格页面的页脚中我有一个单独的面板在这个面板中添加相同的参数我有另一个下拉列表它绑定了参数名称它绑定它(模型实体) )但是当我想要选择最后一个的值时我总是有第一个

为绑定功能的名称这个下拉列表是gv_pm_DataBound

这是我的code pllz我需要帮助








i have dropdownlist wich bind it from (model entity) in this dropdown i have "Station"
when i chose same one it apear in gridveiw list of "Parametre of this station"
in footer of my grid veiw i have panel it apear to add same parametre in this panel i have another dropdownlist wich it bind of name of parametre it bind it from (model entity) but when i want to select value of this last one i always have the first one
for the name of function of binding this dropdown is "gv_pm_DataBound"
this is my code pllz i need help



<asp:DropDownList ID="drp_state" runat="server" AutoPostBack="True" OnSelectedIndexChanged="drp_state_SelectedIndexChanged"></asp:DropDownList>

 <asp:GridView ID="gv_pm"

                       ForeColor="#333333" AutoGenerateColumns="false" ShowFooter="True" runat="server" DataKeyNames="ID_PM"

                       OnRowEditing="gv_pm_RowEditing"

                       OnRowCancelingEdit="gv_pm_RowCancelingEdit"

                       OnRowUpdating="gv_pm_RowUpdating"

                       OnRowCommand="gv_pm_RowCommand"

                       OnRowDataBound="gv_pm_RowDataBound"

                       OnDataBound="gv_pm_DataBound">
                       <Columns>
                           <asp:TemplateField>
                               <ItemTemplate>
                                   <asp:LinkButton ID="LinkButton2" runat="server" Text="" CommandName="Select" ToolTip="Modifier"

                                        CommandArgument='<%# DataBinder.Eval(Container, "RowIndex") %>'

                          >Seuil d'alerte</asp:LinkButton>
                                   <asp:LinkButton ID="lnkEdit" runat="server" Text="" CommandName="Edit" ToolTip="Modifier"

                                       CommandArgument=''><img src="../Images/show.png" /></asp:LinkButton>
                                   <asp:LinkButton ID="lnkCancel" Visible="false" runat="server" Text="" CommandName="Cancel" ToolTip="Annuler"

                                       CommandArgument=''><img src="../Images/cancel.png" /></asp:LinkButton>
                                   <asp:LinkButton ID="lnkInsert" Visible="false" runat="server" Text="" ValidationGroup="editGrp" CommandName="Update" ToolTip="Sauvegarder"

                                       CommandArgument=''><img src="../Images/icon_save.png" /></asp:LinkButton>
                               </ItemTemplate>
                               <FooterTemplate>
                                   <img alt="" style="cursor: pointer" src="../Images/plus.png" />
                                   <asp:Panel ID="pnlOrderss" runat="server" Style="display: none">
                                       <table style="width: 100%;">
                                           <tr>
                                               <td>
                                                   <asp:Label ID="Label3" runat="server" Text="Paramètre"></asp:Label></td>
                                               <td>

                                                   <asp:DropDownList ID="drp_pmm" runat="server" ></asp:DropDownList></td>
                                           </tr>

                                           <tr>
                                               <td>
                                                   <asp:Label ID="Unit" runat="server" Text="Unité de mesure"></asp:Label></td>
                                               <td>
                                                   <asp:TextBox ID="txt_um" runat="server"></asp:TextBox>
                                                       </td>
                                           </tr>
                                           <tr>
                                               <td>
                                                   <asp:Label ID="Typegraphe" runat="server" Text="Type graphe"></asp:Label></td>
                                               <td>
                                                   <asp:TextBox ID="txt_typeg" runat="server"></asp:TextBox>
                                                       </td>
                                           </tr>
                                           <tr>
                                               <td>
                                                   <asp:Label ID="Label2" runat="server" Text="Interval min"></asp:Label></td>
                                               <td>
                                                   <asp:TextBox ID="txt_min" runat="server"></asp:TextBox>
                                                       </td>
                                           </tr>
                                           <tr>
                                               <td>
                                                   <asp:Label ID="Intervalmax" runat="server" Text="Interval max"></asp:Label></td>
                                               <td>
                                                   <asp:TextBox ID="txt_max" runat="server"></asp:TextBox>
                                                       </td>
                                           </tr>
                                           <tr>
                                               <td>
                                                   <asp:Label ID="Label1" runat="server" Text="Tolerence"></asp:Label></td>
                                               <td>
                                                   <asp:TextBox ID="txt_tol" runat="server"></asp:TextBox>
                                                    </td>
                                           </tr>
                                           <tr>
                                               <asp:LinkButton ID="lnkInsertt" ValidationGroup="newGrp" runat="server" Text="" CommandName="InsertNew" ToolTip="Ajouter nouveau paramètre de mesure"

                                                   CommandArgument=''><img src="../Images/icon_save.png" /></asp:LinkButton>
                                               <asp:LinkButton ID="LinkButton1" runat="server" Text="" CommandName="CancelNew" ToolTip="Annuler"

                                                   CommandArgument=''><img src="../Images/cancel.png" /></asp:LinkButton>
                                           </tr>
                                       </table>
                                   </asp:Panel>
                               </FooterTemplate>

                           </asp:TemplateField>
                           <asp:TemplateField HeaderText="Paramètre de mesure" HeaderStyle-HorizontalAlign="Left" HeaderStyle-Font-Size="13px">
                               <ItemTemplate>
                                   <asp:Label ID="ID_PM" runat="server" Text='<%# Bind("ID_PM") %>'></asp:Label>
                               </ItemTemplate>
                               <EditItemTemplate>
                                   <asp:TextBox ID="ID_PM" runat="server" Text='<%# Bind("ID_PM") %>' Style="width: 119px; font-size: 12px;" MaxLength="30"></asp:TextBox>
                               </EditItemTemplate>
                           </asp:TemplateField>
                           <asp:TemplateField HeaderText="Unité de mesure" HeaderStyle-HorizontalAlign="Left" HeaderStyle-Font-Size="13px">
                               <EditItemTemplate>
                                   <asp:TextBox ID="UNITE_MESURE" runat="server" Text='<%# Bind("UNITE_MESURE") %>' Style="width: 122px; font-size: 12px;" MaxLength="30"></asp:TextBox>
                               </EditItemTemplate>
                               <ItemTemplate>
                                   <asp:Label ID="UNITE_MESURE" runat="server" Text='<%# Bind("UNITE_MESURE") %>'></asp:Label>
                               </ItemTemplate>
                           </asp:TemplateField>
                           <asp:TemplateField HeaderText="Tolerence" HeaderStyle-HorizontalAlign="Left" HeaderStyle-Font-Size="13px">
                               <EditItemTemplate>
                                   <asp:TextBox ID="TOLERENCE" runat="server" Text='<%# Bind("TOLERENCE") %>' Style="width: 122px; font-size: 12px;" MaxLength="30"></asp:TextBox>
                               </EditItemTemplate>
                               <ItemTemplate>
                                   <asp:Label ID="TOLERENCE" runat="server" Text='<%# Bind("TOLERENCE") %>'></asp:Label>
                               </ItemTemplate>
                           </asp:TemplateField>
                           <asp:TemplateField HeaderText="Intervale min" HeaderStyle-HorizontalAlign="Left" HeaderStyle-Font-Size="13px">
                               <EditItemTemplate>
                                   <asp:TextBox ID="INTERVAL_MIN" runat="server" Text='<%# Bind("INTERVAL_MIN") %>' Style="width: 119px; font-size: 12px;" MaxLength="30"></asp:TextBox>
                               </EditItemTemplate>
                               <ItemTemplate>
                                   <asp:Label ID="INTERVAL_MIN" runat="server" Text='<%# Bind("INTERVAL_MIN") %>'></asp:Label>
                               </ItemTemplate>
                           </asp:TemplateField>
                           <asp:TemplateField HeaderText="Intervale max" HeaderStyle-HorizontalAlign="Left" HeaderStyle-Font-Size="13px">
                               <EditItemTemplate>
                                   <asp:TextBox ID="INTERVAL_MAX" runat="server" Text='<%# Bind("INTERVAL_MAX") %>' Style="width: 80px; font-size: 12px;" MaxLength="30"></asp:TextBox>
                               </EditItemTemplate>
                               <ItemTemplate>
                                   <asp:Label ID="INTERVAL_MAX" runat="server" Text='<%# Bind("INTERVAL_MAX") %>'></asp:Label>
                               </ItemTemplate>
                           </asp:TemplateField>
                           <asp:TemplateField HeaderText="Type du graphe" HeaderStyle-HorizontalAlign="Left" HeaderStyle-Font-Size="13px">
                               <EditItemTemplate>
                                   <asp:TextBox ID="TYPE_GRAPHE" runat="server" Text='<%# Bind("TYPE_GRAPHE") %>' Style="width: 80px; font-size: 12px;" MaxLength="30"></asp:TextBox>
                               </EditItemTemplate>
                               <ItemTemplate>
                                   <asp:Label ID="TYPE_GRAPHE" runat="server" Text='<%# Bind("TYPE_GRAPHE") %>'></asp:Label>
                               </ItemTemplate>
                           </asp:TemplateField>
                       </Columns>
                   </asp:GridView>







and








and


protected void Page_Load(object sender, EventArgs e)
     {
         if (!Page.IsPostBack)
         {
             binddrp();

         }
     }
     void binddrp()
     {
         using (QTEL_Entities qtl = new QTEL_Entities())
         {
             var state = from st in qtl.STATIONs
                         orderby st.ID_STATION
                         select new
                         {
                             id = st.ID_STATION,
                             name = st.LIBELE_STATION
                         };

             drp_state.DataSource = state.ToList();
             drp_state.DataTextField = "name";
             drp_state.DataValueField = "id";

             drp_state.DataBind();

             drp_state.Items.Insert(0, new ListItem("--Station--", ""));
         }
     }

     protected void drp_state_SelectedIndexChanged(object sender, EventArgs e)
     {
         using (QTEL_Entities qtl = new QTEL_Entities())
         {
             var rslt = from tab in qtl.STATION_PM
                        where tab.ID_STATION == drp_state.SelectedValue
                        select tab;

             gv_pm.DataSource = rslt.ToList();
             gv_pm.DataBind();
         }
     }

     void BindGrid()
     {
         using (QTEL_Entities qtl = new QTEL_Entities())
         {
             var rslt = from tab in qtl.STATION_PM
                        where tab.ID_STATION == drp_state.SelectedValue
                        select tab;

             gv_pm.DataSource = rslt.ToList();
             gv_pm.DataBind();
         }
     }

     protected void gv_pm_RowEditing(object sender, GridViewEditEventArgs e)
     {
         gv_pm.EditIndex = e.NewEditIndex;
         BindGrid();

         LinkButton likcancel = gv_pm.Rows[gv_pm.EditIndex].FindControl("lnkCancel") as LinkButton;
         likcancel.Visible = true;

         LinkButton lnkInsert = gv_pm.Rows[gv_pm.EditIndex].FindControl("lnkInsert") as LinkButton;
         lnkInsert.Visible = true;

         LinkButton linkedit = gv_pm.Rows[gv_pm.EditIndex].FindControl("lnkEdit") as LinkButton;
         linkedit.Visible = false;

     }



     protected void gv_pm_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
     {
         gv_pm.EditIndex = -1;
         BindGrid();
     }

     protected void gv_pm_RowUpdating(object sender, GridViewUpdateEventArgs e)
     {
         //instruction de modification
         GridViewRow row = gv_pm.Rows[e.RowIndex];
         TextBox ID_PM = row.FindControl("ID_PM") as TextBox;
         TextBox UNITE_MESURE = row.FindControl("UNITE_MESURE") as TextBox;
         TextBox TOLERENCE = row.FindControl("TOLERENCE") as TextBox;
         TextBox INTERVAL_MIN = row.FindControl("INTERVAL_MIN") as TextBox;
         TextBox INTERVAL_MAX = row.FindControl("INTERVAL_MAX") as TextBox;
         TextBox TYPE_GRAPHE = row.FindControl("TYPE_GRAPHE") as TextBox;

         if (ID_PM != null)
         {
             using (QTEL_Entities qtl = new QTEL_Entities())
             {
                 string Id_PM = Convert.ToString(gv_pm.DataKeys[e.RowIndex].Value);
                 Web_QTel_V01.STATION_PM st = qtl.STATION_PM.First(x => x.ID_PM == Id_PM && x.ID_STATION== drp_state.SelectedValue);
                 st.ID_PM = ID_PM.Text;
                 st.UNITE_MESURE = UNITE_MESURE.Text;
                 st.TOLERENCE = int.Parse(TOLERENCE.Text);
                 st.INTERVAL_MIN = int.Parse(INTERVAL_MIN.Text);
                 st.INTERVAL_MAX = int.Parse(INTERVAL_MAX.Text);
                 st.TYPE_GRAPHE = TYPE_GRAPHE.Text;
                 qtl.SaveChanges();
                 gv_pm.EditIndex = -1;
                 BindGrid();
             }
         }
     }

     protected void gv_pm_RowCommand(object sender, GridViewCommandEventArgs e)
     {
         if (e.CommandName == "InsertNew")
         {
             GridViewRow row = gv_pm.FooterRow;

             //DropDownList drp_pmm = gv_pm.FooterRow.FindControl("drp_pmm") as DropDownList;

             //TextBox txt_pmm = row.FindControl("txt_pmm") as TextBox;
             DropDownList drp_pmm = row.FindControl("drp_pmm") as DropDownList;
             TextBox txt_um = row.FindControl("txt_um") as TextBox;
             TextBox txt_typeg = row.FindControl("txt_typeg") as TextBox;
             TextBox txt_min = row.FindControl("txt_min") as TextBox;
             TextBox txt_max = row.FindControl("txt_max") as TextBox;
             TextBox txt_tol = row.FindControl("txt_tol") as TextBox;
             if (txt_typeg != null)
             {
                 using (QTEL_Entities context = new QTEL_Entities())
                 {
                     Web_QTel_V01.STATION_PM obj = new Web_QTel_V01.STATION_PM();
                     obj.ID_STATION = drp_state.SelectedValue;
                     obj.UNITE_MESURE = txt_um.Text.Remove(0, 1);
                     obj.ID_PM = drp_pmm.SelectedValue;
                     obj.TYPE_GRAPHE = txt_typeg.Text.Remove(0,1);
                     obj.INTERVAL_MIN = int.Parse(txt_min.Text.Remove(0, 1));
                     obj.INTERVAL_MAX = int.Parse(txt_max.Text.Remove(0, 1));
                     obj.TOLERENCE = int.Parse(txt_tol.Text.Remove(0, 1));
                     context.STATION_PM.Add(obj);
                     context.SaveChanges();
                     BindGrid();
                 }
             }
         }
         if (e.CommandName == "CancelNew")
         {
             gv_pm.EditIndex = -1;
             BindGrid();
         }

         if (e.CommandName=="Select")
         {
             int rowIndex = int.Parse(e.CommandArgument.ToString());
             string val = (string)this.gv_pm.DataKeys[rowIndex]["ID_PM"];

             using (QTEL_Entities context = new QTEL_Entities())
             {
                 var alar = from sa in context.ALARME_STATION
                            join na in context.NIVEAU_ALERTE on sa.ID_NA equals na.ID_NA
                            join pm in context.PARAMETRE_MESURE on sa.ID_PM equals pm.ID_PM
                            join s in context.STATIONs on sa.ID_STATION equals s.ID_STATION
                            where pm.LIBELE_PM == val && s.ID_STATION == drp_state.SelectedValue
                            select new
                            {

                                libel = pm.LIBELE_PM,
                                na = na.LIBELE_NA,
                                seul = sa.SEUIL_ALERTE
                            };

                 gv_alerte.DataSource = alar.ToList();
                 gv_alerte.DataBind();
             }
         }

     }



     protected void gv_pm_DataBound(object sender, EventArgs e)
     {

             DropDownList drp_pmm = gv_pm.FooterRow.FindControl("drp_pmm") as DropDownList;

             using (QTEL_Entities qtl = new QTEL_Entities())
             {
                 var pm = from p in qtl.PARAMETRE_MESURE
                          orderby p.ID_PM
                          select new
                          {
                              id = p.ID_PM,
                              name = p.LIBELE_PM
                          };

                 drp_pmm.DataSource = pm.ToList();
                 drp_pmm.DataTextField = "name";
                 drp_pmm.DataValueField = "id";
                 drp_pmm.DataBind();
                 drp_pmm.Items.Insert(0, new ListItem("--Parametre de mesure--", ""));

             }

     }

推荐答案

Hi.



The problem is that gv_pm_DataBound event is fired each time when you refresh data in Grid. When you call:



Hi.

The problem is that gv_pm_DataBound event is fired each time when you refresh data in Grid. When you call:

gv_pm.DataSource = rslt.ToList();
gv_pm.DataBind(); 





it refreshes everything, including footer DropDownList drp_pmm and sets SelectedIndex to -1.



So after selecting new value in the drp_state you refresh the footer DropDownList as well (gv_pm_DataBound is called after selecting something in the drp_state dropdownlist). To avoid this you have to save the selected value from drp_pmm before refreshing data, and then set it after, check the code below, I used local variable, called _selected_drp_pmm for this purpose.



To resolve the issue you can try to save the selected value from the drp_pmm and then set it after binding the data. Please, pay attention to the code highlighted in bold and drp_state_SelectedIndexChanged event handler, which was also changed.





it refreshes everything, including footer DropDownList drp_pmm and sets SelectedIndex to -1.

So after selecting new value in the drp_state you refresh the footer DropDownList as well (gv_pm_DataBound is called after selecting something in the drp_state dropdownlist). To avoid this you have to save the selected value from drp_pmm before refreshing data, and then set it after, check the code below, I used local variable, called _selected_drp_pmm for this purpose.

To resolve the issue you can try to save the selected value from the drp_pmm and then set it after binding the data. Please, pay attention to the code highlighted in bold and drp_state_SelectedIndexChanged event handler, which was also changed.

protected void drp_state_SelectedIndexChanged(object sender, EventArgs e)
{
    BindGrid();
}

private int _selected_drp_pmm = -1;
void BindGrid()
{
    if (gvItemsTest.FooterRow != null)
    {
        DropDownList ddlTestPostback = gvItemsTest.FooterRow.FindControl("ddlTestPostback") as DropDownList;
        _selected_drp_pmm = ddlTestPostback.SelectedIndex;
    }

    using (QTEL_Entities qtl = new QTEL_Entities())
    {
        var rslt = from tab in qtl.STATION_PM
                   where tab.ID_STATION == drp_state.SelectedValue
                   select tab;

        gv_pm.DataSource = rslt.ToList();
        gv_pm.DataBind();
    }
}

protected void gv_pm_DataBound(object sender, EventArgs e)
        {
          
                DropDownList drp_pmm = gv_pm.FooterRow.FindControl("drp_pmm") as DropDownList;
 
                using (QTEL_Entities qtl = new QTEL_Entities())
                {
                    var pm = from p in qtl.PARAMETRE_MESURE
                             orderby p.ID_PM
                             select new
                             {
                                 id = p.ID_PM,
                                 name = p.LIBELE_PM
                             };
 
                    drp_pmm.DataSource = pm.ToList();
                    drp_pmm.DataTextField = "name";
                    drp_pmm.DataValueField = "id";
                    drp_pmm.DataBind();
                    drp_pmm.Items.Insert(0, new ListItem("Parametre de mesure", ""));
                    ddlTestPostback.SelectedIndex = _selected_drp_pmm;
 
                }
        }





Suggestion, if you need to resolve some very difficult issue, try to create simple case and check out if it works. Divide your issue into parts and investigate these parts one by one. Try to create simple project with grid and dropdownlist and see if it works. Always try to divide into parts and simplify the difficult problem as much as possible. This way you would be able to find a solution.



One question, if the drp_pmm is not dependent on data, why you put it in the footer? You can locate it outside of the Grid gv_pm.



Suggestion, if you need to resolve some very difficult issue, try to create simple case and check out if it works. Divide your issue into parts and investigate these parts one by one. Try to create simple project with grid and dropdownlist and see if it works. Always try to divide into parts and simplify the difficult problem as much as possible. This way you would be able to find a solution.

One question, if the drp_pmm is not dependent on data, why you put it in the footer? You can locate it outside of the Grid gv_pm.


这篇关于始终选择我的下拉列表的第一个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆