1

我想在 gridview 上使用下拉列表...我有来自 asp.net 的以下代码

<asp:GridView ID="grdvEventosVendedor" runat="server" AllowPaging="True" 
                AutoGenerateColumns="False" CellPadding="4" DatakeyNames="idCita"
                EmptyDataText="No Hay Eventos Para Este Vendedor" ForeColor="#333333" 
                GridLines="None" AllowSorting="True"
                onpageindexchanging="grdvEventosVendedor_PageIndexChanging" 
                onrowcommand="grdvEventosVendedor_RowCommand" 
                onsorting="grdvEventosVendedor_Sorting" CellSpacing="1" HorizontalAlign="Center">
                <AlternatingRowStyle BackColor="White" ForeColor="#284775"/>

                <Columns>
                    <asp:TemplateField HeaderText="" ItemStyle-Width="35px">
                        <ItemTemplate>
                            <asp:ImageButton ID="imgBtnEdicEvento" runat="server" 
                                CommandArgument='<%# Eval("idCita")%>' CommandName="Edicion" 
                                Height="32px" ImageUrl="~/img/pencil_32.png" Width="32px" />
                        </ItemTemplate>
                    </asp:TemplateField>

                    <asp:TemplateField HeaderText="" ItemStyle-Width="35px">
                        <ItemTemplate>
                            <asp:ImageButton ID="imgBtnDelete" runat="server"
                                CommandName="Borrar" 
                                ImageUrl="~/img/1385_Disable_16x16_72.png" 
                                onclientclick="return confirm('¿Desea eliminar el registro?');" 
                                CommandArgument='<%# Eval("idCita")%>' />
                        </ItemTemplate>
                    </asp:TemplateField>

                    <asp:BoundField DataField="Cliente" HeaderText="Cliente" InsertVisible="False" ReadOnly="True" SortExpression="Cliente" ItemStyle-Width="50px" />
                    <asp:BoundField DataField="Empresa" HeaderText="Empresa" InsertVisible="False" ReadOnly="True" SortExpression="Empresa" ItemStyle-Width="50px"/>
                    <asp:BoundField DataField="Telefono" HeaderText="Telefono" InsertVisible="False" ReadOnly="True" SortExpression="Telefono" ItemStyle-Width="50px"/>
                    <asp:BoundField DataField="Nextel" HeaderText="Nextel" InsertVisible="False" ReadOnly="True" SortExpression="Nextel" ItemStyle-Width="50px"/>
                    <asp:BoundField DataField="Tipo" HeaderText="Tipo" InsertVisible="False" ReadOnly="True" SortExpression="Tipo" ItemStyle-Width="50px"/>
                    <asp:BoundField DataField="Descripcion" HeaderText="Descripcion" InsertVisible="False" ReadOnly="True" SortExpression="Descripcion" ItemStyle-Width="100px"/>

                    <asp:TemplateField HeaderText="Fecha" SortExpression="Fecha" ItemStyle-Width="50px">
                        <ItemTemplate>                                            
                            <%# DataBinder.Eval(Container.DataItem, "Fecha", "{0:dd/MM/yyyy}")%>
                        </ItemTemplate>

                        <EditItemTemplate>                                        
                            <asp:TextBox ID="tbxFecha"  runat="server" Text='<%#Bind("Fecha","{0:dd/MM/yyyy}") %>' ValidationGroup="gpEdicionAgenda">
                            </asp:TextBox>
                        </EditItemTemplate>
                    </asp:TemplateField>

                    <asp:BoundField DataField="HoraInicio" HeaderText="Hora" InsertVisible="False" ReadOnly="True" SortExpression="HoraInicio" ItemStyle-Width="50px"/>
                    <asp:BoundField DataField="Lugar" HeaderText="Lugar" InsertVisible="False" ReadOnly="True" SortExpression="Lugar" ItemStyle-Width="50px"/>

                    <asp:TemplateField HeaderText="Estado" ItemStyle-Width="50px">
                        <ItemTemplate>
                            <asp:DropDownList ID="dpdListEstatus" runat="server">
                                <asp:ListItem>Pendiente</asp:ListItem>
                                <asp:ListItem>Atendido</asp:ListItem>
                            </asp:DropDownList>
                        </ItemTemplate>
                    </asp:TemplateField>

                    <asp:TemplateField HeaderText="CRM" ItemStyle-Width="25px">
                        <ItemTemplate>
                            <asp:ImageButton ID="imgBtnCRM" runat="server"
                                CommandArgument='<%# Eval("IdCliente")%>' CommandName="CRM" 
                                ImageUrl="~/img/activar.png" Width="16px" Height="16px" />
                        </ItemTemplate>
                    </asp:TemplateField>

                    <asp:TemplateField HeaderText="VM" ItemStyle-Width="25px">
                        <ItemTemplate>
                            <asp:ImageButton ID="imgBtnVerMas" runat="server"
                                CommandArgument='<%# Eval("IdCliente")%>' CommandName="VerMas" 
                                ImageUrl="~/img/search.png" Width="16px" Height="16px" />
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>

                <EditRowStyle BackColor="#999999" Font-Size="Small" />
                <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" Font-Size="Larger" />
                <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
                <RowStyle BackColor="#F7F6F3" ForeColor="#333333" HorizontalAlign="Center" Font-Size="Small" />
                <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
                <SortedAscendingCellStyle BackColor="#E9E7E2" />
                <SortedAscendingHeaderStyle BackColor="#506C8C" />
                <SortedDescendingCellStyle BackColor="#FFFDF8" />
                <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
            </asp:GridView>

