OnCheckedChanged doesnt pick up function when checked - asp.net

Using a repeater, which displays a list, which includes a checkbox field.
When a checkbox is clicked I want the function to run and display the label....trying to debug this but cant seem to get it going, using breakpoint wont pick up the function
//code behind
protected void chkMyCheck_CheckedChanged(object sender, EventArgs e)
{
label.Text = "Button Clicked";
}
aspx:
<asp:Repeater id="rptSelectedUtilities" runat="server">
<HeaderTemplate>
<table class="detailstable FadeOutOnEdit">
<tr>
<th style="width:200px;">Utility</th>
<th style="width:200px;">Contacted</th>
<th style="width:200px;">Comment</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<th style="width:200px;"><%# Eval("Name") %></th>
<th style="width:200px;"><asp:CheckBox ID="chkMyCheck" AutoPostBack="true" runat="server" OnCheckedChanged="chkMyCheck_CheckedChanged" Checked='<%# Convert.ToBoolean(Eval("Checked")) %>'/></th>
<th style="width:200px;"><%# Eval("Comment") %></th>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
<asp:Label id="labelTableEmpty" runat="server" Text="There are currently no items in this table." />
<asp:Label ID ="label" runat="server" />
Any idea as to why the code doesnt go into the chkMyCheck_CheckedChanged function when a checkbox is clicked?, the page does re-fresh so its picking up auto post back

Are you bind repeater inside of !IsPostBack ????
for
void page_load()
{
if(!IsPostBack)
{
//BindRepeater method???
}
}
Check

Related

ASP.net DataGrid adding extra information to table I do not want

Here is what DataGrid does to my table:
<table id="ContentPlaceHolder1_articleList" cellspacing="0" style="border-width:0px;border-collapse:collapse;" headertext="File Name" rules="all">
How can I remove the unnecessary info from the table?
I do not want cellSpacing, anything in the style. I take care of all of thise with CSS and because these are inline it is overriding my CSS declarations.
I don't care if the ID, or rules="all" (not even sure what that does) or the HeaderText is present, but I would love to get rid of the rest.
As Curt suggested, you can use a Repeater control for more control over the output. Another option would be the ListView control, which gives you more options than the the Repeater does.
<asp:ListView ID="ListView1" runat="server">
<LayoutTemplate>
<table class="myclass">
<asp:PlaceHolder ID="itemPlaceholder" runat="server" />
</table>
</LayoutTemplate>
<ItemTemplate>
<tr>
<td><%# Eval("ColumnName", "{0:#,###} bytes") %></td>
</tr>
</ItemTemplate>
</asp:ListView>
If you really want full control over your table, I would recommend using a Repeater instead. Unlike the DataGrid control, the Repeater control doesn't render any HTML which isn't inside the ItemTemplate giving you full control over your rendered code.
<table>
<tr>
<td>Column Header</td>
</tr>
<asp:repeater id="rep" runat="server">
<itemtemplate>
<tr>
<td>
<%#eval("ColumnName") %>
</td>
</tr>
</asp:repeater>
</table>
As the the Eval/DataItem will give object/String return value. In
order to convert into 3,546 bytes output, it is required to convert it into
integer. For that onitemdatabound event registration will be required.
Mark Up
<asp:ListView ID="ListView1" runat="server" onitemdatabound="rpt_ItemDataBound">
<LayoutTemplate>
<table class="myclass">
<asp:PlaceHolder ID="itemPlaceholder" runat="server" />
</table>
</LayoutTemplate>
<ItemTemplate>
<tr>
<td><asp:Label ID="lbl" runat="server" ></asp:Label></td>
</tr>
</ItemTemplate>
</asp:ListView>
Code Behind
protected void rpt_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item)
{
Label lbl= (Label)e.Item.FindControl("lbl");
lbl.Text = String.Format("{0:#,### bytes}",
Convert.ToInt32(((YourClassName)e.Item.DataItem).YourProperty);
}
}

Binding a Dictionary to a repeater asp.net 4

