Friday, July 08, 2005

Moojjoo Blog - Using User Controls in Datagrid

And the anwser is:

In the code behind:


str_emp_dept = Convert.ToString(((Department_dropdown)e.Item.FindControl("Department_dropdown2")).Selected_dept_value());

WHOOOOOOHOOOOOOO!!!!!!!!!!!!!!


THE QUESTION IS BELOW... WHEN I GET THE ANSWER I WILL POST IT.

I created an user control (.ascx file) with a DropDownList (DDL) with the
following code behide:

public string Selected_dept_value()
{
string dept_value = dept_dropdown.SelectedValue;
return dept_value;
}

All the values were added via the visual interface.

I have droped this on my .aspx page and everything works fine. When making
additions to my datagrid.

<uc1:department_dropdown id="Department_dropdown1" runat="server"></uc1:department_dropdown>

The problem is when I am editing my datagrid.

Where I have:

<asp:templatecolumn headertext="Department">
<itemtemplate>
<%# DataBinder.Eval(Container.DataItem, "Emp_Department") %>
</itemtemplate>
<edititemtemplate>
<uc1:department_dropdown id="Department_dropdown2" runat="server">
</uc1:department_dropdown>
</edititemtemplate>

The DDL displays fine, but I cannot get the values when clicking update:

Here is the error and code behind that it is failing on:

Error: Object reference not set to an instance of an object <-- However I
have the following:

protected Department_dropdown Department_dropdown2;

Code:
str_emp_dept =
((DropDownList)e.Item.FindControl(Department_dropdown2.Selected_dept_value())).SelectedValue;

Post a Comment