How to get a e.CommandArgument from a row in a gridview while using a dropdownlist inside it in asp.net c#?

StackOverflow https://stackoverflow.com/questions/11972673

Question

recently I asked a similar question, which you can find in the link below

Using Dropdownlist in a gridview Asp.Net?

I have a gridview that looks like this...

<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>

As mentioned in the link, I had to manually add some code, like OnSelectedIndexChanged="dpdListEstado_SelectedIndexChanged" AutoPostBack="True" and also add this on the cs file...

    protected void dpdListEstado_SelectedIndexChanged(object sender, EventArgs e)
    {

    }

I have tried it and it did work, whenever I change the value of a ddl, it does enter to the void int the cs file...

however now I have a question...

I need the Id from the record that it's stored in a specific row (idCita, as in the buttons I have which use a CommandArgument)... that way can use another class I have which would trigger a query that would edit a field called Estado....

However, I noticed that when I use CommandArguments, I use another void called...

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")
        {
            //....
        }
    }

with this I can call both e.CommandName which tells me which button I pressed, and e.CommandArgument which is the value for idCita... however I noticed that the ddl void that I previously showed you doesnt have a GridViewCommandEventArgs e, instead it has a EventArgs e...

Is there a way that I can get the value of idCita from the gridview, all the way to the ddl void??

What do I need to add in the gridview code and in the cs file??

Thanks

Était-ce utile?

La solution

Refactor you're code something like this - it should get you most of the way there, additionally look at converting the else if statements to a more readable switch case statement...

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")
    {

    }
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top