在gridview Asp.Net中使用Dropdownlist?

时间:2012-08-14 19:29:45

标签: c# asp.net gridview html-select

我想在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的Selected Value成为我从查询中获得的值....

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

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

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

由于

3 个答案:

答案 0 :(得分: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后端代码中,您应该找到控件并根据dataitem值设置它的选定值。

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,并在该后端代码中继续您的逻辑。 Ow,不要忘记在下拉列表中设置autopostback = true。

答案 1 :(得分:0)

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

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

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

答案 2 :(得分: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());
        }