Getting values from user control added to DataGrid

G

Guest

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;
 
G

Guest

And the anwser is:

In the code behind:


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

WHOOOOOOHOOOOOOO!!!!!!!!!!!!!!
 
G

Guest

When you have a user control, declare it in the webform designer generated
code with all of your other controls. Make sure to name it exacltly the same
as your aspx page. Then you will be able to access it the exact same as you
would any other control.

OR -

You can just create an inherited dropdown control with the added field for
your selected department. Then you wouldn't have do a find control, it would
just automatically add itself to the designer generated code. As an added
bonus, you would see the dropdown in your designer instead of the generic
user control.
--
Staff Consultant II
Enterprise Web Services
Cardinal Solutions Group

Future Business Model
Loan Origination Services
National City Mortgage
 
G

Guest

Michael can you go into more detail on naming my user control the same as the
..aspx page?

Also, can you go into more detail on inherited dropdown control? Also did
you understand my question I was placing the UserControl inside of a datagrid?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,050
Latest member
AngelS122

Latest Threads

Top