i have a dictionary in this form Dictionary<int, List<CartItem>>, this dictionany contain informationa about my shopping cart item. i am looking for a way to display the result in a repeater or a datalist what ever is better. i have started using a repeater and i got stuck
i want to display the information in the list in the repeater for each element in the repeater
in the page.aspx.cs
void bindinfo()
{
ShoppingCart cart = ShoppingCart.GetShoppingCart();
RepeaterCustomerShoppingCarts.DataSource = cart.dictionaryShoppingCart;
RepeaterCustomerShoppingCarts.DataBind();
}
in page.aspx
<asp:Repeater id="RepeaterCustomerShoppingCarts" onitemdatabound="RepeaterCustomerShoppingCarts_ItemDataBound" runat="server">
<HeaderTemplate>
<table width="100%" border="0" cellspacing="0" cellpadding="0" class="shopping-table">
<tr>
<th> </th>
<th>Merchant Name</th>
<th>Quantity</th>
<th>Total Amount</th>
<th> </th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td class="first"><asp:Image ID="MerchantLogo" runat="server" width="52" height="46"/></td>
<td>
<b><asp:Label ID="lblTitle" runat="server" Text=""></asp:Label></b>
<p> <asp:Label ID="lblDetails" runat="server" Text=""></asp:Label> </p>
</td>
<td>
<div class="dropdown">
<asp:Label ID="lblQuantity" runat="server" Text=""></asp:Label>
</div>
</td>
<td><b><asp:Label ID="lblTotalPrice" runat="server" Text=""></asp:Label> </b></td>
<td><asp:HyperLink ID="hlShopDetails" CssClass="remove" runat="server">More Details</asp:HyperLink></td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
any help and advice to try to display the information would be great. the purpose of the page is u can have many shopping cart depending on different merchant and u display them here than there is a link called more details it will take you to a specific cart.
thanks
Try this
protected void RepeaterCustomerShoppingCarts_ItemDataBound(Object Sender, RepeaterItemEventArgs e) {
if (e.Item.ItemType == ItemType.AlternatingItem || e.Item.ItemType == ItemType.Item){
// Grab all your controls like this
var lblQuantity = e.Item.FindControl("lblQuantity") as Label;
var NestedRepeater = e.Item.FindControl("NestedRepeater") as Repeater;
// Get the current data
var data = (KeyValuePair<Int32, List<CartItem>>)e.Item.DataItem;
//Bind the values
lblQuantity.Text = data.Value.Count.ToString();
NestedRepeater.DataSource = data.Value;
NestedRepeater.DataBind();
}
}
The markup would look something like this.
<asp:Repeater id="RepeaterCustomerShoppingCarts" onitemdatabound="RepeaterCustomerShoppingCarts_ItemDataBound" runat="server">
<ItemTemplate>
<asp:Label id="lblQuantity" runat="server" />
<asp:Repeater runat="server" id="NestedRepeater">
<ItemTemplate>
<!-- whatever your controls -->
</ItemTemplate>
</asp:Repeater>
</ItemTemplate>
</asp:Repeater>
The thing here is that you control your layout in the code behind. Grab the necessary controls there and the current data. Then just do your bindings as normally.
Rather than using the normal Eval("ColumnName") on a dictionary you would simply do Eval("value")
You could do something like this.
<asp:Label ID="lblQuantity" runat="server" Text='<%#(Eval("key")=="Quantity" ? Eval("value") : "") %>' />

Modal Popup Extender: Fetch data from database then bind it to the label in the modalpopupextender

I've a problem with ModalPopupExtender. I have put a label in ModalPopupExtender and trying to bind label with the value from database. When I am debugging, the value is showing on the label's text (ie there is no database error), but it is not displaying when the popup window is shown.
Can any one help me please ????????
Here's my code
Code on html
<asp:Panel ID="PopupPnl" runat="server">
<table style="width:100%;">
<tr>
<td >
<asp:Label ID="Label2" runat="server"
Text="View Description" >
</asp:Label>
</td>
</tr>
<tr>
<td >
<asp:Label ID="label_Descrption" runat="server" >
</asp:Label>
</td>
</tr>
<tr style="background-color:White;">
<td >
<asp:Button ID="btnExit" runat="server" Text="Exit"/>
</td>
</tr>
</table>
</asp:Panel>
<asp:Button ID="btnShowPopup" runat="server"
style="display:none;"/>
<cc1:ModalPopupExtender ID="ModalPopupExtender2" runat="server"
TargetControlID="btnShowPopup"
PopupControlID="PopupPnl"
CancelControlID="btnExit"
BackgroundCssClass="ModalPopupBG" >
</cc1:ModalPopupExtender>
Code on c#
protected void lbn_Template_Click(object sender, EventArgs e)
{
util_Category objUtilCategory = new util_Category();
cl_Category objClCategory = new cl_Category();
if (ddlSubCategory.SelectedItem.Text != "--Select A SubCategory--")
{
objClCategory.CategoryId = Convert.ToInt32(ddlSubCategory.SelectedValue);
Label lbl = (Label)PopupPnl.FindControl("label_Descrption");
label_Descrption.Text = objUtilCategory.GetATemplate(objClCategory);
ModalPopupExtender2.Show();
}
}
You need to place the Panel which contains the label_Description inside UpdatePanel.

