在GridView中隐藏UserId

时间:2012-07-20 10:50:45

标签: vb.net sql-server-2008 gridview

我有2个GridView,通过一个复选框,我可以在GridView 1(gvAll)中选择行,并将所选行的多个值传递给另一个GridView 2(gvSelected)。而我又将其用于将所选内容插入到数据库中。 GridViews中的数据是用户数据UserId,Firstname,Surname,ServiceNo。我要插入整个选定记录中的每个值。但是,虽然这部分工作正常,但只有当UserId显示在我不想要的GridView中时才有效。我想隐藏UserId但仍然将值插入数据库。 有解决方法吗?

<div>
        <asp:Label ID="lbTitle" runat="server" Text=""></asp:Label>
        <asp:DataList ID="DataList1" OnItemCommand="DataList1_ItemCommand"
            OnItemDataBound="DataList1_ItemDataBound" RepeatDirection="Horizontal" runat="server" Width="500">

        <SeparatorTemplate>

            </SeparatorTemplate>
            <ItemTemplate>
                <asp:LinkButton ID="lnkbtnPaging" runat="server" CommandArgument='<%# Bind("PageIndex") %>'
                    Text='<%# Bind("PageText") %>'></asp:LinkButton>
            </ItemTemplate>
        </asp:DataList>

        <br />
                <asp:Panel ID="pnlgvAll" runat="server">

           <asp:GridView ID="gvAll" runat="server" 
        AutoGenerateColumns = "false"  AllowPaging ="true"   
        OnPageIndexChanging = "OnPaging" PageSize = "10" CssClass="rounded-corner" Width="550" >
       <Columns>
        <asp:TemplateField>
            <HeaderTemplate>
                <asp:CheckBox ID="chkAll" runat="server" onclick = "checkAll(this);" AutoPostBack = "true"  OnCheckedChanged = "CheckBox_CheckChanged"/>
            </HeaderTemplate> 
            <ItemTemplate>
                <asp:CheckBox ID="chk" runat="server" onclick = "Check_Click(this)" AutoPostBack = "true"  OnCheckedChanged = "CheckBox_CheckChanged" />
            </ItemTemplate>
        </asp:TemplateField>

        <asp:BoundField DataField = "UserId" HeaderText = "UserId" HtmlEncode = "false"  /> 
        <asp:BoundField DataField = "serviceNo" HeaderText = "Service No" HtmlEncode = "false" />
        <asp:BoundField DataField = "forename" HeaderText = "First Name" HtmlEncode = "false" />
        <asp:BoundField DataField = "surname" HeaderText = "Surname" HtmlEncode = "false" />
       </Columns> 

    </asp:GridView> </asp:Panel>
    <asp:Label ID="Msg" runat="server"></asp:Label>
            <br />
            <asp:UpdatePanel ID="UpdatePanel2" runat="server">
                    <ContentTemplate>
<asp:GridView ID="gvSelected" runat="server" 
AutoGenerateColumns = "False" EmptyDataText = "No Firefighters Selected" CssClass="rounded-corner" Width="500"   >

<Columns>
<asp:TemplateField HeaderText="UserId">
        <ItemTemplate>
            <input id="txtDetUsr" type="hidden" runat="server" value='<%# Bind("UserId") %>'  />
                <%--<asp:Label ID="lbl1" runat="server" Text='<%# Bind("UserId") %>'></asp:Label>--%>
        </ItemTemplate>
       </asp:TemplateField>
    <asp:TemplateField HeaderText="Service No">
        <ItemTemplate>
            <asp:Label ID="Label1" runat="server" Text='<%# Bind("serviceNo") %>'></asp:Label>
        </ItemTemplate>
        <EditItemTemplate>"
            <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("serviceNo") %>'></asp:TextBox>
        </EditItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="First Name">
        <ItemTemplate>
            <asp:Label ID="Label2" runat="server" Text='<%# Bind("forename") %>'></asp:Label>
        </ItemTemplate>
        <EditItemTemplate>
            <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("forename") %>'></asp:TextBox>
        </EditItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Surname">
        <ItemTemplate>
            <asp:Label ID="Label3" runat="server" Text='<%# Bind("surname") %>'></asp:Label>
        </ItemTemplate>
        <EditItemTemplate>
            <asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("surname") %>'></asp:TextBox>
        </EditItemTemplate>
    </asp:TemplateField>
    </Columns>

