Home

Web User Control DropDownList Values Access

Sheez! Why so complicated!!

OK DropDownList in . Web User Control or .ascx page for humans.

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="clientdropdownlist.ascx.cs" Inherits="controls_clientdropdownlist" %>

<asp:DropDownList ID="mylist" runat="server" />

Code Behind

using System;
using Npgsql;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class controls_clientdropdownlist : System.Web.UI.UserControl
{

    public string clientf
    {
        get
        {
            return mylist.SelectedValue;
        }
        set
        {
            mylist.SelectedValue = value;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            using (NpgsqlConnection dbconn = new NpgsqlConnection(RCSharp.MyPgsqlConnString()))
            {
                dbconn.Open();
                string qwe = "SELECT * FROM clientt WHERE customerf = @customerf ORDER BY namef";
                using (NpgsqlCommand runcommand = new NpgsqlCommand(qwe, dbconn))
                {
                    runcommand.Parameters.AddWithValue("@customerf", Session["customerf"]);
                    using (NpgsqlDataReader dbread = runcommand.ExecuteReader())
                    {
                        mylist.DataSource = dbread;
                        mylist.DataTextField = "namef";
                        mylist.DataValueField = "uidf";
                        mylist.DataBind();
                    }
                }

            }
        }   
    }
}

Main Page

.......

<%@ Register Src="~/controls/clientdropdownlist.ascx" TagPrefix="uc1" TagName="clientdropdownlist" %>

...........

 <uc1:clientdropdownlist runat="server" ID="clientdropdownlist" />

So now in the main regular page we can access the dropdown list thus

To SET the list

clientdropdownlist.clientf = SomeValueMustBeInList;

so clientdropdownlist is name of the control as stated on the primary main page. clientf is the name of the string under the umbrella of the web user control.

To GET the selected value

WhatIsTheSelectedValue = clientdropdownlist.clientf;

Eh...what...no selectedvalue, no extra bit? I figure the "get" and "set" bit means if the string is mentioned in a "here gimme this" it uses the get route. If it the string is mentioned in "here I want you to be this" it sets in the the set method.

 

Reader's Comments

Post Your Comment Posts/Links Rules

Name

Comment

Add a RELEVANT link (not required)

Upload an image (not required)

No uploaded image
Real person number
Please enter the above number below




Home
Admin Ren's Biking Blog