binding an asp.net checkbox on serverside - c#

I have an search functionality in which the checkbox should come as checked or unchecked based on the data these check boxes were multiple and I am doing it on server side here I have is the code
<asp:CheckBox ID="AApBlue" runat="server"Checked='<%#GetBoolean(Eval("blueFlag").ToString()) %>'/>Blue
.cs file
protected Boolean GetBoolean(string val)
{
return val == "Y" ? true : false;
}
I am getting an error:
object reference null pointer exception
Please help!

Method 1:
You should try ItemDataBound event, add a Label and set it as visible false:
<asp:CheckBox ID="AApBlue" runat="server" />
<asp:Label ID="Label1" Text='<% # Eval("blueFlag") %>' Visible="false" runat="server" >
</asp:Label>
.CS File:
protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item ||
e.Item.ItemType == ListItemType.AlternatingItem)
{
CheckBox chk = e.Item.FindControl("AApBlue") as CheckBox;
Label lbl = e.Item.FindControl("Label1") as Label;
chk.Checked = (lbl.Text == "Y") ? true : false;
}
}
Note: Don't forget to add OnItemDataBound event in DataList.
<asp:DataList ID="DataList1" OnItemDataBound="DataList1_ItemDataBound" runat="server">
Method 2:
You can use ternary operator in checkbox checked as:
Checked='<% # (Eval("blueFlag").ToString() == "Y") ? true : false %>'

Related

Error in getting Nullreferenceexception even though there is text assigned to the label

I have been getting the NullrefernceException on my aspx.cs page even though I already assigned a text for the label in my aspx page. At first i thought it could be the session but i log in and try i still get the same error. I have checked my database and the spelling and there is nothing wrong
My aspx.cs code:
protected void Page_Load(object sender, EventArgs e)
{
Label Lefthowmanylabel =(Label)DataList1.FindControl("Lefthowmanylabel");
Label quantitylabel = (Label)DataList1.FindControl("quantitylabel");
if (int.Parse(quantitylabel.Text) < 11)
{
Lefthowmanylabel.Visible = true;
}
else
{
Lefthowmanylabel.Visible = false;
}
}
My datalist item in aspx page:
<asp:Label ID="Lefthowmanylabel" runat="server" Text="Only 10 Left!! While stock last!" Visible="False"/>
<asp:Label ID="quantitylabel" runat="server" Text='<%# Eval("Quantity") %>' Visible="False" />
</td>
If you want to access access controls inside DataList and manipulate them, you want to use ItemDataBound.
For example,
<asp:DataList ... OnItemDataBound="Item_Bound" runat="server">
</asp:DataList>
void Item_Bound(Object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item ||
e.Item.ItemType == ListItemType.AlternatingItem)
{
Label lefthowmanylabel =(Label)e.Item.FindControl("Lefthowmanylabel");
Label quantitylabel = (Label)e.Item.FindControl("quantitylabel");
if (int.Parse(quantitylabel.Text) < 11)
{
lefthowmanylabel.Visible = true;
}
else
{
lefthowmanylabel.Visible = false;
}
}
}

Items are looping all items in Repeater's ItemDataBound event

I have a normal Repeater Control in my aspx page
<asp:Repeater ID="rpt1" runat="server" OnItemDataBound="rpt1_ItemDataBound">
<ItemTemplate>
<asp:CheckBox ID="chks" runat="server" />
<asp:TextBox ID="txtName" runat="server" CssClass="form-control" Text='<%# DataBinder.Eval(Container,"DataItem.Name").ToString()%>'></asp:TextBox><asp:Label ID="lblValue" runat="server" Visible="false" Text='<%# DataBinder.Eval(Container,"DataItem.Id").ToString() %>'></asp:Label>
</ItemTemplate>
</asp:Repeater>
On the button click I'm binding data to the Repeater as
rpt1.DataSource = GetData();
rpt1.DataBind();
After binding the ItemDataBound event is called. In that I'm looping through the repeater items for some manipulations
protected void rpt1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
foreach (RepeaterItem item in rpt1.Items)
{
if (item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem)
{
string val = ((Label)item.FindControl("lblValue")).Text;
// Some Stuff
}
}
}
The problem is that the loop is getting started from first every time.
For Ex if my data is as 1 2 3 so on.....
It is iterarting as
1
1 2
1 2 3
How can I make it as
1
2
3
What am I doing wrong
ItemDataBound is already called for every item in the repeater, so you don't need a loop there.
protected void rpt1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
string val = ((Label)e.Item.FindControl("lblValue")).Text;
// Some Stuff
}
}
Side-Note: that applies to all Data-Bound Web Server Controls.

Issue Related to Binding Image in <asp:Repeater> Control

