reference a control

G

Guest

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.
 
S

Steven Cheng[MSFT]

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/subscriptions/managednewsgroups/default.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/subscriptions/support/default.aspx.

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



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

Guest

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 said:
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/subscriptions/managednewsgroups/default.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/subscriptions/support/default.aspx.

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



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

Steven Cheng[MSFT]

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 <div> or html <table> to 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.
 
G

Guest

Hi Steven thanks for your reply. Now I have a more clear idea on the two
different choises (masterpage or tables and div).
sincerely
 

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

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top