我有这段代码:
<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。 我的问题是我希望下拉列表始终显示2个选项True和False,但它也应该具有从数据源获得的选定值。
如果我删除2行
<asp:ListItem Text="True" Value="True" />
<asp:ListItem Text="False" Value="False" />
它只显示从db获得的值,如果我留下这些行,它会显示3个值:一个来自db,True,False由此代码添加。 那么,我该怎么做呢?我希望我很清楚,谢谢
IT完成了! 再次感谢您
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
答案 0 :(得分:1)
尝试此操作,将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>
<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>
<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
首先,使用网格视图的RowCommand事件,将编辑标签的值获取到ViewState中,然后使用viewstate并处理RowDataBound
要更新您的数据,您可以使用roeupdate事件,如下所示
protected void Gridview2_RowUpdating(Object sender, GridViewUpdateEventArgs e)
{
//add code for updating values in database
}
答案 1 :(得分:0)
如果我理解正确,你只需要值为TRUE和FALSE,那么,你不需要数据源来做到这一点......
删除此属性DataSourceID,DataTextField,DataValueField和SelectedValue ...
请试试。