ASP:Object reference not set to an instance of an object - asp.net

i am trying to pass a value to a label from a data source, but it keeps on giving me this error.
form1.aspx:
<asp:FormView ID="ProductsView" DataSourceID="ProductDataSource" DataKeyNames="Parking_ID"
runat="server" >
<ItemTemplate>
<div style="width: 200px; height: 200px; border: solid 10px white; background-position: center;
background-repeat: no-repeat; background-image: url('<%# Eval("Parking_ID", "images/{0}.bmp") %>');">
</div>
<asp:HiddenField Value='<%# Eval("Parking_Cost") %>' ID="hiddenPrice" runat="server" />
<asp:HiddenField Value='<%# Eval("Parking_Name") %>' ID="hiddenName" runat="server" />
<asp:HiddenField Value='<%# Eval("Parking_ID") %>' ID="HiddenField1" runat="server" />
</ItemTemplate>
</asp:FormView>
<asp:Label ID="lblName" runat="server" Text="TEST" CssClass="title" Style="font-size: 17px;
display: block;"></asp:Label>
<asp:Label ID="lblPrice" runat="server" Text="TEST" CssClass="title" Style="color: Red;
font-size: 12px;"></asp:Label>
<asp:Label ID="lbl_ID" runat="server" Text="" CssClass="title" Style="color: Red;
font-size: 12px;"></asp:Label>
<asp:SqlDataSource ID="ProductDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:mydatabaseConnectionString2 %>"
ProviderName="System.Data.SqlClient" SelectCommand="SELECT * FROM [Parking] WHERE ([Parking_ID] = #Parking_ID)">
<SelectParameters>
<asp:Parameter Name="Parking_ID" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
form1.aspx.vb:
lblName.Text = DirectCast(Me.ProductsView.FindControl("hiddenName"), HiddenField).Value
I am using vb.net script
Any ideas ?
Regards.

The Controls are usually in the data row of the FormView.
Try
lblName.Text = DirectCast(Me.ProductsView.Row.FindControl("hiddenName"), HiddenField).Value
But in general, when you use Control.FindControl(), there is a chance it will return Nothing if it cannot find it. So the best thing to do is do a TryCast instead of DirectCast and check to see if it's Nothing.

I guess the error source is the following statement:
Me.ProductsView.FindControl("hiddenName")
And the reason is that you dont have a Control with the ID of "hiddenName" directly in the ProductsView control.
You may need to use a recursive function to get the control ..

Is this code in the bind event?
lblName.Text = DirectCast(Me.ProductsView.FindControl("hiddenName"), HiddenField).Value
If so, it might be that you have to check which template it is running:
if (e.Item.ItemType == ItemType || e.Item.ItemType == AlternateItemType)
(I'm not sure of the types for FormView, but in a GridView it would be like this.
The control hiddenName doesn't existing on other templates.
Also you have to find the control from e.Item which is the current template.

Related

Change label text based on file upload control inside repeater

I have file upload and label control in a repeater. The problem is whenever a file is uploaded, I want to assign name of the uploaded file to a label inside repeater.
<asp:Repeater ID="Repeater1" runat="server" ClientIDMode="AutoID">
<HeaderTemplate>
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text=""><img src="../images/upload.png" alt="" style="position:absolute;cursor:pointer;"/></asp:Label>
<asp:FileUpload ID="PhotoUpload" runat="server" CssClass="uplodfile" Style="padding: 0px; position: relative; left: 0px; top: 0px; width: 20px;" />
<asp:Label ID="lblFilePath" Text="" runat="server" Style="vertical-align: inherit; padding: 0px 5px;"></asp:Label>
</ItemTemplate>
</asp:Repeater>
Can anybody assist me how to achieve this?
You can use FindControl.
protected void PhotoUpload_Click(object sender, EventArgs e)
{
Label lbl = Repeater1.Items[0].FindControl("Label1") as Label;
lbl.Text = "Label found!";
}

How to change the table layout when using datalist?

How to change the table layout when using datalist. Below is the aspx i am using to populate the data, but i get the result like shown in Example 1. There is not continuation of showing the data due to the table layout ? How can achieve to show the result as shown in example 2 ?
I am in need of using the datalist since I am using repeatcolumn which is not available in repeater.
Aspx :
<asp:DataList ID="dl_Groups" RepeatColumns="2" runat="server"
OnItemDataBound="dl_Groups_ItemDataBound" RepeatDirection="vertical"
ShowFooter="False" ShowHeader="False">
<ItemTemplate>
<asp:CheckBox runat="server" ID="chk_Group" Text='<%# Eval("category_type") %>'
Value='<%# Eval("service_type_category_id") %>' onclick="OnGroupClick" />
<asp:CheckBoxList runat="server" ID="chkServiceType" Style="padding-left: 20px"
DataValueField="ServiceTypeID" DataTextField="Name" EnableViewState="true">
</asp:CheckBoxList>
<br />
</ItemTemplate>
</asp:DataList>
have you gone through these properties of the CheckBoxList
RepeatColumns="2"
RepeatDirection="Vertical"
RepeatLayout="Table"
These may be helpful to you.
You may also take benefit of css
As follows
#chkServiceType tr{
display:inline-block;
margin-right:20px;
} ​
#chkServiceType tr label{
margin-left:5px;
}

