在过去的几天里,我一直在尝试使用 DetailsView 来显示和编辑从 GridView 获取的单个记录中的数据,它们都从 SqlDataSource 读取它们的数据。
我一直在互联网和 MSDN 上搜索有关如何使用 DetailsView 编辑数据的信息,并且我设法将一些东西拼凑在一起,问题是显然实际的更新方法没有运行。
所以这是我的代码的摘录
GridView 页面中:
<asp:DetailsView ID="DetailsView1" runat="server" Height="50px" Width="125px" AutoGenerateRows="False" DefaultMode="Edit">
<fields>
<asp:TemplateField HeaderText="Codice Cliente">
<ItemTemplate>
<asp:Label ID="lblCliCod" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.cli_cod") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtCliCod" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.cli_cod")%>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Descrizione">
<ItemTemplate>
<asp:Label ID="lblCliDesc" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.cli_desc")%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtCliDesc" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.cli_desc")%>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Nome Utente">
<ItemTemplate>
<asp:Label ID="lblCliUser" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.cli_user")%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtCliUser" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.cli_user")%>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Password">
<ItemTemplate>
<asp:Label ID="lblCliPass" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.cli_pass")%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtCliPass" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.cli_pass")%>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Amministratore">
<ItemTemplate>
<asp:Label ID="lblCliAdmin" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.cli_admin")%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:CheckBox ID="chkCliAdminE" runat="server" Text='<%# CBool(DataBinder.Eval(Container, "DataItem.cli_admin"))%>' />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<EditItemTemplate>
<asp:LinkButton Text="Aggiorna" ID="UpdateButton" runat="Server" CommandName="Update"></asp:LinkButton>
<asp:LinkButton Text="Elimina" ID="DeleteButton" runat="server" CommandName="Edit"></asp:LinkButton>
</EditItemTemplate>
</asp:TemplateField>
</fields>
</asp:DetailsView>
代码隐藏的相关部分:
Private Sub SetupDataSource() 'this is the sqldatasource I'm using for the detailsview
SqlDataSource2.ConnectionString = Assist.connectionString
SqlDataSource2.SelectCommand = detailsSelect
SqlDataSource2.SelectParameters.Add(New Parameter("id"))
SqlDataSource2.UpdateCommand = detailsUpdate
SqlDataSource2.UpdateParameters.Add(New Parameter("cli_cod"))
SqlDataSource2.UpdateParameters.Add(New Parameter("cli_desc"))
SqlDataSource2.UpdateParameters.Add(New Parameter("cli_user"))
SqlDataSource2.UpdateParameters.Add(New Parameter("cli_pass"))
SqlDataSource2.UpdateParameters.Add(New Parameter("cli_admin"))
End Sub
Private Sub SetupDetailsView(id As Int32)
DetailsView1.AutoGenerateRows = False
DetailsView1.DataSource = SqlDataSource2
SqlDataSource2.SelectParameters("id").DefaultValue = id
End Sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
cn.Open()
SetupDataSource()
If Not IsPostBack Then
Dim id As String = Request.QueryString("id")
If id <> "" Then
SetupDetailsView(id)
DetailsView1.DataBind()
End If
End If
End Sub
Protected Sub DetailsView1_ItemUpdating(sender As Object, e As DetailsViewUpdateEventArgs) Handles DetailsView1.ItemUpdating
Dim cliCod As String = (CType(DetailsView1.FindControl("txtCliCod"), TextBox)).Text.ToString()
Dim cliDesc As String = (CType(DetailsView1.FindControl("txtCliDesc"), TextBox)).Text.ToString()
Dim cliUser As String = (CType(DetailsView1.FindControl("txtCliUser"), TextBox)).Text.ToString()
Dim cliPass As String = (CType(DetailsView1.FindControl("txtCliPass"), TextBox)).Text.ToString()
Dim cliAdmin As Boolean = If((CType(DetailsView1.FindControl("chkCliAdminE"), CheckBox)).Checked = True, True, False)
SqlDataSource2.UpdateParameters("cli_cod").DefaultValue = cliCod
SqlDataSource2.UpdateParameters("cli_desc").DefaultValue = cliDesc
SqlDataSource2.UpdateParameters("cli_user").DefaultValue = cliUser
SqlDataSource2.UpdateParameters("cli_pass").DefaultValue = cliPass
SqlDataSource2.UpdateParameters("cli_Admin").DefaultValue = If(cliAdmin = True, "1", "0")
DetailsView1.DataBind()
End Sub
我要么遗漏了某些东西,要么在某处做错了,这是哪一个,为什么?