[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Datagrid with Multiple Select Checkboxes

Posted on 2004-09-02
1
Medium Priority
?
275 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 1500 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses

834 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