I have asp.net Web Application. i have following Repeater control on the .aspx Page:
<asp:Repeater ID="repHomeList" runat="server" onitemdatabound="repHomeList_ItemDataBound">
<ItemTemplate>
<div class="pro-col">
<div class="p-img">
<asp:Image runat="server" ID="imgHomeImage" />
<asp:Label runat="server" ID="lblImageName" Visible="false" Text='<%#Eval("Images") %>'></asp:Label>
</div>
</div>
</ItemTemplate>
</asp:Repeater>
and Following Event
protected void repHomeList_ItemDataBound(object sender, RepeaterItemEventArgs e) {
if (e.Item.ItemType != ListItemType.Item && e.Item.ItemType != ListItemType.AlternatingItem)
return;
Image imgHomeImage = (Image)e.Item.FindControl("imgHomeImage");
Label lblImageName = (Label)e.Item.FindControl("lblImageName");
if (imgHomeImage != null && lblImageName != null)
imgHomeImage.ImageUrl = Server.MapPath("PropertyImages/" + lblImageName.Text);
}
the above code compiles fine. but Image is not displaying when DataBinds in Repeater Control.
so how to set Image on ItemDatabound event ??
Thanks
Try changing it to this instead:
imgHomeImage.ImageUrl = Server.MapPath("~/PropertyImages/" + lblImageName.Text);
Please note the ~
Ps you can get the image name server side with the following:
ListViewDataItem dataItem = (ListViewDataItem)e.Item;
YourObject anObject = (YourObject)dataItem.DataItem;
var images = anObject.Images;
imgHomeImage.ImageUrl = Server.MapPath("~/PropertyImages/" + images);
You'd have to change YourObject to your specific object.
This would avoid the need to write them out and retrieve them.

Passing the current Item to a function

I have a Repeater:
<asp:Repeater runat="server" ID="RepeaterCategorie">
<ItemTemplate>
<%#((isBlocked()) ? "true" : "false") %>
</ItemTemplate>
</asp:Repeater>
where I call a function on the .cs. I'd like to pass the current item (I mean, the current item iterate in the list of the datasource) to that function. How can I do it without passing the reference through the isBlocked function?
HTML
<asp:Repeater runat="server" ID="RepeaterCategorie"
OnItemDataBound="RepeaterCategorie_ItemDataBound">
<ItemTemplate>
<asp:Label runat="server" Id="lblBool"></asp:Label>
</ItemTemplate>
</asp:Repeater>
CS
protected void RepeaterCategorie_ItemDataBound(
object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item ||
e.Item.ItemType == ListItemType.AlternatingItem)
{
var lblBool = (Label)e.Item.FindControl("lblBool");
lblBool.Text = isBlocked(sender, e) ? "true" : "false";
}
}

Can't find control within asp.net repeater?

I have the following repeater below and I am trying to find lblA in code behind and it fails. Below the markup are the attempts I have made:
<asp:Repeater ID="rptDetails" runat="server">
<HeaderTemplate>
<table>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td><strong>A:</strong></td>
<td><asp:Label ID="lblA" runat="server"></asp:Label>
</td>
</tr>
</ItemTemplate>
</asp:Repeater>
</table>
First I tried,
Label lblA = (Label)rptDetails.FindControl("lblA");
but lblA was null
Then I tried,
Label lblA = (Label)rptDetails.Items[0].FindControl("lblA");
but Items was 0 even though m repeater contains 1 itemtemplate
You need to set the attribute OnItemDataBound="myFunction"
And then in your code do the following
void myFunction(object sender, RepeaterItemEventArgs e)
{
Label lblA = (Label)e.Item.FindControl("lblA");
}
Incidentally you can use this exact same approach for nested repeaters. IE:
<asp:Repeater ID="outerRepeater" runat="server" OnItemDataBound="outerFunction">
<ItemTemplate>
<asp:Repeater ID="innerRepeater" runat="server" OnItemDataBound="innerFunction">
<ItemTemplate><asp:Label ID="myLabel" runat="server" /></ItemTemplate>
</asp:Repeater>
</ItemTemplate>
</asp:Repeater>
And then in your code:
void outerFunction(object sender, RepeaterItemEventArgs e)
{
Repeater innerRepeater = (Repeater)e.Item.FindControl("innerRepeater");
innerRepeater.DataSource = ... // Some data source
innerRepeater.DataBind();
}
void innerFunction(object sender, RepeaterItemEventArgs e)
{
Label myLabel = (Label)e.Item.FindControl("myLabel");
}
All too often I see people manually binding items on an inner repeater and they don't realize how difficult they're making things for themselves.
I just had the same problem.
We are missing the item type while looping in the items. The very first item in the repeater is the header, and header does not have the asp elements we are looking for.
Try this:
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{Label lblA = (Label)rptDetails.Items[0].FindControl("lblA");}
Code for VB.net
Protected Sub rptDetails_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rptDetails.ItemDataBound
If e.Item.ItemType = ListItemType.AlternatingItem Or e.Item.ItemType = ListItemType.Item Then
Dim lblA As Label = CType(e.Item.FindControl("lblA"), Label)
lblA.Text = "Found it!"
End If
End Sub
Investigate the Repeater.ItemDataBound Event.
You should bind first.
for example)
rptDetails.DataSource = dataSet.Tables["Order"];
rptDetails.DataBind();

Categories