Can't get edit to work on a ListView

I have the following code which I thought would give me the ability to edit a record in my list view but when i click edit i get a postback but am not able to edit anything. Am i doing something wrong?
<asp:ListView ID="lv_Personnel" runat="server" OnItemEditing="lv_Personnel_ItemEditing">
<LayoutTemplate>
<table cellpadding="2" border="1" runat="server" id="tbl_Personnel">
<tr id="headerRow" runat="server">
<th>
</th>
<th>
Level of Staff
</th>
</tr>
<tr runat="server" id="itemPlaceholder" />
<tr runat="server" id="insertPlaceholder" />
</table>
</LayoutTemplate>
<ItemTemplate>
<tr runat="server">
<td>
<asp:LinkButton ID="btnEdit" runat="server" Text="Edit" CommandName="Edit" />
</td>
<td>
<%# Eval("LineDescription")%>
</td>
</tr>
</ItemTemplate>
<EditItemTemplate>
<tr runat="server" style="background-color: #ADD8E6">
<td>
</td>
<td>
Level of Staff:
<asp:TextBox ID="tb_LevelOfStaff" runat="server" Text='<%# Eval("LineDescription") %>' />
</td>
</tr>
</EditItemTemplate>
</asp:ListView>
Are you databinding your ListView to anything? If it's not databound, then you're going to have to manually specify the record that you want to edit by handling the ItemEditing event.
protected void MyListView_ItemEditing(object sender, ListViewEditEventArgs e)
{
ListView1.EditIndex = e.NewEditIndex;
// Re-databind here
}
Based on comments, don't databind on every postback unless you have ViewState turned off.
private void Page_Load()
{
if (!IsPostBack)
{
//databind
}
}
It looks like i just needed to add an OnItemEditing event to my ListView declaration and the function to back it up. I've updated my code snippit above to reflect the changes made in the aspx file.

Accessing controls in the edititemtemplate of a listview

