3

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.

2 Answers 2

3

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
  );
Sign up to request clarification or add additional context in comments.

1 Comment

I got it works now. Thanks for being specific which is really helpfull for newbie like me.
1

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; 
   }
}

9 Comments

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!
I'am happy to help you Milacay Thank's
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" />"?
Aghilas, please show me how? I only have one line ""<uc:VendorListControl id="vendorListControl" runat="server" />" code for both dropdown lists. how can I make them in 2 rows in the table using <tr><td> tags? Thanks
<table><tr><td>your ddl 1</td></tr> <tr><td>your ddl 2</td></tr></table>
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.