CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Jun 2009
    Posts
    2

    Post .cs page does not find listbox on aspx page

    Hi. I have an aspx page that has two list boxes defined on it. The first list box is populated from a stored procedure. What I am trying to do is when a user selects list items from the populated list box, then clicks the add item button, the code in the code behind page will add the list item to the second listbox and delete it from the first listbox.

    My problem is that when I build the project I get errors that say:
    "The name 'lstAllLinkCats' does not exist in the current context"

    I have searched for the answer and have tried several different things but I cannot resolve this... please help.

    I need to figure out why when I do a build my project does not find these controls.

    There is a label on the same page that is referenced in the codebehind and it is found but not these two list boxes.

    The things I have tried are:
    - cleaning the solution prior to build
    - adding listbox ld = new lixtbox(); (this gets rid of the error but does not allow me to add or delete or find the selected items from the aspx - I think this is a different listbox instance)
    - tried using page.findcontrol - (returns null BUT it the control is in the list)
    - created a new project and moved the code over to make sure it was not the solution or project itself.

    I am including the code from the aspx page and the cs page below...

    aspx code:
    <%@ Page Language="C#" MasterPageFile="~/admin.master" AutoEventWireup="true" CodeBehind="AddLink.aspx.cs" Inherits="admin_AddLink" Title=" Add Link" %>

    <asp:Content ID="addlink" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    <table border="0" cellpadding="5" cellspacing="5" width="100%">
    <tr>
    <td valign="top">
    <h1>Add New Link Listing</h1>
    <asp:SqlDataSource id="AllCatSqlDataSource" Runat="Server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
    SelectCommandType="StoredProcedure" SelectCommand="dbo.GetCatNotSelectedByLinkId">
    <SelectParameters>
    <asp:Parameter DefaultValue="0" Type="Int32" Name="LinkID" />
    </SelectParameters>
    </asp:SqlDataSource>
    <asp:SqlDataSource id="ProvidersSqlDataSource" Runat="Server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
    SelectCommandType="StoredProcedure" SelectCommand="dbo.GetProviders">
    </asp:SqlDataSource>

    <asp:SqlDataSource id="LinkSqlDataSource" Runat="Server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
    InsertCommandType="StoredProcedure" InsertCommand="dbo.AddLink" OnUpdated="LinkSqlDataSource_OnUpdated">
    <InsertParameters>
    <asp:FormParameter Name="varLinkName" FormField="txbLinkName" />
    <asp:FormParameter Name="varLinkTitle" FormField="txbLinkTitle" />
    <asp:FormParameter Name="varLinkURL" FormField="txbLinkURL" />
    <asp:FormParameter Name="dteExpireDate" FormField="txbExpireDate" />
    <asp:FormParameter Name="varImgURL" FormField="txbImgURL" />
    <asp:FormParameter Name="varImgWidth" FormField="txbImgWidth" />
    <asp:FormParameter Name="varImgHeight" FormField="txbImgHeight" />
    <asp:FormParameter Name="varAdvertiserLinkID" FormField="txbAdvertiserLinkID" />
    <asp:FormParameter Name="varLinkType" FormField="ddlLinkType" />
    <asp:FormParameter Name="bitDeleted" FormField="ddlDeleted" />
    <asp:FormParameter Name="bitSuspended" FormField="ddlSuspended" />
    <asp:FormParameter Name="varAdvertiser" FormField="ddlAdvertiser" />
    <asp:FormParameter Name="Provider" FormField="ddlProvider" />
    <asp:FormParameter Name="Categories" FormField="lstCatgories" />
    </InsertParameters>
    </asp:SqlDataSource>
    <br />
    <asp:Panel ID="pnlLinkForm" runat="server">
    <asp:Label ID="lblStatus" runat="server" CssClass="error" Visible="true" />
    <asp:ValidationSummary ID="valSummary" runat="server" HeaderText="The following fields are required:" CssClass="error" />
    <asp:FormView id="LinkFormView" DefaultMode="Insert" DataSourceID="LinkSqlDataSource" Runat="Server" DataKeyNames="intLinksID">
    <EditItemTemplate>
    <table border="0" cellpadding="3" class="UpdateTbl">
    <tr>
    <th>Link Name:</th>
    <td>
    <asp:TextBox id="txbLinkName" Text='<%# Bind("varLinkName") %>' Runat="Server" Width="200" Font-Names="Calibri" />
    <asp:RequiredFieldValidator ID="reqLinkName" runat="server" ControlToValidate="txbLinkName" Text="*" ErrorMessage="Link Name" />
    </td>
    </tr>
    <tr>
    <th>Link Title:</th>
    <td>
    <asp:TextBox id="txbLinkTitle" Text='<%# Bind("varLinkTitle") %>' Runat="Server" Width="600" MaxLength="500" Font-Names="Calibri" />
    <asp:RequiredFieldValidator ID="reqLinkTitle" runat="server" ControlToValidate="txbLinkTitle" Text="*" ErrorMessage="Link Title" />
    </td>
    </tr>
    <tr>
    <th>Link URL:</th>
    <td>
    <asp:TextBox id="txbLinkURL" Text='<%# Bind("varLinkURL") %>' Runat="Server" Width="600" MaxLength="500" Font-Names="Calibri" />
    <asp:RequiredFieldValidator ID="reqLinkURL" runat="server" Text="*" ControlToValidate="txbLinkURL" ErrorMessage="Link URL" />
    </td>
    </tr>
    <tr>
    <th>Expire Date:</th>
    <td>
    <asp:TextBox id="txbExpireDate" Text='<%# Bind("dteExpireDate") %>' Runat="Server" Width="150" Font-Names="Calibri" />
    <asp:RequiredFieldValidator ID="reqExpireDate" runat="server" ControlToValidate="txbExpireDate" Text="*" ErrorMessage="Expire Date" />
    </td>
    </tr>
    <tr>
    <th>Img URL:</th>
    <td>
    <asp:TextBox id="txbImgURL" Text='<%# Bind("varImgURL") %>' Runat="Server" Width="600" MaxLength="500" Font-Names="Calibri" />
    </td>
    </tr>
    <tr>
    <th>Img Width:</th>
    <td>
    <asp:TextBox id="txbImgWidth" Text='<%# Bind("varImgWidth") %>' Runat="Server" Width="50" Font-Names="Calibri" />
    </td>
    </tr>
    <tr>
    <th>Img Height:</th>
    <td>
    <asp:TextBox id="txbImgHeight" Text='<%# Bind("varImgHeight") %>' Runat="Server" Width="50" Font-Names="Calibri" />
    </td>
    </tr>
    <tr>
    <th>Advertiser's ID:</th>
    <td>
    <asp:TextBox id="txbAdvertiserLinkID" Text='<%# Bind("varAdvertiserLinkID") %>' Runat="Server" Width="100" Font-Names="Calibri" />
    </td>
    </tr>
    <tr>
    <th>Link Type:</th>
    <td>
    <aspropDownList ID="ddlLinkType" runat="server" AutoPostBack="true" SelectedValue='<%# Bind("varLinkType") %>' Font-Names="Calibri">
    <asp:ListItem Text="" Value="" />
    <asp:ListItem Text="Text" Value="Text" />
    <asp:ListItem Text="Image" Value="Image" />
    </aspropDownList>
    <asp:RequiredFieldValidator ID="reqLinkType" runat="server" ControlToValidate="ddlLinkType" Text="*" ErrorMessage="Link Type" />
    </td>
    </tr>
    <tr>
    <th>Deleted:</th>
    <td>
    <aspropDownList ID="ddlDeleted" runat="server" AutoPostBack="true" SelectedValue='<%# Bind("bitDeleted") %>' Font-Names="Calibri">
    <asp:ListItem Text="" Value="" />
    <asp:ListItem Text="Yes" Value="True" />
    <asp:ListItem Text="No" Value="False" />
    </aspropDownList>
    <asp:RequiredFieldValidator ID="reqDeleted" runat="server" ControlToValidate="ddlDeleted" Text="*" ErrorMessage="Deleted" />
    </td>
    </tr>
    <tr>
    <th>Suspended:</th>
    <td>
    <aspropDownList ID="ddlSuspended" runat="server" AutoPostBack="true" SelectedValue='<%# Bind("bitSuspended") %>' Font-Names="Calibri">
    <asp:ListItem Text="" Value="" />
    <asp:ListItem Text="Yes" Value="True" />
    <asp:ListItem Text="No" Value="False" />
    </aspropDownList>
    <asp:RequiredFieldValidator ID="reqSuspended" runat="server" ControlToValidate="ddlSuspended" Text="*" ErrorMessage="Suspended" />
    </td>
    </tr>
    <tr>
    <th>Advertiser:</th>
    <td>
    <asp:TextBox id="txbAdvertiser" Text='<%# Bind("varAdvertiser") %>' Runat="Server" Width="100" Font-Names="Calibri" />
    <asp:RequiredFieldValidator ID="reqAdvertiser" runat="server" ControlToValidate="txbAdvertiser" Text="*" ErrorMessage="Advertiser" />
    </td>
    </tr>
    <tr>
    <th>Provider:</th>
    <td>
    <aspropDownList ID="ddlProvider" runat="server" AutoPostBack="true" DataSourceID="ProvidersSqlDataSource" DataTextField="varProviderName" DataValueField="intProviderID" Font-Names="Calibri">
    <asp:ListItem Text="" Value="" />
    </aspropDownList>
    <asp:RequiredFieldValidator ID="reqProvider" runat="server" ControlToValidate="ddlProvider" Text="*" ErrorMessage="Provider" />
    </td>
    </tr>
    <tr>
    <th>Categories</th>
    <td>
    <table border="1" cellpadding="3" >
    <tr>
    <td>
    <asp:ListBox ID="lstAllLinkCats" runat="server" Rows="30" Width="150" SelectionMode="Multiple" AutoPostBack="true" DataSourceID="AllCatSqlDataSource" DataTextField="varCategoryName" DataValueField="intCatID" Font-Names="Calibri">
    </asp:ListBox>
    </td>
    <td>
    <asp:Button id="AddItem"
    OnCommand="MoveItem_listboxClick"
    Text=">"
    ToolTip="Add Item"
    CommandName="AddItems"
    CommandArgument="AddItems"
    runat="server"
    CausesValidation="false" />

    <br />

    <asp:Button id="AddAllItems"
    OnCommand="MoveItem_listboxClick"
    Text=">>"
    ToolTip="Add All Item"
    CommandName="AddAllItems"
    runat="server"
    CausesValidation="false" />

    <br />

    <asp:Button id="RemoveItem"
    OnCommand="MoveItem_listboxClick"
    Text="<"
    ToolTip="Remove Item"
    CommandName="RemoveItems"
    runat="server"
    CausesValidation="false" />

    <br />

    <asp:Button id="RemoveAllItems"
    OnCommand="MoveItem_listboxClick"
    Text="<<"
    ToolTip="Remove All Items"
    CommandName="RemoveAllItems"
    runat="server"
    CausesValidation="false" />
    </td>
    <td>
    <asp:ListBox ID="lstLinkCats" runat="server" Rows="30" AutoPostBack="true" Width="150" SelectionMode="Multiple" Font-Names="Calibri">
    </asp:ListBox>
    </td>
    </tr>
    </table>

    </td>
    </tr>
    <tr>
    <th></th>
    <td>
    <asp:Button id="btnAdd" CommandName="Add" Text="Add Link" Width="100" Runat="Server" Font-Names="Calibri" />
    </td>
    </tr>
    </table>
    </EditItemTemplate>
    </asp:FormView>
    </asp:Panel>
    </td>
    </tr>
    </table>
    </asp:Content>

    .cs code:
    using System;
    using System.Data;
    using System.Data.SqlClient;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;


    public partial class admin_AddLink : System.Web.UI.Page
    {

    protected void Page_Load(object sender, EventArgs e)
    {
    }

    protected void LinkSqlDataSource_OnUpdated(object sender, SqlDataSourceStatusEventArgs e)
    {

    if (e.Exception == null)
    {
    lblStatus.Text = "The link listing was successfully added.";
    }
    else
    {
    lblStatus.Text = "There was an error addingthe Link listing.";
    }
    }

    protected void MoveItem_listboxClick(object sender, CommandEventArgs e)
    {
    switch (e.CommandName)
    {
    case "AddItems":
    {

    for (int i = 0; i < this.lstAllLinkCats.Items.Count; i++)
    {
    if (!lstAllLinkCats.Items[i].Selected)
    {
    lstLinkCats.Items.Add(lstAllLinkCats.Items[i].Value);
    lstAllLinkCats.Items.Remove(lstAllLinkCats.Items[i].Value);
    }
    }
    break;
    }

    case "RemoveItems":
    {
    for (int i = 0; i < lstLinkCats.Items.Count; i++)
    {
    if (lstLinkCats.Items[i].Selected)
    {
    }
    }
    break;
    }

    case "AddAllItems":
    {
    for (int i = 0; i < lstAllLinkCats.Items.Count; i++)
    {
    if (lstAllLinkCats.Items[i].Selected)
    {
    }
    }
    break;
    }

    case "RemoveAllItems":
    {
    for (int i = 0; i < lstLinkCats.Items.Count; i++)
    {
    if (lstLinkCats.Items[i].Selected)
    {
    }
    }
    break;
    }
    }
    }
    }

  2. #2
    Join Date
    Nov 2007
    Location
    .NET 3.5 / VS2008 Developer
    Posts
    624

    Re: .cs page does not find listbox on aspx page

    If the FormView control acts anything like the ListView control, any controls that are inside the "EditItemTemplate" or "InsertItemTemplate" are not available to the server.

  3. #3
    Join Date
    Jun 2009
    Posts
    2

    Re: .cs page does not find listbox on aspx page

    So if I understand what you are saying correctly I can fix this by moving the listboxes outside the template tag.

    If I do this will I still be able to save one of the list boxes items to my database even though it is outside the template tag?

  4. #4
    Join Date
    Nov 2007
    Location
    .NET 3.5 / VS2008 Developer
    Posts
    624

    Re: .cs page does not find listbox on aspx page

    For you to have access to controls, they cannot be inside of the EditItemTemplate or InsertItemTemplate. they need to either be inside the LayoutTemplate(which is probably not what you want) or outside of the FormView all together.

    This is all assuming the FormView works the same as the ListView. I haven't used the FormView, but I have used the ListView and I ran into the same problem.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured