Dovberman
asked on
How to refer to a control in the cs file
I placed a control named "lstEvalItemName" in the aspx file.
<asp:ListBox id="lstEvalItemName" runat="server" AutoPostBack="True" Height="1.2in"
Width="2.28in" BackColor="Azure"
SelectionMode="Single"
OnSelectedIndexChanged = "lstEvalItemName_Click">
</asp:ListBox>
Then I tried to refer to it in the cs file.
intEvalItemID = int.Parse(lstEvalItemName. SelectedVa lue);
The name 'lstEvalItemName' does not exist in the current context.
This happens with all the controls on the page.
This page is attached to a master page.
What do I need to change?
Thanks
<asp:ListBox id="lstEvalItemName" runat="server" AutoPostBack="True" Height="1.2in"
Width="2.28in" BackColor="Azure"
SelectionMode="Single"
OnSelectedIndexChanged = "lstEvalItemName_Click">
</asp:ListBox>
Then I tried to refer to it in the cs file.
intEvalItemID = int.Parse(lstEvalItemName.
The name 'lstEvalItemName' does not exist in the current context.
This happens with all the controls on the page.
This page is attached to a master page.
What do I need to change?
Thanks
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Where is located your lstEvalItemName ListBox? In the master page or in the derived page?
ASKER
It is in a derived page.
This works, but is it the preferred way?
public ListBox myListBox;
protected void Page_Load(object sender, EventArgs e)
{
myListBox = FindControl("lstEvalItemNa me") as ListBox;
This works, but is it the preferred way?
public ListBox myListBox;
protected void Page_Load(object sender, EventArgs e)
{
myListBox = FindControl("lstEvalItemNa
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
By default all controls added to a page are protected members. It is accessible from within that page and only from within that page.
If you want to access the control from outside the page you have to make it public or use FindControl method.
If you want to access the control from outside the page you have to make it public or use FindControl method.
public ListBox SelectBox
{
get { return lstEvalItemName; }
}
You can then access SelectBox in the derived page
ASKER
The control is acting as if it is protected. However the control I am trying to reference should be available directly.
I placed a control named "lstEvalItemName" in the aspx file.
<asp:ListBox id="lstEvalItemName" runat="server" AutoPostBack="True" Height="1.2in"
Width="2.28in" BackColor="Azure"
SelectionMode="Single"
OnSelectedIndexChanged = "lstEvalItemName_Click">
</asp:ListBox>
Then I tried to refer to it in the cs file.
I am not trying to access it from the master's Page_Load?
intEvalItemID = int.Parse(lstEvalItemName. SelectedVa lue);
It should be accessible from within that page.
I placed a control named "lstEvalItemName" in the aspx file.
<asp:ListBox id="lstEvalItemName" runat="server" AutoPostBack="True" Height="1.2in"
Width="2.28in" BackColor="Azure"
SelectionMode="Single"
OnSelectedIndexChanged = "lstEvalItemName_Click">
</asp:ListBox>
Then I tried to refer to it in the cs file.
I am not trying to access it from the master's Page_Load?
intEvalItemID = int.Parse(lstEvalItemName.
It should be accessible from within that page.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you.
ASKER
This does not work either.
intEvalItemID = int.Parse(this.FindControl
Ideally, I would like to reference the control as a public listbox and give it a name.
public ListBox myListBox;
Then I would like to set myListBox equal to the actual control on the page.
?? myListBox == ("lstEvalItemName")