gridview 中的 DropDown 绑定

DropDown binding in gridview

我有这个代码:

<asp:TemplateField HeaderText="Gestisci IP" SortExpression="GestisciIp">
    <EditItemTemplate>
        <asp:DropDownList ID="ddlGestisciIP" runat="server" AppendDataBoundItems="true" 
            DataSourceID="SqlDataSource3" DataTextField="GestisciIP" 
            DataValueField="GestisciIP" SelectedValue='<%# Bind("GestisciIP") %>'>
             <asp:ListItem Text="True" Value="True" />
             <asp:ListItem Text="False" Value="False"  />
        </asp:DropDownList>
    </EditItemTemplate>
    <ItemTemplate>
        <asp:Label ID="Label2" runat="server" Text='<%# Bind("GestisciIp") %>'></asp:Label>
    </ItemTemplate>
</asp:TemplateField>

数据源包含在当前用户(用户 ID)上选择的字段值,它可以是 True 或 False。 我的问题是 我希望下拉列表始终显示 True 和 False 这两个选项,但它也应该具有从数据源.

中获取的选定值

如果我删除 2 行

<asp:ListItem Text="True" Value="True" />
<asp:ListItem Text="False" Value="False"  />

它只显示从数据库中获取的值,如果我保留这些行,它会显示 3 个值:从数据库中获取的值和此代码添加的 True、False。 那么,我该怎么做呢?希望我说得够清楚了,谢谢


完成! 再次感谢

ASPX

     <asp:TemplateField HeaderText="Gestisci IP" SortExpression="GestisciIp">
                    <EditItemTemplate>
                     <asp:DropDownList ID="ddlGestisciIP" runat="server">

                    </asp:DropDownList>
                    <asp:ListItem Text="True" Value="True" />
                             <asp:ListItem Text="False" Value="False"  />
                        </asp:DropDownList>--%>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label2" runat="server" Text='<%# Bind("GestisciIp") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>

