Dim x As DataGridItem
Dim i As Integer = 0
For Each x In DataGrid1.Items
If CType(x.FindControl("che"), CheckBox).Checked = True Then
i = i + 1
If i >= 2 Then
Response.Write("<script>alert('只能选一个');</script>")
Exit For
Exit Sub
response.end()
else
Response.Write(CType(x.FindControl("getvalue"), Label).Text)
end if
End If
Next
Dim i As Integer = 0
For Each x In DataGrid1.Items
If CType(x.FindControl("che"), CheckBox).Checked = True Then
i = i + 1
If i >= 2 Then
Response.Write("<script>alert('只能选一个');</script>")
Exit For
Exit Sub
response.end()
else
Response.Write(CType(x.FindControl("getvalue"), Label).Text)
end if
End If
Next
Dim i As Integer = 0
For Each x In DataGrid1.Items
If CType(x.FindControl("che"), CheckBox).Checked = True Then
i = i + 1
If i >= 2 Then
Response.Write("<script>alert('只能选一个');</script>")
Exit For
Exit Sub
end if
Next
For Each x In DataGrid1.Items
If CType(x.FindControl("che"), CheckBox).Checked = True Then
Response.Write(CType(x.FindControl("getvalue"), Label).Text)
End If
Next
如果要实现单选,可以将模版改为radiobutton,或变相使用selectitem。
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>WOLF.net视频点播系统</title>
<meta content="Microsoft Visual Studio .NET 7.0" name="GENERATOR">
<meta content="Visual Basic 7.0" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
</HEAD>
<body>
<FONT face="宋体">
<FORM id="Form1" method="post" runat="server">
<FONT face="宋体"></FONT>
<TABLE id="Table1" style="WIDTH: 524px; HEIGHT: 415px" cellSpacing="0" cellPadding="0" width="524" align="center" border="0">
<TR>
<TD style="HEIGHT: 24px" align="middle"><asp:label id="Label1" runat="server" ForeColor="#C00000" Width="118px">菜单管理——</asp:label><asp:label id="Label2" runat="server" ForeColor="#C00000"></asp:label></TD>
</TR>
<TR>
<TD style="HEIGHT: 45px"><FONT face="宋体"> <INPUT style="WIDTH: 77px; COLOR: #ffffff; HEIGHT: 24px; BACKGROUND-COLOR: #cc0000" onclick="jacascript:history.go(-1)" type="button" value="返回" runat="server"> </FONT><FONT face="宋体">
<asp:button id="Button2" runat="server" Height="25px" ForeColor="White" BackColor="ActiveCaption" Width="80px" Text="增加"></asp:button>
<asp:button id="Button3" runat="server" Height="25px" ForeColor="White" BackColor="ActiveCaption" Width="80px" Text="修改"></asp:button>
<asp:button id="Button4" runat="server" Height="25px" ForeColor="White" BackColor="ActiveCaption" Width="80px" Text="删除"></asp:button></FONT></TD>
</TR>
<TR>
<TD vAlign="top">
<asp:datagrid id="DataGrid1" runat="server" Width="523px" BackColor="White" Height="20px" AutoGenerateColumns="False" CellPadding="4" BorderWidth="1px" BorderColor="#CC9966" AllowPaging="True" Font-Size="X-Small" BorderStyle="None" PageSize="5">
<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>
<ItemTemplate>
<asp:CheckBox id="che" runat="server"></asp:CheckBox>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList id="DropDownList1" runat="server" Width="96px" AutoPostBack="True">
<asp:ListItem Value="2">2</asp:ListItem>
<asp:ListItem Value="1">1</asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="二级菜单">
<ItemTemplate>
<asp:label id="getvalue" runat="server" text='<%# DataBinder.Eval(Container,"DataItem.二级菜单")%>'>
</asp:label>
</ItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn DataField="顺序" HeaderText="顺序"></asp:BoundColumn>
</Columns>
<PagerStyle HorizontalAlign="Center" ForeColor="#330099" BackColor="#FFFFCC" Mode="NumericPages"></PagerStyle>
</asp:datagrid></TD>
</TR>
</TABLE>
</FORM>
</FONT>
</body>
</HTML>.vb
Imports System.Data.SqlClient
Imports System.Data
Imports SystemPublic Class zimenu
Inherits System.Web.UI.Page
Protected WithEvents Button4 As System.Web.UI.WebControls.Button
Protected WithEvents Button3 As System.Web.UI.WebControls.Button
Protected WithEvents Button2 As System.Web.UI.WebControls.Button
Protected WithEvents Label1 As System.Web.UI.WebControls.Label
Protected WithEvents Label2 As System.Web.UI.WebControls.Label
Protected WithEvents DataGrid1 As System.Web.UI.WebControls.DataGrid
Protected WithEvents Button1 As System.Web.UI.HtmlControls.HtmlInputButton
Protected WithEvents Form1 As System.Web.UI.HtmlControls.HtmlForm#Region " Web 窗体设计器生成的代码 " '该调用是 Web 窗体设计器所必需的。
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() End Sub Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: 此方法调用是 Web 窗体设计器所必需的
'不要使用代码编辑器修改它。
InitializeComponent()
End Sub#End Region Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim id As String = Request.QueryString("zhucai")
Label2.Text = id If Not IsPostBack Then
'判断是否有权限访问
If Session("yes") <> "yes" Then
Response.Redirect("error.aspx?number=1")
End If
'连接数据库信息
Dim str As String = ConfigurationSettings.AppSettings("conn") Dim conn As SqlConnection
conn = New SqlConnection()
conn.ConnectionString = str
conn.Open() '结束信息
Dim da As SqlDataAdapter
Dim ds As New DataSet()
Dim dv As DataTable da = New SqlDataAdapter("select distinct ci as 二级菜单,shunxu1 as 顺序 from menu where zhu='" & id & "'", conn)
da.Fill(ds, "table1")
dv = ds.Tables("table1")
DataGrid1.DataSource = dv
DataGrid1.DataBind()
End If '在此处放置初始化页的用户代码
End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) End Sub
Private Sub DataGrid1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DataGrid1.SelectedIndexChanged End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click End Sub Private Sub DataGrid1_PageIndexChanged(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridPageChangedEventArgs) Handles DataGrid1.PageIndexChanged
'连接数据库信息 Dim id As String = Request.QueryString("zhucai") Dim str As String = ConfigurationSettings.AppSettings("conn")
Dim conn As SqlConnection
conn = New SqlConnection()
conn.ConnectionString = str
conn.Open() '结束信息
Dim da As SqlDataAdapter
Dim ds As New DataSet()
Dim dv As DataTable da = New SqlDataAdapter("select distinct ci as 二级菜单,shunxu1 as 顺序 from menu where zhu='" & id & "'", conn)
da.Fill(ds, "table1")
dv = ds.Tables("table1")
DataGrid1.DataSource = dv
DataGrid1.CurrentPageIndex = e.NewPageIndex
DataGrid1.DataBind()
End Sub Private Sub Button1_ServerClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.ServerClick End Sub
End Class