473,385 Members | 1,922 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

reference a control

A basic question:
In a content page I access a TextBox from code behind with no problems
(something like Me.MyField.Text="1").

This is the portion of aspx file :
<asp:Content ID="secMessagge" ContentPlaceHolderID="cphFooter" Runat="Server">
<asp:TextBox ID="MyField" runat="server" BorderStyle="Inset"
BorderWidth="3px"
ReadOnly="True" Style="left: 485px; position: relative; top:
2px; text-align: center"
Width="52px"></asp:TextBox>
</asp:Content>

In a second Content Section of the same page I then added a GridView.
The Select command in the DataSource reference the TextBox like this:
.... SelectCommand="SELECT * FROM [Contracts] WHERE ([ContractID] =
@ContractID)" ....

<SelectParameters>
<asp:ControlParameter ControlID="MyField" Name="ContractID"
PropertyName="Text" Type="string" />
</SelectParameters>

Problem:
1 - running the page I get an error saying control MyField is not found.
2 - in programming phase, the selecting list of controls shows all controls
twice (?).

The problem is surely related to the presence of a Master page. Without it a
similar page is runnning.
Can I get some help?
Thanks.

--
bruno
Oct 17 '06 #1
4 3045
Hello Bruno,

As for the DataSource control referencing problem, it is caused by the
DataSourceControl and ParamterSource control are in different
ContentPlaceHolder of the Master_Content page. The DataSourceControl can
only locate control that are in the same parent container, for master page
with multiple contentPlaceHolders, controls in different Content holder can
not be found by DataSourceControl in other content holder.

Based on my research, you can consider using the following ways to overcome
the problem:

1. Instead of using ControlParamter, you can define a normal parameter
which doesn't has a linked source (control, querystring....). e.g
==========
<asp:SqlDataSource ...............>
<SelectParameters>
<asp:Parameter DefaultValue="" Name="CategoryID" Type="Int32" />

</SelectParameters>
</asp:SqlDataSource>
===========

And runtime, you programmtically retrieve parameter value from the source
control and add the value into Select Command's parameter, you can do it in
the DataSourceControl's "Selecting" event. e.g

=-=============
protected void SqlDataSource1_Selecting(object sender,
SqlDataSourceSelectingEventArgs e)
{

e.Command.Parameters["@CategoryID"].Value =
int.Parse(txtCategory.Text);
}
==================

2. Since the cause of the problem is due to paramter control and datasource
control in different Content Holder, you can add a hidden TextBox control
in the same contentHolder of the DataSourceControl and let
DataSourcecontrol's ControlParameter refer to this hidden control, and you
need to add some code in the original TextBox.Load event to synchronize the
value between them. e.g

#the following page use a hiddden TextBox(txtTemp) to act as a linker
between the two content holder to pass parameter value.
==========================
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1"
Runat="Server">
<asp:TextBox ID="txtCategory" runat="server"
OnLoad="txtCategory_Load">1</asp:TextBox>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder2"
Runat="Server">
<asp:TextBox ID="txtTemp" runat="server" Visible="false"></asp:TextBox>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
OnSelecting="SqlDataSource1_Selecting" SelectCommand="SELECT
[CategoryID], [CategoryName] FROM [Categories] WHERE ([CategoryID] =
@CategoryID)">
<SelectParameters>

<asp:ControlParameter ControlID="txtTemp" PropertyName="Text"
Name="CategoryID" DefaultValue="1" />
</SelectParameters>
</asp:SqlDataSource>

<asp:DetailsView ID="DetailsView1" runat="server"
AutoGenerateRows="False" DataKeyNames="CategoryID"
DataSourceID="SqlDataSource1" Height="50px" Width="125px">
<Fields>
<asp:BoundField DataField="CategoryID" HeaderText="CategoryID"
InsertVisible="False"
ReadOnly="True" SortExpression="CategoryID" />
<asp:BoundField DataField="CategoryName"
HeaderText="CategoryName" SortExpression="CategoryName" />
</Fields>
</asp:DetailsView>
<asp:Button ID="Button1" runat="server" Text="Button"
OnClick="Button1_Click" />
</asp:Content>
========================================

