Har en gridview där jag vill kunna välja flera rader och göra något med dom raderna, t.ex ta bort dom ur databasen. Har lagt till en checkbox men vet inte hur jag skall få det att fungera, checkboxes fungerar ju så konstigt i asp.net..
hur kopplar man ett värde och gemensamt namn till checkboxarna?
verkar ju inte gå men det borde ju gå att lösa på någotvis.
har kollat runt på olika ställen men inte fått något utav exemplen att fungera med min kod.
tacksam för hjälp!
[Default.asp]
<%@ Page Language="VB" AutoEventWireup="true" CodeFile="Default.aspx.vb" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<ajaxToolkit:ToolkitScriptManager ID="ScriptManager1" runat="server" />
<div>
<asp:TextBox ID="Search_title" runat="server"></asp:TextBox>
<asp:Button ID="Search_submit" runat="server" Text="Sök" />
<asp:GridView id="MySQLDataGrid" runat="server" AutoGenerateColumns="true" AllowPaging="True" PageSize="4" AllowSorting="True">
<Columns>
<asp:TemplateField >
<HeaderStyle HorizontalAlign="left" />
<HeaderTemplate >
<asp:CheckBox id="chkSelectAll" ToolTip="Click here to select/deselect all rows" runat="server" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="chkSelect" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Button ID="btnDelete" runat="server" OnClick="btnDelete_Click" Text="Delete Selected Records" />
</div>
</form>
</body>
</html>
Default.aspx.vb
Imports System.Data
Imports MySql.Data.MySqlClient
Partial Class _Default
Inherits System.Web.UI.Page
Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
End Sub
Protected Sub Search_submit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Search_submit.Click
bindgrid()
End Sub
Private Sub bindgrid()
' Define data objects
Dim conn As MySqlConnection
Dim adapter As MySqlDataAdapter
Dim dataset As New DataSet
If ViewState("MySQLDataSet") Is Nothing Then
' Read the connection string from Web.config
Dim strsearch As String = Search_title.Text
conn = New MySqlConnection("server=localhost; database=jmdb; pooling=false;")
Dim strSQL As String = "SELECT * FROM movies where title like '%" & strsearch & "%'"
' Create adapter
adapter = New MySqlDataAdapter(strSQL, conn)
' Fill the DataSet
adapter.Fill(dataset, "movies")
' Store the DataSet in view state
ViewState("MySQLDataSet") = dataset
Else
dataset = ViewState("MySQLDataSet")
End If
' Prepare the sort expression using the gridSortDirection and
' gridSortExpression properties
Dim sortExpression As String
If gridSortDirection = SortDirection.Ascending Then
sortExpression = gridSortExpression & " ASC"
Else
sortExpression = gridSortExpression & " DESC"
End If
' Sort the data
dataset.Tables("movies").DefaultView.Sort = sortExpression
' Bind the grid to the DataSet
MySQLDataGrid.DataSource = dataset.Tables("Movies").DefaultView
MySQLDataGrid.DataBind()
End Sub
Protected Sub MySQLDataGrid_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles MySQLDataGrid.PageIndexChanging
' Retrieve the new page index
Dim newPageIndex As Integer = e.NewPageIndex
' Set the new page index of the GridView
MySQLDataGrid.PageIndex = newPageIndex
' Bind the grid to its data source again to update its contents
bindgrid()
End Sub
Protected Sub MySQLDataGrid_Sorting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSortEventArgs) Handles MySQLDataGrid.Sorting
' Retrieve the name of the clicked column (sort expression)
Dim sortExpression As String = e.SortExpression
' Decide and save the new sort direction
If (sortExpression = gridSortExpression) Then
If gridSortDirection = SortDirection.Ascending Then
gridSortDirection = SortDirection.Descending
Else
gridSortDirection = SortDirection.Ascending
End If
Else
gridSortDirection = WebControls.SortDirection.Ascending
End If
' Save the new sort expression
gridSortExpression = sortExpression
' Rebind the grid to its data source
bindgrid()
End Sub
Private Property gridSortDirection()
Get
' Initial state is Ascending
If (ViewState("GridSortDirection") Is Nothing) Then
ViewState("GridSortDirection") = SortDirection.Ascending
End If
' Return the state
Return ViewState("GridSortDirection")
End Get
Set(ByVal value)
ViewState("GridSortDirection") = value
End Set
End Property
Private Property gridSortExpression()
Get
' Initial sort expression is DepartmentID
If (ViewState("GridSortExpression") Is Nothing) Then
ViewState("GridSortExpression") = "title"
End If
' Return the sort expression
Return ViewState("GridSortExpression")
End Get
Set(ByVal value)
ViewState("GridSortExpression") = value
End Set
End Property
End Class