【问题标题】:Problems with dropdown list in template field in gridviewgridview中模板字段中的下拉列表问题
【发布时间】:2014-11-19 09:13:46
【问题描述】:

好的,所以我有一个网格视图,如下 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>

为了填写数据库中每条记录的下拉列表,我使用了以下代码(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 而不是下拉列表的静态文本?

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

编辑:

当我从第二个网格中选择一些东西(你可以看到图片的第二个下拉列表)并且当我按下按钮对第一个产品进行评分时,我会从我选择的第二个下拉列表中获得评分。现在清楚了吗?

谢谢!

这里是 yogi 的代码:

   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);
                    }
                }

【问题讨论】:

  • 第一行下拉和第二行下拉的关系是什么。你不是很清楚。你想要什么,有什么问题你能澄清一下吗?
  • 你能看到这张图片吗:oi59.tinypic.com/2s0k4k9.jpg 当我从第二个网格中选择一些东西(你可以看到图片的第二个下拉列表)并且当我按下按钮对第一个产品进行评分时,我得到了从我选择的第二个下拉列表中评分。现在清楚了吗?
  • 对不起,我看不到这张照片。可以编辑您的问题吗?
  • 你只是在说正在发生的事情,而不是你想要的!你想停止从第二个下拉列表中获得成绩吗?
  • 不,不是这样,请查看更新问题上的图片。附言我希望现在更清楚

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


【解决方案1】:

遵循这种方法

  1. 添加gridview的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>
    

(我刚刚在 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. } }

编辑

用于将下拉菜单转换为标签

  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
    

【讨论】:

  • 老兄,你最好,它有效!!!你能再帮我做一件事吗?现在,当某些内容已插入数据库(某行)时,我想在网格中显示一个标签,显示等级(1,2,3,4,5),而不是该特定行的下拉列表插入了哪个成绩?
  • 很高兴它对您有所帮助。 :) 将值插入数据库后,您想用标签替换下拉列表吗?
  • @perkes456 查看编辑。希望它能解决你的问题
  • 是的瑜伽士,谢谢你,不幸的是我还有一个问题:(。当 1 个用户评价 3 个产品时,另一个用户来购买所有 3 个相同的产品,现在当他尝试评价它们时,它不再显示下拉列表,而只是等级标签本身(不是用于对产品进行分级的下拉列表)。有什么办法可以将第一个人的评分踢出第二个人,以便他输入自己的等级?
  • 您是否分别存储每个用户的评分?
猜你喜欢
  • 1970-01-01
  • 2015-01-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-04
  • 1970-01-01
  • 2013-06-02
  • 1970-01-01
相关资源
最近更新 更多