If you are not making a Column for the checkbox in your grid then the databind will create one for you but I think that you will not be able to select or unselectit or it will be read only.
Imports System.IO
Partial Class CheckWithServerSidePostback
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
Dim dirInfo As New DirectoryInfo(Request.PhysicalApplicationPath)
FileList.DataSource = dirInfo.GetFiles()
FileList.DataBind()
End If
End Sub
Protected Sub DeleteButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles DeleteButton.Click
Summary.Text = "The following file would have been deleted:<ul>"Dim currentRowsFilePath As String'Enumerate the GridViewRowsFor index As Integer = 0 To FileList.Rows.Count - 1
'Programmatically access the CheckBox from the TemplateFieldDim cb As CheckBox = CType(FileList.Rows(index).FindControl("RowLevelCheckBox"), CheckBox)
'If it's checked, delete it...If cb.Checked Then
currentRowsFilePath = FileList.DataKeys(index).Value
Summary.Text &= String.Concat("<li>", currentRowsFilePath, "</li>")
End If
Next
Summary.Text &= "</ul>"End Sub
Protected Sub CheckAll_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles CheckAll.Click
'Enumerate each GridViewRowFor Each gvr As GridViewRow In FileList.Rows
'Programmatically access the CheckBox from the TemplateFieldDim cb As CheckBox = CType(gvr.FindControl("RowLevelCheckBox"), CheckBox)
'Check it!
cb.Checked = True
Next
End Sub
Protected Sub UncheckAll_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles UncheckAll.Click
'Enumerate each GridViewRowFor Each gvr As GridViewRow In FileList.Rows
'Programmatically access the CheckBox from the TemplateFieldDim cb As CheckBox = CType(gvr.FindControl("RowLevelCheckBox"), CheckBox)
'Uncheck it!
cb.Checked = False
Next
End Sub
End Class
Netforum
Member
7 Points
60 Posts
retreiving checkbox control value from database
Dec 14, 2007 01:21 PM|LINK
i have already added checkbox in datagrid.
in my application i want that paricular check box should be
bydefault checked and unchecked depending upon values of databse suppose id.
when id=1, checkbox should be checked and when id=0,checkbox should be bydefault unchecked.
i dont know how to check and uncheck bydefault value.Please help. Thank you.
nikki_doer_o...
Contributor
6863 Points
1097 Posts
Re: retreiving checkbox control value from database
Dec 14, 2007 01:27 PM|LINK
I thought if you data bind it, it does that for you? Was that not working?
"If we learn from our mistakes, I should be brilliant by now."
MCTS - .NET Framework 2.0: Web Applications
Netforum
Member
7 Points
60 Posts
Re: retreiving checkbox control value from database
Dec 14, 2007 01:38 PM|LINK
I dont know how can I assign the the property to the checkbox. Can u please give me the code to load the page and the stored procedure to be used.
naveed_khan
Member
658 Points
151 Posts
Re: retreiving checkbox control value from database
Dec 14, 2007 01:49 PM|LINK
hi,
If you are not making a Column for the checkbox in your grid then the databind will create one for you but I think that you will not be able to select or unselectit or it will be read only.
here is a code.
'-------------------------HTML CODE------------------------------------------
<asp:GridView ID="FileList" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None" AutoGenerateColumns="False" DataKeyNames="FullName"> <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" /> <RowStyle BackColor="#FFFBD6" ForeColor="#333333" /> <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" /> <PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" /> <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" /> <AlternatingRowStyle BackColor="White" /> <Columns> <asp:TemplateField> <ItemTemplate> <asp:CheckBox runat="server" ID="RowLevelCheckBox" /> </ItemTemplate> </asp:TemplateField> <asp:BoundField DataField="Name" HeaderText="Name" /> <asp:BoundField DataField="CreationTime" HeaderText="Created On"> <ItemStyle HorizontalAlign="Right" /> </asp:BoundField> <asp:BoundField DataField="Length" DataFormatString="{0:N0}" HeaderText="File Size" HtmlEncode="False"> <ItemStyle HorizontalAlign="Right" /> </asp:BoundField> </Columns> </asp:GridView>Also check out the below given link
http://aspnet.4guysfromrolla.com/articles/052406-1.aspx
Hope this will help you
N@vEeD Kh@N
Karenros
Participant
1361 Points
1177 Posts
Re: retreiving checkbox control value from database
Dec 14, 2007 02:16 PM|LINK
<asp:CheckBox runat="server" ID="RowLevelCheckBox" Checked = '<%#DataBinder.Eval(Container,"DataItem.IsFundDefault")%>'/>
Hope this helps.
Regards
Karen
Netforum
Member
7 Points
60 Posts
Re: retreiving checkbox control value from database
Dec 15, 2007 01:44 AM|LINK
I want to get the id=0 or 1 from the database. I didnt understand the above program. Can you please explain me. Thank you.
vasanth.kuma...
Contributor
5324 Points
1041 Posts
Re: retreiving checkbox control value from database
Dec 15, 2007 03:21 AM|LINK
Hi,
try the below one...
<asp:GridView ID="GridView1" runat="server" Height="233px" Width="943px" AutoGenerateColumns="true">
<Columns>
<asp:CheckBoxField DataField="IsMale" HeaderText="Is Male" ReadOnly="false"/>
</Columns>
</asp:GridView>
Software Engineer.
abhimjoshi
Member
198 Points
38 Posts
Re: retreiving checkbox control value from database
Dec 15, 2007 03:40 AM|LINK
Hi..
Very simple solution.
You can set checkbox like this :
Take checkbox control insted of <asp:CheckBoxField> in Template code.
You can use this code for your solution.
<Columns>
<asp:TemplateField HeaderText="Active">
<HeaderStyle HorizontalAlign="center" />
<ItemStyle HorizontalAlign="center" />
<ItemTemplate>
<asp:CheckBox ID="chActive" runat="server" Checked='<%# bool.Parse(Eval("id").ToString()) %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
That's it !
Hope you will like it.
Don't forget to 'Mark as an answer' if this post works for you.
Abhi
Visit : Find more in Asp.Net
sameer_khanj...
Star
7504 Points
1466 Posts
Re: retreiving checkbox control value from database
Dec 15, 2007 03:53 AM|LINK
by default make a checkbox checked and put in the item template and remove any bind method from the that check box.
then declare your datatable or dataset top of the page and bing grid on page load, under (!IsPostBack)
public partial class admin_restaurantmanageredit : System.Web.UI.Page
{
DataTable dtContacts = new DataTable("Item");
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
dtContacts=clsREstaurant.FillDataTable();
dgContacts.DataSource = dtContacts.DefaultView;
dgContacts.DataBind();
}
}
}
then use this code for data grid if u are using Grid view then work on row level
protected void dgContacts_OnItemDataBound(object sender, DataGridItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
((CheckBox)e.Item.Cells[5].FindControl("chkMain")).Checked = Convert.ToBoolean(dtContacts.Rows[e.Item.ItemIndex]["contact"].ToString());
}
}
sameer.khanjit@gmail.com
View Blog
Click "Mark as Answer" on the post that helped you.
arpan.shah
Member
52 Points
14 Posts
Re: retreiving checkbox control value from database
Dec 15, 2007 08:06 AM|LINK
Here is very simple solution for you.
Just Create one grid with the checkbox column.
<
div> <asp:GridView ID="FileList" runat="server" CellPadding="4" AutoGenerateColumns="False" > <Columns> <asp:TemplateField><ItemTemplate>
<asp:CheckBox runat="server" ID="RowLevelCheckBox" Checked='<%# Bind("IsRFIDEnabled")%>' /> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> </div>(Bind the appropriate data source and it works)
For your information i have created one sample table with the field "IsRFIDEnabled".
Checked='<%# Bind("IsRFIDEnabled")%>'
Above statement will work for you.
Please check it out and let me know still you need anything more than this.