=========code behind================
protected void txtCategory_Load(object sender, EventArgs e)
{
txtTemp.Text = txtCategory.Text;
}
=-=====================

Hope this helps. If there is anything unclear, please feel free to let me
know.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead

==================================================

Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.

==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.


Oct 18 '06 #2
Thank you Steven for your clear explanation.
Let's consider a Page with a Menu on the left, a common area on the bottom
side and a central area that changes depending on the user choices and the
application logic. A very common Web application.
I'm new on ASP.NET development and self learning and till now I didn't use
MasterPage and solved the problem with a .aspx file in which the body area
has many panels (<DIV runat="server" ..>) I change the Visible attriute
accordingly.
In this case I don't have the problem of different ControlPlaceHolders, but
from an architectural point of you, do you think this is the standard
suggested approach? What about MasterPage?
Thank you Steven.
--
bruno
"Steven Cheng[MSFT]" wrote:
Hello Bruno,

As for the DataSource control referencing problem, it is caused by the
DataSourceControl and ParamterSource control are in different
ContentPlaceHolder of the Master_Content page. The DataSourceControl can
only locate control that are in the same parent container, for master page
with multiple contentPlaceHolders, controls in different Content holder can
not be found by DataSourceControl in other content holder.

Based on my research, you can consider using the following ways to overcome
the problem:

1. Instead of using ControlParamter, you can define a normal parameter
which doesn't has a linked source (control, querystring....). e.g
==========
<asp:SqlDataSource ...............>
<SelectParameters>
<asp:Parameter DefaultValue="" Name="CategoryID" Type="Int32" />

</SelectParameters>
</asp:SqlDataSource>
===========

And runtime, you programmtically retrieve parameter value from the source
control and add the value into Select Command's parameter, you can do it in
the DataSourceControl's "Selecting" event. e.g

=-=============
protected void SqlDataSource1_Selecting(object sender,
SqlDataSourceSelectingEventArgs e)
{

e.Command.Parameters["@CategoryID"].Value =
int.Parse(txtCategory.Text);
}
==================

2. Since the cause of the problem is due to paramter control and datasource
control in different Content Holder, you can add a hidden TextBox control
in the same contentHolder of the DataSourceControl and let
DataSourcecontrol's ControlParameter refer to this hidden control, and you
need to add some code in the original TextBox.Load event to synchronize the
value between them. e.g

#the following page use a hiddden TextBox(txtTemp) to act as a linker
between the two content holder to pass parameter value.
==========================
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1"
Runat="Server">
<asp:TextBox ID="txtCategory" runat="server"
OnLoad="txtCategory_Load">1</asp:TextBox>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder2"
Runat="Server">
<asp:TextBox ID="txtTemp" runat="server" Visible="false"></asp:TextBox>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
OnSelecting="SqlDataSource1_Selecting" SelectCommand="SELECT
[CategoryID], [CategoryName] FROM [Categories] WHERE ([CategoryID] =
@CategoryID)">
<SelectParameters>

<asp:ControlParameter ControlID="txtTemp" PropertyName="Text"
Name="CategoryID" DefaultValue="1" />
</SelectParameters>
</asp:SqlDataSource>

<asp:DetailsView ID="DetailsView1" runat="server"
AutoGenerateRows="False" DataKeyNames="CategoryID"
DataSourceID="SqlDataSource1" Height="50px" Width="125px">
<Fields>
<asp:BoundField DataField="CategoryID" HeaderText="CategoryID"
InsertVisible="False"
ReadOnly="True" SortExpression="CategoryID" />
<asp:BoundField DataField="CategoryName"
HeaderText="CategoryName" SortExpression="CategoryName" />
</Fields>
</asp:DetailsView>
<asp:Button ID="Button1" runat="server" Text="Button"
OnClick="Button1_Click" />
</asp:Content>
========================================

=========code behind================
protected void txtCategory_Load(object sender, EventArgs e)
{
txtTemp.Text = txtCategory.Text;
}
=-=====================

