Use data in repeater when Checkbox is check in ASP.net - asp.net

I have a repeater for showing my data . this repeater showing 2 field that one of feild is checkBox Control and other is a lable.
NOW , how can I understand text of lable when the checkBox is Checked?
I want to see text of lable in evry row that the CheckBoxes is checksd.
how do I do?
I use LINQtoSQL for get and set data from database

On postback, you need to loop through every row of your repeater, and grab out the checkbox control. Then you can access it's .Checked and .Text properties. If it's .Checked, then add it to a list or array. I can elaborate if needed..

Page...
<asp:CheckBox ID="chkBoxID" runat="server" OnCommand="doSomething_Checked" CommandArgument="<%# Some Binding Information%>"
CommandName="NameForArgument">
</asp:CheckBox>
Code Behind...
protected void doSomething_Checked(object sender, CommandEventArgs e) {
CheckBox ctrl = (CheckBox)sender;
RepeaterItem rpItem = ctrl.NamingContainer as RepeaterItem;
if (rpItem != null) {
CheckBox chkBox = (LinkButton)rpItem.FindControl("chkBoxID");
chkBox.DoSomethingHere...
}
}

<asp:Repeater ID="rptX" runat="server">
<ItemTemplate>
<asp:Label ID="lblX" runat="server" Visible='<%# Eval("IsChecked") %>' />
<asp:CheckBox ID="chkX" runat="server" Checked='<%# Eval("IsChecked") %>' />
</ItemTemplate>
</asp:Repeater>
And code behind when you assign your data
rptX.DataSource = SomeIEnumerableFromLinq; // which has a bool field called IsChecked
rptX.DataBind();

Related

how to bind a value of datasource to a checkbox in datalist with Eval()

I have a datalist in my asp.net page. I bind a datasource to it in codebehind
and I have a checkbox in this datalist.
var n = from gi in DataContext.Context.GalleryImages
join g in DataContext.Context.Galleries
on gi.GalleryID equals g.GalleryID
where g.UserID == UserID && gi.GalleryID==GalleryID
select new
{
GalleryID = g.GalleryID,
ImageDescription = gi.ImageDescription,
GalleryName = g.GalleryName,
ImageFileName = gi.ImageFileName,
IsAlbumImage = gi.IsAlbumImage,
ImageID=gi.ImageID
};
dlGalleryList.DataSource = n;
dlGalleryList.DataBind();
When the "IsAlbumImage " is true the checkbox should be checked.
How can I bind this property to the checkbox?
It should be bind like:
<ItemTemplate>
<asp:CheckBox id="MyCheckBox" runat="server" Checked='<%#Eval("IsAlbumImage") %>' />
</ItemTemplate>
Actually you have to ways to bind checkbox in a datalist
1- (recommended) Binding it directly from the ASP code using the Bind or Eval
<ItemTemplate>
<asp:CheckBox id="MyCheckBox" runat="server" Checked='<%#Eval("IsAlbumImage") %>' />
</ItemTemplate>
2- Binding it on the ItemDataBound Event
First you will add the event handler to your datalist control, and adds the Boolean value to a datakey to be used in itemdatabound event
<asp:DataList ID = "DataList1" OnItemDataBound="DataListItemEventHandler" DataKeys = "IsAlbumImage"/>
Then you add the C# code that bind this
protected void DataListItemEventHandler(object sender, DataListItemEventArgs e)
{
CheckBox checkbx = new CheckBox();
checkbx = (CheckBox)e.Item.FindControl("MyCheckBox");
checkbx.Checked = (bool) DataList1.DataKeys(e.Item.ItemIndex)("IsAlbumImage");
}
Like this:
<asp:CheckBox
ID="check"
runat="server"
Checked='<%# Eval("column_name").ToString().Equals("1") %>'
/>

Lost Focus Events for a Control Within GridView

I have multiple textboxes and dropdown lists within my GridView. For one particular textbox I need trigger a server event which gets data from the database and fills it in other columns of the Grid. Is there a simple way to do it or a slightly complicated way as detailed here
I have no problems implementing the above method or thinking of a work around but then thought that there is Cell Lost Focus in a grid control surprises me a little. Am I missing something ? Any help on this would appreciated.
You can set AutoPostBack to true and handle it's TextChanged event.
<asp:GridView ID="GridView1" runat="server" EmptyDataText="It's Empty.">
<Columns>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:TextBox ID="txtName"
runat="server"
Text='<%#Eval("Name") %>'
AutoPostBack="true"
OnTextChanged="NameChanged" >
</asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</GridView>
in codebehind:
protected void NameChanged(Object sender, EventArgs e)
{
var txtName = (TextBox) sender;
var row = (GridViewRow) txtName.NamingContainer;
// you could find other controls in this GridViewRow via
// row.FindControl("ControlID") in case of a TemplateField or
// row.Cells[0].Text (0 = index of column) in case of a BoundField
}