How to change color in TemplateField using Eval

I have a Gridview contain many TemplateField.
I want make every <td> in my html source equal the color saved in my database
I try code Located below but not working it's give me a <span> tag inside <td> with my color but But do not appear on the browser
<asp:TemplateField HeaderText="BackGround Color">
<ItemTemplate>
<asp:Label ID="lblBackColor" runat="server"
BackColor='<%# ConvertFromHexToColor( Eval("BackColor").ToString()) %>'>
</asp:Label>
<itemstyle width="20%" horizontalalign="Center" />
</ItemTemplate>
</asp:TemplateField>
C# code working
public Color ConvertFromHexToColor(string hex)
{
string colorcode = hex;
int argb = Int32.Parse(colorcode.Replace("#", ""), NumberStyles.HexNumber);
Color clr = Color.FromArgb(argb);
return clr;
}
And this is the source html and css code in my browser
<td>
<span id="BodyZone__ThemesGrid_lblForeColor_0" style="background-color: #FFFFFF;"></span>
<itemstyle width="20%" horizontalalign="Center">
</itemstyle>
</td>
CSS
table.activity_datatable td {
padding: 8px 15px;
color: #6c6c6c;
vertical-align: middle;
-webkit-transition: all 0.2s;
}
If you want to check with a boolean value if it is true then Green colour else Red colour will effect. Then displaying text with the respective colour according to the Eval function. Here GetStatus is a method you need to create it in code behind with its we are binding the text to UI, or else you can bind with Eval or Bind function as usual.
ForeColor='<%# (bool)Eval("UserType")==true?System.Drawing.Color.Green:System.Drawing.Color.Red %>'
Text='<%# GetStatus((bool)Eval("UserType")) %>'>
You need to place text inside of your Label (which renders to a span)
<asp:TemplateField HeaderText="BackGround Color">
<ItemTemplate>
<asp:Label ID="lblBackColor" runat="server" BackColor='<%# ConvertFromHexToColor( Eval("BackColor").ToString()) %>'>PUT_TEXT_HERE</asp:Label>
<itemstyle width="20%" horizontalalign="Center" />
</ItemTemplate>
</asp:TemplateField>
You may also prefer using a Panel (which renders to a div) rather than a Label. Don't forget to put stuff inside of the div or span.
ForeColor='<%# Convert.ToString(Eval("ESM")) == "Elective" ? System.Drawing.Color.Green:
Convert.ToString(Eval("ESM")) == "Emergency" ? System.Drawing.Color.Red: System.Drawing.Color.Purple%>'
Try this code..........

Setting Checkbox ItemStyle ForeColor on ASP.Net DetailsView and GridView