I am working with the listview control. By default I am showing the itemtemplate with an edit button. When the edit button is pressed the listview switches to the edititemtemplate. I need to populate one of the controls in the edititemtemplate based on the item being edited - I've tried accessing the control (via FindControl) in the ItemEditing event (and pretty much every other event as well), however the controls just don't seem to exist. I can access controls in the itemtemplate ok, but not the edititemtemplate.
Can anyone let me know how I can access a control held within the edititemtemplate of a listview, and from which event I should do so?
EDIT
I'm trying to access the control using this:
protected void UnitsLV_ItemEditing(object sender, ListViewEditEventArgs e)
{
ListViewItem item = UnitsLV.Items[e.NewEditIndex];
ListBox tempLB = (ListBox)e.item.FindControl("ListBox3");
}
I've also tried in ItemDataBound and ItemCreated.
The listview declaration is:
<asp:Content ID="Content1" ContentPlaceHolderID="ColumnA" runat="server">
<asp:Panel ID="Panel1" runat="server">
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Panel ID="SummaryPnl" runat="server">
<asp:ListView ID="UnitsLV" runat="server" DataSourceID="DataLDS" DataKeyNames="u_uid"
InsertItemPosition="LastItem" OnItemInserting="UnitsLV_ItemInserting" OnItemDataBound="UnitsLV_ItemDataBound"
OnItemCreated="UnitsLV_ItemCreated" onitemediting="UnitsLV_ItemEditing">
<ItemTemplate>
<tr class="rowA">
<td>
<asp:Label runat="server" ID="UnitIDLbl" Text='<%# Eval("u_uid")%>'></asp:Label>
</td>
<td>
<%# Eval("u_Title")%>
</td>
<td>
<asp:LinkButton ID="EditBtn" runat="server" CommandName="Edit" CommandArgument='<%#Eval("u_uid") %>'
Text="Edit" />
</td>
<td>
<asp:LinkButton ID="DeleteBtn" runat="server" CommandName="Delete" CommandArgument='<%#Eval("u_uid") %>'
Text="Delete" />
</td>
</tr>
</ItemTemplate>
<InsertItemTemplate>
<tr class="rowB">
<td>
<br />
</td>
<td>
<br />
<asp:TextBox ID="TitleTB" runat="server" Text='<% #Bind("u_Title")%>'></asp:TextBox>
</td>
<td>
<br />
<asp:ListBox ID="ListBox3" runat="server"></asp:ListBox>
<asp:ListBox ID="ToBeDeletedLB" runat="server"></asp:ListBox>
</td>
<td>
<asp:LinkButton ID="InsertBtn" runat="server" CommandName="Insert" Text="Insert" />
</td>
<td>
<asp:LinkButton ID="CancelBtn" runat="server" CommandName="Cancel" Text="Cancel" />
</td>
</tr>
</InsertItemTemplate>
<EditItemTemplate>
<tr class="rowB">
<td>
<br />
<asp:Label runat="server" ID="UnitIDLbl" Text='<%# Bind("u_uid")%>'></asp:Label>
</td>
<td>
<br />
<asp:TextBox ID="TitleTB" runat="server" Text='<% #Bind("u_Title")%>'></asp:TextBox>
</td>
<td>
<br />
<asp:ListBox ID="ListBox3" runat="server"></asp:ListBox>
<asp:ListBox ID="ToBeDeletedLB" runat="server"></asp:ListBox>
</td>
<td>
<asp:LinkButton ID="UpdateBtn" runat="server" CommandName="Update" Text="Update" />
</td>
<td>
<asp:LinkButton ID="LinkButton2" runat="server" CommandName="Cancel" Text="Cancel" />
</td>
</tr>
</EditItemTemplate>
<LayoutTemplate>
<table id="Table2" runat="server" width="100%">
<tr id="Tr1" runat="server">
<td id="Td1" runat="server">
<table id="itemPlaceholderContainer" runat="server" border="0" style="" width="100%">
<tr id="itemPlaceholder" runat="server"></tr>
</table>
</td>
</tr>
<tr id="Tr2" runat="server">
<td id="Td2" runat="server" style=""></td>
</tr>
</table>
</LayoutTemplate>
</asp:ListView>
</asp:Panel>
</ContentTemplate>
</asp:UpdatePanel>
</asp:Panel>
</asp:Content>
EDIT:
I've iterated over all the controls in the listview using code similar to below, and the control is still not visible. Does the ItemEditing event fire before the edit template is shown? If so, what event can I use to access the edit template controls?
foreach (Control child in control.Controls)
{
Control result = Find(child, id);
if (result != null)
{
return result;
}
}
**EDIT: **
I can access the controls in the edititemtemplate in the listview's ItemCreated event, however none they have no content (I'd assume the data hasn't been bound yet), so I can't get the key-value I need to do a lookup to get the data I need to populate the control.
I've figured out a way to do what I need to do, though I'm not terribly happy with it.
protected void UnitsLV_ItemDataBound(object sender, ListViewItemEventArgs e)
{
if (UnitsLV.EditIndex > -1)
{
// Controls within the edititemtemplate are available via e.Item.FindControl("controlname")
}
}
I don't know about previous editions but in Visual Studio 2010 you can easily access the edit item trhough the "ListView.EditItem" property like this:
private void myListView_ItemEditing(object sender, ListViewEditEventArgs e)
{
myListView.EditIndex = e.NewEditIndex;
myListView.DataBind();
ListViewItem lvwItem = lvwLista.EditItem;
ListBox tempLB = (ListBox) lvwItem.FindControl("ListBox3");
}
protected void ListView1_ItemUpdating(object sender, ListViewUpdateEventArgs e)
{
ListBox myListBox = (ListBox)(((ListView)sender).EditItem.FindControl("ListBox1"));
}
protected void UnitsLV_ItemEditing(object sender, ListViewEditEventArgs e)
{
ListViewItem item = UnitsLV.Items[e.NewEditIndex];
ListBox tempLB = (ListBox)e.item.FindControl("ListBox3");
}
I believe I found a typo in the above function. The second line should be
ListBox tempLB = (ListBox)item.FindControl("ListBox3");
What I did was replace "e.item" with "item"
I usually use the ItemDataBound Event... check the other options in ListItemType Enum
protected void UnitLV_ItemDataBound(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.EditItem)
{
ListBox myListBox = (ListBox) e.Item.FindControl("ListBox3");
}
}
this is similar to the accepted answer, but I think its author was really driving towards this:
protected void UnitsLV_ItemDataBound(object sender, ListViewItemEventArgs e)
{
ListViewDataItem listViewDataItem = e.Item as ListViewDataItem;
if (UnitsLV.EditIndex == listViewDataItem.DataItemIndex)
{
// Controls within the edititemtemplate are available via e.Item.FindControl("controlname")
}
}

Resources