By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,544 Members | 2,002 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,544 IT Pros & Developers. It's quick & easy.

Getting values from user control added to DataGrid

P: n/a
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_dropd own2.Selected_dept_value())).SelectedValue;
Nov 19 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
And the anwser is:

In the code behind:
str_emp_dept =
Convert.ToString(((Department_dropdown)e.Item.Find Control("Department_dropdown2")).Selected_dept_val ue());

WHOOOOOOHOOOOOOO!!!!!!!!!!!!!!
"Moojjoo" wrote:
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_dropd own2.Selected_dept_value())).SelectedValue;

Nov 19 '05 #2

P: n/a
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
"Moojjoo" wrote:
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_dropd own2.Selected_dept_value())).SelectedValue;

Nov 19 '05 #3

P: n/a
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?

"Michael Baltic" wrote:
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
"Moojjoo" wrote:
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_dropd own2.Selected_dept_value())).SelectedValue;

Nov 19 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.