gridview中模板字段中下拉列表的问题

时间:2014-11-19 09:13:46

标签: c# asp.net gridview drop-down-menu disabled-control

好的,我有一个gridview,如下面的html所示:

 <asp:GridView ID="gridDetaljiNarudzbe" AutoGenerateColumns="false" AllowPaging="true" PageSize="10" runat="server" OnRowCommand="gridDetaljiNarudzbe_RowCommand" OnPageIndexChanging="gridDetaljiNarudzbe_PageIndexChanging" OnRowDataBound="gridDetaljiNarudzbe_RowDataBound">
        <Columns>
           <asp:BoundField DataField="Naziv" HeaderText="Naziv" />
           <asp:BoundField DataField="Sifra" HeaderText="Šifra" />
           <asp:BoundField DataField="Cijena" HeaderText="Cijena" />
           <asp:BoundField DataField="Kolicina" HeaderText="Količina" />
                <asp:TemplateField HeaderText="Ocjena">
                <ItemTemplate>
                    <asp:DropDownList ID="DropDownList1" runat="server"></asp:DropDownList>
                </ItemTemplate>
            </asp:TemplateField>
          <asp:TemplateField>
              <ItemTemplate> 
                  <asp:LinkButton ID="btnOcijeni" title="Ocijeni proizvod" CommandName="OcijeniCommand" CommandArgument='<%# Eval("ProizvodID") %>' runat="server"><img src="../images/ocijeni.png" /></asp:LinkButton>
              </ItemTemplate>
          </asp:TemplateField>
        </Columns>
    </asp:GridView>

为了填写DB中每条记录的下拉列表,我使用了以下代码(RowDataBound事件):

if (e.Row.RowType == DataControlRowType.DataRow)
            {
                DropDownList drop = e.Row.FindControl("DropDownList1") as DropDownList;
                drop.Items.Add(new ListItem(""));
                drop.Items.Add(new ListItem("1"));
                drop.Items.Add(new ListItem("2"));
                drop.Items.Add(new ListItem("3"));
                drop.Items.Add(new ListItem("4"));
                drop.Items.Add(new ListItem("5"));
            }

从每行的下拉列表中选择一个值:

   foreach (GridViewRow gr in gridDetaljiNarudzbe.Rows)
{
                        DropDownList drop = gr.FindControl("DropDownList1") as DropDownList;
                     // now selecting a value from dropdownlist 
                 int selectednumber = Convert.ToInt32(drop.Text);
}

现在我的问题是,假设我们在网格中有两条记录,我想从第二行的第二个下拉列表中获取值(假设我这样做了)。当我按下第一行中第一个记录的按钮时,我从第二个下拉列表中获取的值现在被插入到数据库中,就像我从第一个下拉列表中选择了一样。

此外,我想知道是否可能在下拉列表中添加了某些内容并插入到数据库中,现在禁用该下拉列表并转换为仅显示5而不是下拉列表的静态文本?

有人可以帮我解决这个问题吗?我尝试了一切,但我还没有成功。 :/

编辑:

当我从第二个网格中选择一些内容时(第二个下拉列表,因为你可以看到图片),当我按下按钮对第一个产品进行评分时,我从第二个下拉列表中选择了我的评分。现在更清楚了吗?

Problem

谢谢!

这里编辑的是瑜伽士的代码:

   List<hsp_Proizvodi_SprijeciDvaPutaOcijeniti_Result> lista = ServisnaKlasa.SprijeciDvostrukoOcjenjivanje(ProizvodID, Sesija.kupac.KupacID);
                foreach (GridViewRow gr in gridDetaljiNarudzbe.Rows)
                {
                    if (lista.Count == 0)
                    {
                        DropDownList drop = gr.FindControl("DropDownList1") as DropDownList;
                        if (drop.SelectedIndex != 0)
                        {
                            Ocjene o = new Ocjene();
                            o.KupacID = Sesija.kupac.KupacID;
                            o.ProizvodID = ProizvodID;
                            o.Datum = DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture);
                            o.Ocjena = Convert.ToInt32(drop.Text);
                            ServisnaKlasa.OcjenjivanjeProizvoda(o);
                            string poruka = "Proizvod uspješno ocijenjen!";
                            ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('" + poruka + "');", true);
                            drop.SelectedIndex = 0;

                        }
                    }
                    else
                    {
                        string poruka = "Ovaj proizvod ste već ocijenili!";
                        ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('" + poruka + "');", true);
                    }
                }

1 个答案:

答案 0 :(得分:1)

遵循这种方法

  1. 添加gridview&#39; RowCommand事件。
  2. 为您的链接按钮提供命令参数。像这样

    <asp:LinkButton ID="btnOcijeni" title="Ocijeni proizvod" CommandName="OcijeniCommand" CommandArgument='<%#Eval("ProizvodID") + ";" +((GridViewRow) Container).RowIndex%>' runat="server"><img src="../images/ocijeni.png" /></asp:LinkButton>
    
  3. (我刚在rowindex的命令参数中添加了一个字段)

    1. 现在,只要点击链接按钮,就会调用RowCommand事件。

      protected void gridDetaljiNarudzbe_RowCommand(object sender, GridViewCommandEventArgs e) { if (e.CommandName == "OcijeniCommand") { arg = e.CommandArgument.ToString().Split(';'); int index = Convert.ToInt32(arg[1].ToString()); //Finding same row dropdown list //This will give you same row dropdownlist not the below row DropDownList drpdwn1= (DropDownList)gridDetaljiNarudzbe.Rows[index].FindControl("DropDownList1");
      //Now for BoundField values GridViewRow row = GridView1.Rows[index]; string NazivText= row.Cells[0].Text;//It will give Naziv's value //same way you can get other field values //And save into db. } }

    2. <强> 修改

      将下拉列表转换为标签

      1. 在下拉列表下方添加标签,并将其设为Visible=false

         <ItemTemplate>
                        <asp:DropDownList ID="DropDownList1" runat="server"></asp:DropDownList>
                        <asp:Label ID="lbl_ForDrpdwnVal" runat="server"  Visible="false"></asp:Label>               
        

      2. 现在在同一项命令事件中,在将值插入db。

        后添加以下代码
                            //Access the new label
                            Label lblfordrpdwn= (Label)gridDetaljiNarudzbe.Rows[index].FindControl("lbl_ForDrpdwnVal");
                            lblfordrpdwn.Text= drpdwn1.SelectedItem.Text;//Setting value from dropdown to lbel
                            lblfordrpdwn.Visible=true;//Showing label
                            drpdwn1.Visible=false;//Hiding the dropdown