代码隐藏

 Private Sub GridView2_RowCommand(sender As Object, e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView2.RowCommand
  If e.CommandName = "Edit" Then
        Dim gvRow As GridViewRow = DirectCast(DirectCast(e.CommandSource, LinkButton).NamingContainer, GridViewRow)

        Dim lbl As Label = DirectCast(gvRow.FindControl("Label2"), Label)

        ViewState("LabelValue") = lbl.Text
    End If

End Sub

Private Sub GridView2_RowDataBound(sender As Object, e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView2.RowDataBound
    If e.Row.RowType = DataControlRowType.DataRow AndAlso GridView2.EditIndex = e.Row.RowIndex Then
        Dim ddlGestisciIP As DropDownList = DirectCast(e.Row.FindControl("ddlGestisciIP"), DropDownList)
        Dim LblddlGestisciIP As Label = DirectCast(e.Row.FindControl("LblddlGestisciIP"), Label)
        ddlGestisciIP.Items.Add(New ListItem("True", "1"))
        ddlGestisciIP.Items.Add(New ListItem("False", "0"))
        If Convert.ToString(ViewState("LabelValue")) <> "" Then

            ddlGestisciIP.SelectedValue = ddlGestisciIP.Items.FindByText(Convert.ToString(ViewState("LabelValue"))).Value
        End If
    End If


End Sub

Private Sub GridView2_RowUpdating(sender As Object, e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles GridView2.RowUpdating
    Dim ddlGestisciIP As DropDownList = DirectCast(GridView2.Rows(GridView2.EditIndex).FindControl("ddlGestisciIP"), DropDownList)

    SqlDataSourceUtenti.UpdateCommand = "UPDATE [Utenti] SET [RfProfilo] = @RfProfilo, [Nome] = @Nome, [Cognome] = @Cognome, [Username] = @Username, [Password] = @Password, [Badge] = @Badge, [IpCorretto] = @IpCorretto, [GestisciIp] = " & ddlGestisciIP.SelectedValue & " WHERE [IdUtente] = @Original_IdUtente"

End Sub

如果我理解正确,您只需要值 TRUE 和 FALSE,因此,您不需要数据源来执行此操作...

删除此属性 DataSourceID、DataTextField、DataValueField 和 SelectedValue...

请尝试一下。

试试这个,将 Onrowdatabound 事件添加到您的 gridview 并在该事件中绑定您的下拉列表。

  <asp:GridView ID="GridView2" runat="server" AllowPaging="True" CellPadding="4" DataSourceID="SqlDataSourceUtenti" 
                ForeColor="#333333" GridLines="None" AutoGenerateColumns="False" 
                DataKeyNames="IdUtente" BorderColor="#E1E1E1" BorderStyle="Solid" 
                BorderWidth="3px" OnRowDataBound="GridView2_RowDataBound" OnRowCommand="GridView2_RowCommand" OnRowUpdating="GridView2_RowUpdating"  >
                <AlternatingRowStyle BackColor="White" />
                <Columns>
                    <asp:BoundField DataField="RfProfilo" HeaderText="RfProfilo" 
                        SortExpression="RfProfilo" Visible="False" />

                    <asp:BoundField DataField="Nome" HeaderText="Nome" SortExpression="Nome" />

                    <asp:BoundField DataField="Cognome" HeaderText="Cognome" 
                        SortExpression="Cognome" />

                    <asp:BoundField DataField="Username" HeaderText="Username" 
                        SortExpression="Username" />

                    <asp:TemplateField HeaderText="Profilo" SortExpression="Profilo">
                        <EditItemTemplate>
                            <asp:DropDownList ID="DropDownList1" runat="server" 
                                DataSourceID="SqlDataSource2" DataTextField="Nome" DataValueField="idProfilo" 
                                SelectedValue='<%# Bind("RfProfilo") %>'>
                            </asp:DropDownList>
                        </EditItemTemplate>
                        <ItemTemplate>
                            <asp:Label ID="Label1" runat="server" Text='<%# Bind("NomeProfilo") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>

                    <asp:TemplateField HeaderText="Password" SortExpression="Password">
                        <EditItemTemplate>
                            <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Password") %>'></asp:TextBox>
                        </EditItemTemplate>
                        <ItemTemplate>
                            <asp:Label ID="Label3" runat="server" Text='<%# mascheraPassword(Eval("Password").ToString()) %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>


                    <asp:BoundField DataField="Badge" HeaderText="Badge" SortExpression="Badge" />

                    <asp:TemplateField HeaderText="IP di origine" SortExpression="IpCorretto">
                        <EditItemTemplate>
                            <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("IpCorretto") %>'></asp:TextBox>
                        </EditItemTemplate>
                        <ItemTemplate>
                            <asp:Label ID="lblIpCorretto" maxlength="16" runat="server" Text='<%# limitaCaratteri(Eval("IpCorretto").ToString()) %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>




                    <asp:TemplateField HeaderText="Gestisci IP" SortExpression="GestisciIp">
                        <EditItemTemplate>
                         <asp:DropDownList ID="ddlGestisciIP" runat="server">

                        </asp:DropDownList>

                        </EditItemTemplate>
                        <ItemTemplate>
                            <asp:Label ID="Label2" runat="server" Text='<%# Bind("GestisciIp") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>




                    <asp:TemplateField ShowHeader="False">
                        <EditItemTemplate>
                            <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True" 
                                CommandName="Update" Text="Aggiorna" ForeColor="White"></asp:LinkButton>
                            &nbsp;<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" 
                                CommandName="Cancel" Text="Annulla" ForeColor="White"></asp:LinkButton>
                        </EditItemTemplate>
                        <ItemTemplate>
                            <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" 
                                CommandName="Edit" Text="Modifica"></asp:LinkButton>
                            &nbsp;<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" 
                                CommandName="Delete" Text="Elimina"></asp:LinkButton>

                        </ItemTemplate>
                    </asp:TemplateField>



                </Columns>
                <EditRowStyle BackColor="#2461BF" />
                <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
                <RowStyle BackColor="#EFF3FB" />
                <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
                <SortedAscendingCellStyle BackColor="#F5F7FB" />
                <SortedAscendingHeaderStyle BackColor="#6D95E1" />
                <SortedDescendingCellStyle BackColor="#E9EBEF" />
                <SortedDescendingHeaderStyle BackColor="#4870BE" />
            </asp:GridView>

在代码后面

 Protected Sub GridView2_RowCommand(ByVal sender As Object, ByVal e As GridViewCommandEventArgs)
      If e.CommandName = "Edit" Then
            Dim gvRow As GridViewRow = DirectCast(DirectCast(e.CommandSource, LinkButton).NamingContainer, GridViewRow)

            Dim lbl As Label = DirectCast(gvRow.FindControl("Label2"), Label)

            ViewState("LabelValue") = lbl.Text
        End If

    End Sub



    Private Sub GridView2_RowDataBound(sender As Object, e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView2.RowDataBound
        If e.Row.RowType = DataControlRowType.DataRow AndAlso GridView2.EditIndex = e.Row.RowIndex Then
            Dim ddlGestisciIP As DropDownList = DirectCast(e.Row.FindControl("ddlGestisciIP"), DropDownList)

            ddlGestisciIP.Items.Add(New ListItem("True", "1"))
            ddlGestisciIP.Items.Add(New ListItem("False", "0"))
            If Convert.ToString(ViewState("LabelValue")) <> "" Then

                ddlGestisciIP.SelectedValue = ddlGestisciIP.Items.FindByText(Convert.ToString(ViewState("LabelValue"))).Value
            End If
        End If



    End Sub

首先,使用 Grid View 的 RowCommand 事件,将 Edited Label 的值获取到 ViewState,然后使用 viewstate 并处理你的 RowDataBound

要更新您的数据,您可以使用 roeupdate 事件,如下所示

 protected void Gridview2_RowUpdating(Object sender, GridViewUpdateEventArgs e)
    {
//add code for updating values in database
}