On an ASP.Net DetailsView and also on a GridView I noticed that the tick mark in CheckBoxes are a light gray (disabled) colour even though I set it as blue.
<asp:CheckBoxField DataField="DayOfWeekMonday" HeaderText="Monday:" SortExpression="DayOfWeekMonday">
<ItemStyle ForeColor="Blue" />
</asp:CheckBoxField>
The same thing happens when the CheckBox is a TemplateField.
<asp:TemplateField HeaderText="Monday:" SortExpression="DayOfWeekMonday">
<EditItemTemplate>
<asp:CheckBox ID="CheckBoxEditDayOfWeekMonday" runat="server" Checked='<%# Bind("DayOfWeekMonday") %>' />
</EditItemTemplate>
<InsertItemTemplate>
<asp:CheckBox ID="CheckBoxInsertDayOfWeekMonday" runat="server" Checked='<%# Bind("DayOfWeekMonday") %>' />
</InsertItemTemplate>
<ItemTemplate>
<asp:CheckBox ID="CheckBoxItemDayOfWeekMonday" runat="server" Checked='<%# Bind("DayOfWeekMonday") %>' Enabled="false" />
</ItemTemplate>
<ItemStyle ForeColor="Blue" />
</asp:TemplateField>
I also tried this in the code-behind file.
Protected Sub CheckBoxItemDayOfWeekMonday_DataBinding(sender As Object, e As EventArgs)
Dim theControl As CheckBox
theControl = DetailsView.FindControl("CheckBoxItemDayOfWeekMonday")
theControl.ForeColor = Drawing.Color.Blue
End Sub
Is there a way to change it to blue like the rest of our fields and columns?
I noticed you meant the tick mark INSIDE the checkboxes, not the forecolor. I don't think you can change this as this is very OS dependant. I have implemented this with images in the past. You can try these CSS3 CheckBoxes which uses images: http://webdesign.tutsplus.com/tutorials/htmlcss-tutorials/quick-tip-easy-css3-checkboxes-and-radio-buttons/
Sample:
HTML:
<span><input type="checkbox" id="c1" name="cc" />
<label for="c1"><span></span>Check Box 1</label>
</span>
CSS:
input[type="checkbox"] {
display:none;
}
input[type="checkbox"] + label {
color:#000000;
font-family:Arial, sans-serif;
font-size:14px;
}
input[type="checkbox"] + label span {
display:inline-block;
width:19px;
height:19px;
margin:-1px 4px 0 0;
vertical-align:middle;
background:url(http://webdesigntutsplus.s3.amazonaws.com/tuts/391_checkboxes/check_radio_sheet.png) left top no-repeat;
cursor:pointer;
}
input[type="checkbox"]:checked + label span {
background:url(http://webdesigntutsplus.s3.amazonaws.com/tuts/391_checkboxes/check_radio_sheet.png) -19px top no-repeat;
}
JSFiddle: http://jsfiddle.net/4FraV/2/

ImageButton not aligning to center

Given the following markup for a GridView column, why are my image buttons showing up as left aligned?
<ItemStyle HorizontalAlign="Center" Width="55px" />
<ItemTemplate>
<asp:ImageButton ID="removeButton" runat="server"
ImageUrl="~/Images/Icons/x-m.png"
CommandArgument='<%# Eval("ResourceId") %>'
AlternateText="Remove Button"
onclick="removeButton_Click" />
</ItemTemplate>
Can you use css? You might have better luck styling using the following css.
Lets say width of x-m.png is 25px;
.X { width: 55px; }
.Y { display: block; margin: 0 auto; width: 25px; }
<ItemStyle cssClass="X" />
<ItemTemplate>
<asp:ImageButton ID="removeButton" runat="server"
ImageUrl="~/Images/Icons/x-m.png"
CommandArgument='<%# Eval("ResourceId") %>'
AlternateText="Remove Button"
onclick="removeButton_Click"
cssClass="Y"/>
</ItemTemplate>

Resources