Findcontrol in listview itemtemplate

I have the next code in itemtemplate:
<asp:ListView ID="ListView1" runat="server">
<ItemTemplate>
<asp:HiddenField Value='<%# checkCatName(Eval("CatName")) %>' runat="server" />
........
<asp:Label runat="server" id="lblBla" Visible="false"> ... </asp:Label>
</ItemTemplate>
</asp:ListView>
Code Behind (C#):
public void checkCatName(object CatName)
{
Label bla = (Label)ListView1.FindControl("lblBla");
if (CatName.ToString() == "test1")
bla.Visible = true;
return CatName.ToString();
}
I get null - like the page dont find the "bla" label.
Where i'm wrong ?
to get item which is in the listview or a repeater, you will need to go through items in this view and then find control (hidden field).
Page will not be able to find that control directly.
Hope that helps.
If the listview has an itemdatabound event you can use it to find the control and do what ever you need with it. The following code is assuming you have a hidden field in your listview item template with the id="myhiddenfield"
//this goes inside your listview's itemdatabound event
HiddenField myhiddenfield = new HiddenField();
myhiddenfield = (HiddenField)e.Item.FindControl("myhiddenfield");
//get or set hidden field value here.
int myID = Convert.ToInt32(myhiddenfield.Value);

help with asp.net gridview/checkboxfield binding

my stored procedure is returning either 1 or 0 depending on the value of another field, however, my checkboxfield in the gridview I've created to display the data returned from the stored proc, is crashing saying that the value set to the checkboxfield is a string, not boolean. How can I take the field returned as 1 or 0 and convert it to boolean so my checkbox can bind to this value for checking/unchecking?
aspx
<asp:TemplateField SortExpression="TragamonedaActiva" HeaderText="Trag. Activa">
<ItemTemplate>
<asp:CheckBox ID="CK2" runat="server" EnableViewState="true"
Checked='<%# Convert.ToBoolean(Eval("TragamonedaActiva")) %>'/>
</ItemTemplate>
</asp:TemplateField>
.cs
isChecked = ((CheckBox)gvReport.Rows[rowNo].FindControl("CK1")).Checked;
I belive you want something like this - this works for a DataGrid:
<asp:CheckBox ...
Checked='<%# Convert.ToBoolean( DataBinder.Eval(Container.DataItem, "is_checked"))%>'
/>
Create a template field with your checkbox in the datagrid.
// In your aspx page
<asp:CheckBox ID="yourCheckBox" runat="server" OnDataBinding="yourCheckBox_DataBinding" />
// In your codebehind .cs file
protected void yourCheckBox_DataBinding(object sender, System.EventArgs e)
{
CheckBox chk = (CheckBox)(sender);
chk.Checked = Convert.ToBoolean(Eval("YourFieldName"));
}

give the ID as a custom attribute to checkbox in a grid for manual update

I like to update just the value of my checkbox in a asp grid.
so i thought i bind the id to the checkbox ( but how?) and fire an update in code behind by clicking the checkbox.
but how can I get the ID, and where I have to bind this id to get it in code behind in the event?
Thanks
Here is what I've managed to do. (Be aware there should be an easier way to do this. I'm very new to ASP.NET)
Here you have a TemplateField in a GridView. Inside it there is an UpdatePanel and the CheckBox is inside it. This is done to make the checking of the TextBox do post in the backgroung (ajax). You might not need it (UpdatePanel) at all.
<asp:TemplateField HeaderText="Private" SortExpression="IsPrivate">
<ItemTemplate>
<asp:UpdatePanel ID="upIsPrivate" runat="server" UpdateMode="Always" ChildrenAsTriggers="true">
<ContentTemplate>
<asp:CheckBox ID="chkIsPrivate" runat="server" OnCheckedChanged="chkIsPrivate_CheckedChanged" AutoPostBack="true" />
</ContentTemplate>
</asp:UpdatePanel>
</ItemTemplate>
</asp:TemplateField>
And this is the method that handles this. Notice that I get the Id from the GridViewRow that contains the CheckBox:
GridViewRow row = (GridViewRow)((CheckBox)sender).Parent.Parent.Parent.Parent;
protected void chkIsPrivate_CheckedChanged(object sender, EventArgs e)
{
if (editMode)
{
GridViewRow row = (GridViewRow)((CheckBox)sender).Parent.Parent.Parent.Parent;
Int32 id = (Int32)uxPhoneCallList.DataKeys[row.RowIndex]["Id"];
CheckBox isPrivate = (CheckBox)row.FindControl("chkIsPrivate");
PhoneCall phoneCall = PhoneCallManager.GetById(id);
...
}
}

Resources