在GridView Asp.Net使用DROPDOWNLIST? [英] Using Dropdownlist in a gridview Asp.Net?

查看:87
本文介绍了在GridView Asp.Net使用DROPDOWNLIST?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用一个DropDownList上的GridView ......我从asp.net下面的代码

I want to use a dropdownlist on a gridview... I have the following code from 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成为部分...

The part where it says is the part where I want the ddl to be...

您将可能也已经注意到,我用一共有4个按键进行编辑,删除等...
但是我想这是另一个故事...

You'll might've also noticed that I use a total of 4 buttons for edit, delete, etc... However I guess that's another story...

我想要它做的一些事情......第一次,你会发现,我有2个值DropDownList的......那是因为我得到一个从查询数据源,而这些都是2可能值此列可以得到...

I want it to do a couple of things... first of, you'll notice that I have the dropdownlist with 2 values... that's because I get a datasource from a query, and those are the 2 possible values that this column can get...

所以,#1应该是...我怎样才能使该DDL的选定的值是一个我从查询得到....

So #1 should be... how can I make that the Ddl's Selected Value is the one I get from the query....

和#2我可以手动更改DDL的价值,所以我想它做回传并更新为新值,特定行(我需要回传会这样我就可以触发,例如原因的DDL ONSELECTEDINDEX改变,因此使用CS文件来创建一个新的查询,更新行,然后再次刷新gridview的)

and #2 I can manually change the value of the ddl, so I want it to make a postback and update that specific row with the new value (the reason I need the postback would be so I can trigger for example a ddl ONSELECTEDINDEX CHANGED and therefore use the cs file to create a new query, update the row, and then refresh the gridview again)

我想这一切可能是与rowcommand,就像其他4个按钮的工作方式。

I suppose that all of this might have something to do with rowcommand, just like the way the other 4 buttons work.

我用这个C#,所以这将是有益的,如果你能使用C#帮助我,如果你的方法包括CS文件...

I'm using C# on this, so It would be helpful if you can help me using C# if you're method involves the cs file...

感谢

推荐答案

您应该使用在GridView中OnRowDatabound事件。这样的:

You should use the OnRowDatabound event on the GridView. Like:

<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后端代码,你应该找到控制它的基础上的DataItem选择的值设置值。

Then in the .cs backend code you should find the control and set it's selected value based on the dataitem values.

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
}

您可以设置的SelectedIndexChanged上的ASPX代码的下拉列表,并在那片后端代码你可以继续你的逻辑。
噢,不要忘记设置自动回= TRUE在你的下拉列表。

You can set the SelectedIndexChanged on your dropdown in the ASPX code and in that piece of backend code you can continue your logic. Ow, and don't forget to set the autopostback = true on your dropdown.

这篇关于在GridView Asp.Net使用DROPDOWNLIST?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