Click here to Skip to main content
15,896,063 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm trying to receive data from selected datalist control by replyText variable here when I run the solution and click the picture it says "object reference not set to an instance of an object". What could be the problem? Help anyone??

What I have tried:

<script language="C#" runat="server">

void ImageButton_Click(object sender, ImageClickEventArgs e)
{

ImageButton img = (ImageButton)sender;
DataListItem item = (DataListItem)img.NamingContainer;
if (item != null)
{

int itemIndex = item.ItemIndex;

string replyText = ((TextBox)this.DataList1.Items[item.ItemIndex].FindControl("replyBox")).Text;
Session["Description"] = replyText;
}

Response.Redirect("ViewMealDetails.aspx");

}

</script>
Posted
Comments
ZurdoDev 17-Jun-16 11:41am    
You will have to debug it. We can't see your code.
Vincent Maverick Durano 17-Jun-16 15:59pm    
Can you post your DataList markup here? I want to see the location of the TextBox. You may also want to double check the ID of the TextBox that you passing in your FindControl method.
Member 12589899 21-Jun-16 3:34am    
<%@ Page Title="" Language="C#" MasterPageFile="~/Student/MasterPage.master" AutoEventWireup="true" CodeFile="DataList.aspx.cs" Inherits="Student_DataList" %>

<%--<asp:Content ID="Content1" ContentPlaceHolderID="title" Runat="Server">

--%>
<asp:Content ID="Content2" ContentPlaceHolderID="head" Runat="Server">
<script language="C#" runat="server">

void ImageButton_Click(object sender, ImageClickEventArgs e)
{

ImageButton img = (ImageButton)sender;
DataListItem item = (DataListItem)img.NamingContainer;
if (item != null)
{

int itemIndex = item.ItemIndex;

string replyText = ((TextBox)this.DataList1.Items[item.ItemIndex].FindControl("replyBox")).Text;
Session["Description"] = replyText;
}

Response.Redirect("ViewMealDetails.aspx");

}

</script>

<asp:Content ID="Content3" ContentPlaceHolderID="contentBod" runat="Server">

<div>
<asp:DataList ID="DataList1" runat="server"
CellPadding="3" CellSpacing="2"
Font-Names="Verdana" Font-Size="Small" GridLines="Both" RepeatColumns="3" RepeatDirection="Horizontal"
Width="946px">
<footerstyle backcolor="#F7DFB5" forecolor="#8C4510">
<HeaderStyle BackColor="#333333" Font-Bold="True" Font-Size="Large" ForeColor="White"
HorizontalAlign="Center" VerticalAlign="Middle" />
<HeaderTemplate>
Breakfast Meals
</HeaderTemplate>
<itemstyle backcolor="white" forecolor="black" borderwidth="2px">
<itemtemplate>

<asp:ImageButton id="imagebutton1" runat="server"
AlternateText="ImageButton 1"
ImageAlign="left"
ImageUrl='<% # Bind("MealPicture", "../{0}")%>'
OnClick="ImageButton_Click" Height="250px" Width="315px"/>
Description:
<asp:Label ID="lblDescription" runat="server" Text='<%# Bind("Description") %>'>
<br />

Cost:
<asp:Label ID="lblCost" runat="server" Text='<%# Bind("Cost") %>'>                       
<asp:Button ID="btnAdd" runat="server" type="button" style="background-color: orange" Text="Add to order" PostBackUrl="~/Student/ViewMealDetails.aspx" />



</div>

Member 12589899 21-Jun-16 4:05am    
I have copied and pasted all the markup code with the image button click object inside it. Take a look at my comment above.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900