<HeaderStyle BackColor="Green"></HeaderStyle>
</asp:GridView>
 </ContentTemplate>
                </asp:UpdatePanel>
<asp:Button ID="But3" runat="server" Text="My Text" OnClick="btnMyText_Click" OnClientClick="btClick3()" OnCommand="Button_Command" CommandArgument="AddDetach" />
        </div>

Private Sub BindPrimaryGrid()
        Dim constr As String = ConfigurationManager.ConnectionStrings("ConString").ConnectionString
        Dim query As String = "select UserId, serviceNo, forename, surname from userProfilesTbl"
        Dim con As New SqlConnection(constr)
        Dim sda As New SqlDataAdapter(query, con)
        Dim dt As New DataTable()
        sda.Fill(dt)
        gvAll.DataSource = dt
        gvAll.DataBind()
    End Sub
    Protected Sub OnPaging(ByVal sender As Object, ByVal e As GridViewPageEventArgs)
        GetData()
        gvAll.PageIndex = e.NewPageIndex
        BindPrimaryGrid()
        SetData()
    End Sub
    Private Sub GetData()
        Dim dt As DataTable
        If ViewState("SelectedRecords") IsNot Nothing Then
            dt = DirectCast(ViewState("SelectedRecords"), DataTable)
        Else
            dt = CreateDataTable()
        End If
        Dim chkAll As CheckBox = DirectCast(gvAll.HeaderRow _
                            .Cells(0).FindControl("chkAll"), CheckBox)
        For i As Integer = 0 To gvAll.Rows.Count - 1
            If chkAll.Checked Then
                dt = AddRow(gvAll.Rows(i), dt)
            Else
                Dim chk As CheckBox = DirectCast(gvAll.Rows(i) _
                                .Cells(0).FindControl("chk"), CheckBox)
                If chk.Checked Then
                    dt = AddRow(gvAll.Rows(i), dt)
                Else
                    dt = RemoveRow(gvAll.Rows(i), dt)
                End If
            End If
        Next
        ViewState("SelectedRecords") = dt
    End Sub
    Private Sub SetData()
        Dim chkAll As CheckBox = DirectCast(gvAll.HeaderRow.Cells(0).FindControl("chkAll"), CheckBox)
        chkAll.Checked = True
        If ViewState("SelectedRecords") IsNot Nothing Then
            Dim dt As DataTable = DirectCast(ViewState("SelectedRecords"), DataTable)
            For i As Integer = 0 To gvAll.Rows.Count - 1
                Dim chk As CheckBox = DirectCast(gvAll.Rows(i).Cells(0).FindControl("chk"), CheckBox)
                If chk IsNot Nothing Then
                    Dim dr As DataRow() = dt.[Select]("serviceNo = '" & gvAll.Rows(i).Cells(1).Text & "'")
                    chk.Checked = dr.Length > 0
                    If Not chk.Checked Then
                        chkAll.Checked = False
                    End If
                End If
            Next
        End If
    End Sub
    Private Function CreateDataTable() As DataTable
        Dim dt As New DataTable()
        dt.Columns.Add("UserId")
        dt.Columns.Add("serviceNo")
        dt.Columns.Add("forename")
        dt.Columns.Add("surname")
        dt.AcceptChanges()
        Return dt
    End Function
    Private Function AddRow(ByVal gvRow As GridViewRow, ByVal dt As DataTable) As DataTable
        Dim dr As DataRow() = dt.Select("serviceNo = '" _
                                    & gvRow.Cells(1).Text & "'")
        If dr.Length <= 0 Then
            dt.Rows.Add()
            dt.Rows(dt.Rows.Count - 1)("UserId") = gvRow.Cells(1).Text
            dt.Rows(dt.Rows.Count - 1)("serviceNo") = gvRow.Cells(2).Text
            dt.Rows(dt.Rows.Count - 1)("forename") = gvRow.Cells(3).Text
            dt.Rows(dt.Rows.Count - 1)("surname") = gvRow.Cells(4).Text
            dt.AcceptChanges()
        End If
        Return dt
    End Function
    Private Function RemoveRow(ByVal gvRow As GridViewRow, ByVal dt As DataTable) As DataTable
        Dim dr As DataRow() = dt.Select("serviceNo = '" _
                                    & gvRow.Cells(1).Text & "'")
        If dr.Length > 0 Then
            dt.Rows.Remove(dr(0))
            dt.AcceptChanges()
        End If
        Return dt
    End Function
    Protected Sub CheckBox_CheckChanged(ByVal sender As Object, ByVal e As EventArgs)
        GetData()
        SetData()
        'BindWatchGrid()
        BindSecondaryGrid()

    End Sub
    Private Sub BindSecondaryGrid()
        Dim dt As DataTable = DirectCast(ViewState("SelectedRecords"), DataTable)
        gvSelected.DataSource = dt
        gvSelected.DataBind()
    End Sub
    Private Sub CreateAlphaPagings()
        lbTitle.Text = "<h3>Add Detached Firefighters</h3>"
        Dim dt As New DataTable()
        dt.Columns.Add("PageIndex")
        dt.Columns.Add("PageText")

        If Me.ViewState("Paging") Is Nothing Then
            For i As Integer = 65 To (65 + 25)
                Dim dr As DataRow = dt.NewRow()
                dr(0) = [Char].ConvertFromUtf32(i)
                dr(1) = [Char].ConvertFromUtf32(i)
                dt.Rows.Add(dr)
            Next

            Dim drNew As DataRow = dt.NewRow()
            drNew("PageIndex") = "All"
            drNew("PageText") = "All"
            dt.Rows.Add(drNew)

            Me.ViewState("Paging") = dt
        Else
            dt = DirectCast(Me.ViewState("Paging"), DataTable)
        End If

        DataList1.DataSource = dt
        DataList1.DataBind()

    End Sub
    Private Sub BindGrid(ByVal StartAlpha As String)
        Dim constr As String = ConfigurationManager.ConnectionStrings("ConString").ConnectionString
        Dim sql As String = ""

        If StartAlpha = "All" Then
            sql = "Select * from userProfilesTbl Order By serviceNo asc"
        Else
            sql = "Select  * from userProfilesTbl Where surname Like '" & StartAlpha & "%' Order By serviceNo asc "
        End If

        Dim da As New SqlDataAdapter(sql, constr)
        Dim dtSelect As New DataTable()
        da.Fill(dtSelect)

        gvAll.DataSource = dtSelect
        gvAll.DataBind()

    End Sub
    Protected Sub DataList1_ItemCommand(ByVal source As Object, ByVal e As DataListCommandEventArgs)
        Dim lbkbtnPaging As LinkButton = DirectCast(e.CommandSource, LinkButton)
        BindGrid(e.CommandArgument.ToString())
        Me.ViewState("SelectedText") = e.CommandArgument.ToString()
        CreateAlphaPagings()
    End Sub
    Protected Sub DataList1_ItemDataBound(ByVal sender As Object, ByVal e As DataListItemEventArgs)
        If e.Item.ItemType = ListItemType.AlternatingItem OrElse e.Item.ItemType = ListItemType.Item Then
            If Me.ViewState("SelectedText") IsNot Nothing Then
                Dim lbkbtnPaging As LinkButton = DirectCast(e.Item.FindControl("lnkbtnPaging"), LinkButton)
                If Me.ViewState("SelectedText").ToString() = lbkbtnPaging.Text Then
                    lbkbtnPaging.Enabled = False
                End If
            End If
        End If
    End Sub

3 个答案:

答案 0 :(得分:0)

添加TemplateField并使用<asp:HiddenField/>绑定UserId元素。

答案 1 :(得分:0)

您应该能够将要隐藏的列的宽度设置为0来实现此目的。

答案 2 :(得分:0)

感谢03usr和AVD的贡献。但是,通过CSS解决了它 这是

<style type="text/css">
    .hide
    {
        display:none;
    }
</style>

<asp:BoundField DataField = "UserId" HeaderText = "UserId" HtmlEncode = "false" ><ItemStyle CssClass="hide"/><HeaderStyle CssClass="hide" /></asp:BoundField >