Solved

Datagrid with Multiple Select Checkboxes

Posted on 2004-09-02
1
270 Views
Last Modified: 2011-09-20
Hi,

I have a datagrid with a list of items, and I want to be able to have a column that contains checkboxes and then a button below that postbacks. On postback, I want to be able to determine which rows were checked. How can I do this without a postback for each checkbox being checked?

Thanks!
0
Comment
Question by:tonypino123
1 Comment
 
LVL 17

Accepted Solution

by:
AerosSaga earned 500 total points
ID: 11964326
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="WebForm1.aspx.vb" Inherits="RadioButtonInDataGrid.WebForm1"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
      <HEAD>
            <title>WebForm1</title>
            <meta name="GENERATOR" content="Microsoft Visual Studio.NET 7.0">
            <meta name="CODE_LANGUAGE" content="Visual Basic 7.0">
            <meta name="vs_defaultClientScript" content="JavaScript">
            <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
      </HEAD>
      <body MS_POSITIONING="GridLayout">
            <form id="Form1" method="post" runat="server">
                  <asp:DataGrid id="DataGrid1" style="Z-INDEX: 101; LEFT: 8px; POSITION: absolute; TOP: 8px" runat="server" BorderColor="#CC9966" BorderStyle="None" BorderWidth="1px" BackColor="White" CellPadding="4" AutoGenerateColumns="False" Width="144px" Height="22px">
                        <SelectedItemStyle Font-Bold="True" ForeColor="#663399" BackColor="#FFCC66"></SelectedItemStyle>
                        <ItemStyle ForeColor="#330099" BackColor="White"></ItemStyle>
                        <HeaderStyle Font-Bold="True" ForeColor="#FFFFCC" BackColor="#990000"></HeaderStyle>
                        <FooterStyle ForeColor="#330099" BackColor="#FFFFCC"></FooterStyle>
                        <Columns>
                              <asp:TemplateColumn HeaderText="Select">
                                    <ItemTemplate>
                                          <asp:Label id="Label2" runat="server"></asp:Label>
                                    </ItemTemplate>
                              </asp:TemplateColumn>
                              <asp:BoundColumn DataField="lastname" HeaderText="Last Name"></asp:BoundColumn>
                        </Columns>
                        <PagerStyle HorizontalAlign="Center" ForeColor="#330099" BackColor="#FFFFCC"></PagerStyle>
                  </asp:DataGrid>
                  <asp:Label id="Label3" style="Z-INDEX: 103; LEFT: 222px; POSITION: absolute; TOP: 35px" runat="server" Width="184px"></asp:Label>
                  <asp:Button id="Button1" style="Z-INDEX: 102; LEFT: 218px; POSITION: absolute; TOP: 70px" runat="server" Text="Display Selected Value"></asp:Button>
            </form>
      </body>
</HTML>

----------------------------------------------------------------------------------------------------------------------

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If Not Page.IsPostBack Then
            BindGrid()
        End If
    End Sub

    Sub BindGrid()
        Dim connstr As String = "Integrated Security=SSPI;Initial Catalog=Northwind;Data Source=.\NetSDK"
        Dim cnn As SqlConnection
        Dim da As SqlDataAdapter
        Dim ds As New DataSet()
        cnn = New SqlConnection(connstr)
        da = New SqlDataAdapter("select * from employees", cnn)
        da.Fill(ds, "employees")
        DataGrid1.DataSource = ds
        DataGrid1.DataBind()
    End Sub

    Private Sub DataGrid1_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles DataGrid1.ItemDataBound
        If e.Item.ItemType = ListItemType.AlternatingItem Or e.Item.ItemType = ListItemType.Item Then
            Dim r As Label
            r = e.Item.FindControl("Label2")
            r.Text = "<input type=radio name='myradiogroup' value=" & e.Item.Cells(1).Text & ">"
        End If
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Label3.Text = Request.Form("myradiogroup")
        Dim i As DataGridItem
        For Each i In DataGrid1.Items
            If i.ItemType = ListItemType.AlternatingItem Or i.ItemType = ListItemType.Item Then
                Dim r As Label
                r = i.FindControl("Label2")
                If r.Text.IndexOf(Label3.Text) > 0 Then
                    r.Text = "<input type=radio name='myradiogroup' value=" & i.Cells(1).Text & " checked>"
                Else
                    r.Text = "<input type=radio name='myradiogroup' value=" & i.Cells(1).Text & ">"
                End If
            End If
        Next
    End Sub
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question