1

最近我问了一个类似的问题,你可以在下面的链接中找到

在gridview Asp.Net 中使用下拉列表?

我有一个看起来像这样的gridview...

<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" 
                onrowdatabound="grdvEventosVendedor_RowDataBound" >
                <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:BoundField DataField="Estado" HeaderText="Estado" InsertVisible="False" ReadOnly="True" SortExpression="Estado" ItemStyle-Width="50px"/>--%>

                    <asp:TemplateField HeaderText="Estado" ItemStyle-Width="50px">
                        <ItemTemplate>
                            <asp:DropDownList ID="dpdListEstado" runat="server" OnSelectedIndexChanged="dpdListEstado_SelectedIndexChanged" AutoPostBack="True">
                                <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>

如链接中所述,我必须手动添加一些代码,例如 OnSelectedIndexChanged="dpdListEstado_SelectedIndexChanged" AutoPostBack="True" 并将其添加到 cs 文件中...

    protected void dpdListEstado_SelectedIndexChanged(object sender, EventArgs e)
    {

    }

我已经尝试过了,它确实有效,每当我更改 ddl 的值时,它确实会进入 cs 文件的 void int ......

但是现在我有一个问题...

我需要存储在特定行中的记录中的Id(idCita,就像我使用CommandArgument的按钮一样)......这样可以使用我拥有的另一个类,它将触发一个查询,该查询将编辑一个名为埃斯塔多……

但是,我注意到当我使用 CommandArguments 时,我使用了另一个名为...

protected void grdvEventosVendedor_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        int intIdCita = int.Parse(e.CommandArgument.ToString());

        if (e.CommandName == "Edicion")
        {
            //Some Code
        }

        else if (e.CommandName == "Borrar")
        {
           //More Code
        }

        else if (e.CommandName == "CRM")
        {
            //Even More Code
        }

        else if (e.CommandName == "VerMas")
        {
            //....
        }
    }

有了这个我可以同时调用 e.CommandName 告诉我我按下了哪个按钮,以及 e.CommandArgument 是 idCita 的值......但是我注意到我之前向你展示的 ddl void 没有 GridViewCommandEventArgs e,而是它有一个 EventArgs e...

有没有一种方法可以让我从 gridview 中获取 idCita 的值,一直到 ddl void?

我需要在gridview代码和cs文件中添加什么?

谢谢

4

1 回答 1

2

重构你的代码是这样的——它应该能让你大部分时间到达那里,另外考虑将 else if 语句转换为更具可读性的 switch case 语句......

protected void dpdListEstado_SelectedIndexChanged(object sender, EventArgs e)
{
    GridViewRow gvr = (GridViewRow)(((Control)sender).NamingContainer);   
    MeaningfulNameHere(int.Parse(grdvEventosVendedor.DataKeys[gvr.RowIndex]),"Estado");
}

protected void grdvEventosVendedor_RowCommand(object sender, GridViewCommandEventArgs e)
{
    MeaningfulNameHere(int.Parse(e.CommandArgument.ToString()),e.CommandName);
}

private void MeaningfulNameHere(int id, string commandName)
{


    if (commandName == "Edicion")
    {
        //Some Code
    }

    else if (commandName == "Borrar")
    {
       //More Code
    }

    else if (commandName == "CRM")
    {
        //Even More Code
    }

    else if (commandName == "VerMas")
    {
        //....
    }

    else if (commandName == "Estado")
    {

    }
}
于 2012-08-15T16:24:01.850 回答