Hope this helps. If there is anything unclear, please feel free to let me
know.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead

==================================================

Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.

==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.


Oct 18 '06 #3
Thanks for your reply Bruno,

I think whether to use MasterPage depend on the size and maintenance
quirement of your project. If it is a small internat application contains
limited number of pages, I think it surely ok to ingore master page here.
And just use <divor html <tableto structure your web page layout(I
prefer table).

Master page just help us make our website templaterize so that we can
easily define the common part of all the page in an website or web
application. And it make maintain web pages in large application much more
convenient.

Therefore, if you think it ok(and even more easier for implement your code
logic) that we do not use master page in the application (considered over
project size, maintenance, ......), just forget it and use the simplest way
you can get. If you think make pages consistent and maintainable is more
important, then we can should use MasterPage. And if you meet any problems
communication between different part(content section) on a masterized page,
you should use some other means to resolve it (like the solutions in my
last reply).

How do you think? Please feel free to let me know if you have any further
questions or ideas on this.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead
This posting is provided "AS IS" with no warranties, and confers no rights.

Oct 18 '06 #4
Hi Steven thanks for your reply. Now I have a more clear idea on the two
different choises (masterpage or tables and div).
sincerely
--
bruno
"Steven Cheng[MSFT]" wrote:
Thanks for your reply Bruno,

I think whether to use MasterPage depend on the size and maintenance
quirement of your project. If it is a small internat application contains
limited number of pages, I think it surely ok to ingore master page here.
And just use <divor html <tableto structure your web page layout(I
prefer table).

Master page just help us make our website templaterize so that we can
easily define the common part of all the page in an website or web
application. And it make maintain web pages in large application much more
convenient.

Therefore, if you think it ok(and even more easier for implement your code
logic) that we do not use master page in the application (considered over
project size, maintenance, ......), just forget it and use the simplest way
you can get. If you think make pages consistent and maintainable is more
important, then we can should use MasterPage. And if you meet any problems
communication between different part(content section) on a masterized page,
you should use some other means to resolve it (like the solutions in my
last reply).

How do you think? Please feel free to let me know if you have any further
questions or ideas on this.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead
This posting is provided "AS IS" with no warranties, and confers no rights.

Oct 18 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Pkpatel | last post by:
Hi, I keep getting this error every time I try to load crystalreportviewer on a webform with a dataset. Here is the error: -------------------------------------------------------- Server...
39
by: Mike MacSween | last post by:
Just spent a happy 10 mins trying to understand a function I wrote sometime ago. Then remembered that arguments are passed by reference, by default. Does the fact that this slowed me down...
0
by: Patrick | last post by:
This is a C# post. I'm using VB.NET to create an add-in for the Google Sidebar, and have implemented the OnDetailsView method. NOTE : I've come across this same problem (error message) even in...
1
by: Martine | last post by:
Hi there! I have a problem with programmatically adding user controls to my mobile webforms. If I load my usercontrol programmatically (in the Page_Load), the object is instantiated, I have...
9
by: Moe Sizlak | last post by:
Hi There, I am trying to write the selected value of a listcontrol when a button is clicked and I keep getting the error "object not set to a reference of an object". The libox itself is in a...
6
by: blash | last post by:
Can someone help me? I really don't have a clue. My company staff told me they often got such error: "Object reference not set to an instance of an object." when they are in search result page...
1
by: Don | last post by:
I created a Web User Control in my project and need to use it in a datagrid. The data in this control needs to be updated and the control has several properties that need to be databound. Using...
7
by: Samuel | last post by:
Hi, I am building a page that makes use of user control as a templating technique. The following is that I have in mind and it is actually working: Root/ -- login.aspx -- login.aspx.vb --...
2
by: Suzanne | last post by:
Hi all, I'm reposting this message as I'm experiencing this problem more and more frequently : I really hope someone out there can help me as I've been tearing my hair out on this one for a...
9
by: =?Utf-8?B?VG9tbXkgTG9uZw==?= | last post by:
I don't know if the following is what you are looking for, but to me what you described was using a control array. If you were using vb6 that would be easy, the following articles hopefully...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.