它所说的部分是我希望ddl成为的部分......

您可能还注意到我总共使用了 4 个按钮来进行编辑、删除等……但我想那是另一个故事了……

我希望它做几件事...首先,您会注意到我有带有 2 个值的下拉列表...这是因为我从查询中获取了一个数据源,而这些是该列的 2 个可能值可以得到...

所以#1应该是......我怎样才能让Ddl的选定值是我从查询中得到的......

和#2我可以手动更改ddl的值,所以我希望它进行回发并使用新值更新该特定行(我需要回发的原因是我可以触发例如ddl ONSELECTEDINDEX CHANGED和因此使用cs文件创建一个新查询,更新行,然后再次刷新gridview)

我想所有这些都可能与 rowcommand 有关,就像其他 4 个按钮的工作方式一样。

我正在使用 C#,所以如果你的方法涉及 cs 文件,如果你能帮助我使用 C#,那将会很有帮助......

谢谢

4

3 回答 3

4

您应该在 GridView 上使用 OnRowDatabound 事件。喜欢:

<asp:GridView ID="grdvEventosVendedor" OnRowDatabound="grdvEventosVendedor_RowDataBound">
        <asp:TemplateField HeaderText="Estado" ItemStyle-Width="50px">
            <ItemTemplate>
                <asp:DropDownList ID="dpdListEstatus" runat="server" OnSelectedIndexChanged="dpdListEstatus_SelectedIndexChanged" AutoPostBack="True">
                    <asp:ListItem>Pendiente</asp:ListItem>
                    <asp:ListItem>Atendido</asp:ListItem>
                </asp:DropDownList>
            </ItemTemplate>
        </asp:TemplateField>
</asp:GridView>

然后在 .cs 后端代码中,您应该找到控件并根据数据项值设置它的选定值。

protected void grdvEventosVendedor_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        DropDownList dpdListEstatus = e.Row.FindControl("dpdListEstatus") as DropDownList;
        dpdListEstatus.SelectedValue = DataBinder.Eval(e.Row.DataItem, "FieldName").ToString();
    }
}


protected void dpdListEstatus_SelectedIndexChanged(object sender, EventArgs e)
{
    //your logic goes here
}

您可以在 ASPX 代码的下拉列表中设置 SelectedIndexChanged,并且在该后端代码中您可以继续您的逻辑。哦,别忘了在你的下拉菜单中设置 autopostback = true 。

于 2012-08-14T20:10:18.990 回答
0

您可以使用 GridView RowDataBound 事件来访问下拉列表,同样为下拉列表设置 selectedindexchanged 事件。

请参阅下面的此链接,其中显示了您将需要的解决方案的基础知识

http://www.codeproject.com/Articles/53559/Accessing-a-DropDownList-inside-a-GridView

于 2012-08-14T20:18:21.400 回答
0
This is what I did that worked for me:

**Snippet from aspx:**

  <asp:TemplateField HeaderText="RECORD_STATUS" SortExpression="RECORD_STATUS">
     <EditItemTemplate>
        <asp:DropDownList  runat="server" ID="ddlRecStatus"                                                SelectedIndex='<%# GetselectedRecStatus(Eval("RECORD_STATUS")) %>'
                                    DataSource = '<%# Recs_Status %>' />
                            </EditItemTemplate>
  </asp:TemplateField>

**Snippet from code-behind:**

 protected void grdSAEdit_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            //Get the refernce to the list control
            DropDownList ddlRecStatus = (DropDownList)(grdSAEdit.Rows[e.RowIndex].FindControl("ddlRecStatus"));

            // Add it to the parameters
            e.NewValues.Add("RECORD_STATUS", ddlRecStatus.Text);

        }

        protected string[] Recs_Status
        {
            get { return new string[] {  "A", "E", "V", "Z" }; }
        }

        protected int GetselectedRecStatus(object status)
        {
            return Array.IndexOf(Recs_Status, status.ToString());
        }
于 2013-02-06T20:16:21.477 回答