Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

I am new to ASP.NET

Someone in this forum helped me how to get the dropdown list work wth user countrol and it is working.

In my user control file, VendorListControl.ascx, I have this code below. Please assume that the VendorListControl.ascx.cs works correctly, which is when I select a VendorName, it will fired "ddlVendor_SelectedIndexChanged" to refreshed the "ddlVendorBUList" dropdown list.

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="VendorListControl.ascx.cs" Inherits="MyNamespace.VendorListControl" %>
<asp:DropDownList runat="server" ID="ddlVendorList" onselectedindexchanged="ddlVendor_SelectedIndexChanged" AutoPostBack="True" /> 
<asp:DropDownList runat="server" ID="ddlVendorBUList" AutoPostBack="True" /> 
<asp:Label runat="server" ID="lblMessage" /> 

My VendorListControl.ascx.cs code:

using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;

namespace MyNamespace
{
    public partial class VendorListControl : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                FillVendors();
            }
        }

        protected void ddlVendor_SelectedIndexChanged(object sender, EventArgs e)
        {
            int VendorID = Convert.ToInt32(ddlVendorList.SelectedValue.ToString());
            FillVendorBU(VendorID);
        }

        private void FillVendors()
        {
            string strConn = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
            System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection(strConn);

            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = "SELECT VendorID, VendorName FROM MDF_Vendor";

            DataSet objDs = new DataSet();
            SqlDataAdapter dAdapter = new SqlDataAdapter();
            dAdapter.SelectCommand = cmd; ;
            conn.Open();
            dAdapter.Fill(objDs);
            conn.Close();

            if (objDs.Tables[0].Rows.Count > 0)
            {
                this.ddlVendorList.DataSource = objDs.Tables[0];
                this.ddlVendorList.DataTextField = "VendorName";
                this.ddlVendorList.DataValueField = "VendorID";
                this.ddlVendorList.DataBind();
                this.ddlVendorList.Items.Insert(0, "-- Select --");
            }
            else
            {
                this.lblMessage.Text = "No Vendor Found";
            }
        }

        private void FillVendorBU(int VendorID)
        {
            string strConn = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
            SqlConnection con = new SqlConnection(strConn);
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = con;
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = "SELECT VendorBUID, VendorBUName FROM dbo.MDF_VendorBU WHERE VendorID = @VendorID";
            cmd.Parameters.AddWithValue("@VendorID", VendorID);
            DataSet objDs = new DataSet();
            SqlDataAdapter dAdapter = new SqlDataAdapter();
            dAdapter.SelectCommand = cmd;
            con.Open();
            dAdapter.Fill(objDs);
            con.Close();
            if (objDs.Tables[0].Rows.Count > 0)
            {
                ddlVendorBUList.DataSource = objDs.Tables[0];
                ddlVendorBUList.DataTextField = "VendorBUName";
                ddlVendorBUList.DataValueField = "VendorBUID";
                ddlVendorBUList.DataBind();
                ddlVendorBUList.Items.Insert(0, "--Select--");
            }
            else
            {
                lblMessage.Text = "No states found";
            }
        }

    }
}

Next, in my CreateNewRecord.aspx page, I have this code to include both dropdown list from user control. And I can see the dropdown lists works properly.

<%@ Register TagPrefix="uc" TagName="VendorListControl" Src="Controls/VendorListControl.ascx" %>
// Some where in the form
<tr>
    <td class="right" width="20%">Vendor Name:</td>
       <td>
          <uc:VendorListControl runat="server" />
       </td>
</tr>

The problem is related to the ID of those two dropdown lists. When I attemp to do the insert record, it seems not to detect the ID for "ddlVendorList" and "ddlVendorBUList" come from user control ascx page. Error " The name 'ddlVendorList' does not exist in the current context"

ExecuteInsert(ddlVendorList.SelectedItem.Text,
  ddlVendorBUList.SelectedItem.Text, 
  MDFAmount.Text, 
  StartDate.Text,
  EndDate.Text,
  VendorQuarter.Text,
  MDFName.Text,
  MDFSummary.Text,
  Status.SelectedItem.Text,
  CreatedBy.Value
  );
Response.Write("<b>Record was successfully added!</b>");

I know I am new to ASP.NET, so please help.

share|improve this question
up vote 2 down vote accepted

You can put two properties in your VendorListControl to get the ddlVendorList selectedItem text and the ddlVendorBUList selectedItem text.

In VendorListControl.ascx.cs :

public string GetDdlVendorListSelectedItemText
{
   get { return this.ddlVendorList.text; }
}

public string GetDdlVendorBUListSelectedItemText
{
   get { return this.ddlVendorBUList.text; }
}

Then from your page CreateNewRecord, you can access those properties. You just need to add an id to your control :

<%@ Register TagPrefix="uc" TagName="VendorListControl" Src="Controls/VendorListControl.ascx" %>
// Some where in the form
<tr>
    <td class="right" width="20%">Vendor Name:</td>
       <td>
          <uc:VendorListControl id="vendorListControl" runat="server" />
       </td>
</tr>

And you can access your properties like this in CreateNewRecord.aspx.cs :

ExecuteInsert(this.vendorListControl.GetDdlVendorListSelectedItemText,
  this.vendorListControl.GetDdlVendorBUListSelectedItemText, 
  MDFAmount.Text, 
  StartDate.Text,
  EndDate.Text,
  VendorQuarter.Text,
  MDFName.Text,
  MDFSummary.Text,
  Status.SelectedItem.Text,
  CreatedBy.Value
  );
share|improve this answer
    
I got it works now. Thanks for being specific which is really helpfull for newbie like me. – Milacay Oct 4 '12 at 17:13

You define public property who return SelectedItem.Text in your UserControl.

User Control (Ascx)

public string YourValue
{
  get
  {
    return ddlVendorList.SelectedItem.Text;
  }

}

Page (Aspx)

You can use your public property : YourValue.

ExecuteInsert(YourValue,
.......  );

Nota : if you wish set value, you define setter on your property

After your update add theses properties

public string YourDdlVendorListSelectedItemText
{
   get 
   { 
      return this.ddlVendorList.text; 
   }
}

public string YourDdlVendorBUListSelectedItemText
{
   get 
   { 
     return this.ddlVendorBUList.text; 
   }
}
share|improve this answer
    
I updated the post with the code in my ascx.cs file. Since I am new, please help took a look of that file and let me know what I need to modify... thanks! – Milacay Oct 4 '12 at 16:47
    
I'am happy to help you Milacay, i updated my post – Aghilas Yakoub Oct 4 '12 at 16:51
    
Thank you, I got it works now – Milacay Oct 4 '12 at 17:12
    
I'am happy to help you Milacay Thank's – Aghilas Yakoub Oct 4 '12 at 17:12
    
One quick question, if I have a <table>, how do I put these two dropdown list in 2 rows like this <tr><td>"ddlVendorList" dropbox</td></tr> <tr><td>"ddlVendorBUList" dropbox</td></tr>. They are in one line only "<uc:VendorListControl id="vendorListControl" runat="server" />"? – Milacay Oct 4 '12 at 17:22

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.