look into http://www.metabuilders.com/Tools/RowSelectorColumn.aspxand DataGrid Girl's explanations:http://www.datagridgirl.com/rowselector.aspxotherwise, you can do<asp:CheckBox id="chkSelectAll" runat="server" AutoPostBack="true" OnCheckedChanged="Check_Clicked" ></asp:CheckBox> void Check_Clicked(Object sender, EventArgs e) { bool b = ((CheckBox)sender).Checked; foreach (DataGridItem dgi in YourDataGrid1.Items) { CheckBox cb = (CheckBox)dgi.FindControl("chkDelete"); cb.Checked = b; } }or you can always do it on the client side with javascript
string test = "("; foreach (DataGridItem CheckBoxItem in this.UserList.Items) { if (((HtmlInputCheckBox)CheckBoxItem.FindControl("chkSelection")).Checked) { ((HtmlInputCheckBox)CheckBoxItem.FindControl("chkSelection")).Checked = false; test +=((HtmlInputCheckBox)CheckBoxItem.FindControl("chkSelection")).Value+","; } } test += "0)"; con.opensql(); string sql ="delete from admin where id in"+test; SqlCommand command1=new SqlCommand(sql,con.conn); command1.ExecuteNonQuery(); con.closesql();
protected System.Web.UI.WebControls.DataGrid UserList; protected System.Web.UI.HtmlControls.HtmlInputCheckBox chkSelection; string test = "("; foreach (DataGridItem CheckBoxItem in this.UserList.Items) { if (((HtmlInputCheckBox)CheckBoxItem.FindControl("chkSelection")).Checked) { ((HtmlInputCheckBox)CheckBoxItem.FindControl("chkSelection")).Checked = false; test +=((HtmlInputCheckBox)CheckBoxItem.FindControl("chkSelection")).Value+","; } } test += "0)"; con.opensql(); string sql ="delete from admin where id in"+test; SqlCommand command1=new SqlCommand(sql,con.conn); command1.ExecuteNonQuery(); con.closesql();
Function checkitem() As ArrayList Dim ditem As DataGridItem Dim rdo As CheckBox
For Each ditem In DataGrid1.Items rdo = ditem.FindControl("rd1") If rdo.Checked = True Then checkitem.add(ditem.ItemIndex) End If Next
End Function然后把CHECKITEM中的值取出来就可以了
Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click Dim sqlstr As String Dim rscmd As OleDbCommand Dim rolecodes As ArrayList Dim rolecode Dim ischecked As Integer ischecked = 0 Dim item As DataGridItem Dim ch As CheckBox Dim i As Integer rolecodes = New ArrayList For Each item In DataGrid1.Items ch = item.FindControl("chck") If ch.Checked = True Then ischecked = 1 rolecodes.Add(DataGrid1.DataKeys(item.ItemIndex)) End If Next If ischecked = 1 Then For i = 0 To rolecodes.Count - 1 rolecode = rolecodes(i) sqlstr = "delete from roles where code='" & rolecode & "'" rscmd = New OleDbCommand(sqlstr, conn) rscmd.ExecuteNonQuery() Next datagrid1bind() End If End Sub
<asp:TemplateColumn> <HeaderTemplate> <asp:CheckBox id="chkSelectAll" runat="server" AutoPostBack="True" OnCheckedChanged="check1"></asp:CheckBox> </HeaderTemplate> ....................... Public Sub check1(ByVal s As Object, ByVal e As EventArgs) Dim item As DataGridItem Dim ch As CheckBox For Each item In DataGrid1.Items ch = item.FindControl("chkDelete") If ch.Checked = False Then ch.Checked = True End If Next End Sub
在页面上: <Columns> <asp:TemplateColumn HeaderText="全选"> <HeaderTemplate> <asp:CheckBox id="chkSelectAll" runat="server" Height="5px" OnCheckedChanged="chkSelectAll_Click" Runat="server" EnableViewState="True"></asp:CheckBox> </HeaderTemplate> <HeaderStyle Width="20px" Wrap="False" Height="7px"></HeaderStyle> <ItemTemplate> <asp:CheckBox id="chkDelete" runat="server" Height="5px" Runat="server" EnableViewState="True"></asp:CheckBox> </ItemTemplate> <ItemStyle Wrap="False" Height="7px"></ItemStyle> </asp:TemplateColumn>在隐藏类代码: Public Class department Inherits System.Web.UI.Page Public Sub chkSelectAll_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Dim item As DataGridItem Dim selection As CheckBox For Each item In dgdDepartment.Items selection = CType(item.FindControl("chkDelete"), CheckBox) selection.Checked = True 'If Not selection Is Nothing Then ' selection.Checked = True 'End If Next End Sub End Class但发现无论如何点击标题行的chkSelectAll,都不执行Sub chkCheckAll_Click过程 请教各位是不是在哪里写错了,代码没有错,但就是没有执行。
我用的是radiobutton 和楼主同样的问题也没解决 关注
你按我的方法写不行吗?我是测试过可以的啊! <asp:CheckBox id="chkSelectAll" runat="server" Height="5px" OnCheckedChanged="chkSelectAll_Click" Runat="server" AutoPostBack="True" > 注意加上AutoPostBack="True" ........................... Public Sub chkSelectAll_Click(s As System.Object, e As EventArgs)selection = item.FindControl("chkDelete") selection.Checked = True.............. 改成这样是绝对可以的!
http://www.metabuilders.com/Tools/RowSelectorColumn.aspxand DataGrid Girl's explanations:http://www.datagridgirl.com/rowselector.aspxotherwise, you can do<asp:CheckBox id="chkSelectAll" runat="server" AutoPostBack="true"
OnCheckedChanged="Check_Clicked"
></asp:CheckBox>
void Check_Clicked(Object sender, EventArgs e)
{
bool b = ((CheckBox)sender).Checked;
foreach (DataGridItem dgi in YourDataGrid1.Items)
{
CheckBox cb = (CheckBox)dgi.FindControl("chkDelete");
cb.Checked = b;
}
}or you can always do it on the client side with javascript
<ItemStyle HorizontalAlign="Center"></ItemStyle>
<ItemTemplate>
<asp:CheckBox id=CheckBox1 runat="server" checked='<%# DataBinder.Eval(Container.DataItem, "XXXXXX") %>'>
</asp:CheckBox>
</ItemTemplate>
</asp:TemplateColumn>
然后在CheckedChanged事件获得:
(CheckBox)e.Item.FindControl("XXXXXX")
foreach (DataGridItem CheckBoxItem in this.UserList.Items)
{
if (((HtmlInputCheckBox)CheckBoxItem.FindControl("chkSelection")).Checked)
{
((HtmlInputCheckBox)CheckBoxItem.FindControl("chkSelection")).Checked = false;
test +=((HtmlInputCheckBox)CheckBoxItem.FindControl("chkSelection")).Value+",";
}
}
test += "0)";
con.opensql();
string sql ="delete from admin where id in"+test;
SqlCommand command1=new SqlCommand(sql,con.conn);
command1.ExecuteNonQuery();
con.closesql();
protected System.Web.UI.HtmlControls.HtmlInputCheckBox chkSelection;
string test = "(";
foreach (DataGridItem CheckBoxItem in this.UserList.Items)
{
if (((HtmlInputCheckBox)CheckBoxItem.FindControl("chkSelection")).Checked)
{
((HtmlInputCheckBox)CheckBoxItem.FindControl("chkSelection")).Checked = false;
test +=((HtmlInputCheckBox)CheckBoxItem.FindControl("chkSelection")).Value+",";
}
}
test += "0)";
con.opensql();
string sql ="delete from admin where id in"+test;
SqlCommand command1=new SqlCommand(sql,con.conn);
command1.ExecuteNonQuery();
con.closesql();
Dim ditem As DataGridItem
Dim rdo As CheckBox
For Each ditem In DataGrid1.Items
rdo = ditem.FindControl("rd1")
If rdo.Checked = True Then
checkitem.add(ditem.ItemIndex)
End If
Next
End Function然后把CHECKITEM中的值取出来就可以了
Dim sqlstr As String
Dim rscmd As OleDbCommand
Dim rolecodes As ArrayList
Dim rolecode
Dim ischecked As Integer
ischecked = 0
Dim item As DataGridItem
Dim ch As CheckBox
Dim i As Integer
rolecodes = New ArrayList
For Each item In DataGrid1.Items
ch = item.FindControl("chck")
If ch.Checked = True Then
ischecked = 1
rolecodes.Add(DataGrid1.DataKeys(item.ItemIndex))
End If
Next
If ischecked = 1 Then
For i = 0 To rolecodes.Count - 1
rolecode = rolecodes(i)
sqlstr = "delete from roles where code='" & rolecode & "'"
rscmd = New OleDbCommand(sqlstr, conn)
rscmd.ExecuteNonQuery() Next
datagrid1bind()
End If
End Sub
<HeaderTemplate>
<asp:CheckBox id="chkSelectAll" runat="server" AutoPostBack="True" OnCheckedChanged="check1"></asp:CheckBox>
</HeaderTemplate>
.......................
Public Sub check1(ByVal s As Object, ByVal e As EventArgs)
Dim item As DataGridItem
Dim ch As CheckBox For Each item In DataGrid1.Items
ch = item.FindControl("chkDelete")
If ch.Checked = False Then
ch.Checked = True
End If
Next End Sub
{
if((e.Item.ItemType == ListItemType.Header))
{
LinkButton lbAdd=new LinkButton();
lbAdd=e.Item.FindControl("btnAdd") as LinkButton ;
lbAdd.Attributes.Add("onclick","javascript:return confirm('真的要添加记录吗?')");
}修改一下就可以
}
至于怎么得到哪一行选中我想可以使用遍历的方法在服务器上实现!
前台
<asp:TemplateColumn>
<ItemTemplate><INPUT id="Checkbox1" value="<%#DataBinder.Eval(Container.DataItem,"LOC_LOC")%>" type="checkbox" name="Checkbox1" >
</ItemTemplate>
</asp:TemplateColumn> </Columns>
后台:
string []arrayCheckbox = this.Request["Checkbox1"].Trim().Split(',');
<Columns>
<asp:TemplateColumn HeaderText="全选">
<HeaderTemplate>
<asp:CheckBox id="chkSelectAll" runat="server" Height="5px" OnCheckedChanged="chkSelectAll_Click"
Runat="server" EnableViewState="True"></asp:CheckBox>
</HeaderTemplate>
<HeaderStyle Width="20px" Wrap="False" Height="7px"></HeaderStyle>
<ItemTemplate>
<asp:CheckBox id="chkDelete" runat="server" Height="5px" Runat="server" EnableViewState="True"></asp:CheckBox>
</ItemTemplate>
<ItemStyle Wrap="False" Height="7px"></ItemStyle>
</asp:TemplateColumn>在隐藏类代码:
Public Class department
Inherits System.Web.UI.Page Public Sub chkSelectAll_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim item As DataGridItem
Dim selection As CheckBox
For Each item In dgdDepartment.Items
selection = CType(item.FindControl("chkDelete"), CheckBox)
selection.Checked = True
'If Not selection Is Nothing Then
' selection.Checked = True
'End If
Next
End Sub
End Class但发现无论如何点击标题行的chkSelectAll,都不执行Sub chkCheckAll_Click过程
请教各位是不是在哪里写错了,代码没有错,但就是没有执行。
关注
<asp:CheckBox id="chkSelectAll" runat="server" Height="5px" OnCheckedChanged="chkSelectAll_Click"
Runat="server" AutoPostBack="True" >
注意加上AutoPostBack="True"
...........................
Public Sub chkSelectAll_Click(s As System.Object, e As EventArgs)selection = item.FindControl("chkDelete")
selection.Checked = True..............
改成这样是绝对可以的!