Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Error when trying to retrieve data from datagrid

Status
Not open for further replies.

suicidaltendencies

Programmer
Jan 28, 2004
58
US

Error:
Object reference not set to an instance of an object.

Code:
lblDetailsMore.Text =((System.Web.UI.WebControls.Label)dgEditAppointments.SelectedItem.FindControl("lblName")).Text;

ASP Page:
<asp:templatecolumn headerstyle-wrap="false" HeaderText="Customer Name" itemstyle-verticalalign="top">
<itemtemplate>
<asp:label id="lblName"><%# DataBinder.Eval(Container.DataItem, "CustomerName") %></asp:label>
</itemtemplate>
</asp:templatecolumn>

Thanks,
Harold
 
Find the label control in the template column during the ItemDataBound event of the DataGrid.



Code:
Code:
protected void dgEditAppointments_ItemDataBound(object sender, DataGridItemEventArgs e){
    Label lblName = (Label)e.Item.FindControl("lblName");
    if(!(lblName==null)){
        this.lblDetailsMore.Text = lblName.Text;
    }
}

ASP Page:
<asp:datagrid runat="server" id="dgEditAppointments" OnItemDataBound="dgEditAppointments_ItemDataBound">
<Columns>
<asp:templatecolumn headerstyle-wrap="false" HeaderText="Customer Name" itemstyle-verticalalign="top">
    <itemtemplate>            
        <asp:label id="lblName"><%# DataBinder.Eval(Container.DataItem, "CustomerName") %></asp:label>
    </itemtemplate>                
</asp:templatecolumn>
</Columns>
</asp:datagrid>

HTH
 
This doesn't work for me. What I need to happen is when I click the "Select" button column I want a label to display what is in the selected 4th column. The forth column is a label.

Thanks,
James
 
This doesn't work for me. What I need to happen is when I click the "Select" button column I want a label to display what is in the selected 4th column. The forth column is a label.

Thanks,
Harold
 
Add runat=server to the Label tag:
Code:
<asp:templatecolumn headerstyle-wrap="false" HeaderText="Customer Name" itemstyle-verticalalign="top">
    <itemtemplate>            
        <asp:label id="lblName" runat=server><%# DataBinder.Eval(Container.DataItem, "CustomerName") %></asp:label>
    </itemtemplate>                
</asp:templatecolumn>
When you click an item in the datagrid, the ItemCommand event will fire. In the code behind event handler, you can get a reference to the Label in the clicked row.
Code:
private void DataGrid1_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
  Label lbl = (Label)e.Item.FindControl("lblName");
  string text = lbl.Text;
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top