How code can show value in Web control from Default.aspx?

J

JB

Hello

In my ASP.NET application I have Web Control containing a DropDownList
which I populate
from a datset in the Web Controls codebehind page. I then put the Web
Controls on the default.aspx .
From the default.aspx I access the Web Control's id tag to access the
DropDownList in the Web
Control. The FindControl method apparently finds the DropDownList in the
Web Control but an error display when I try to display the value of the first
item in the DropDownList residing in the Web Control.

How do I access the first item in the DropDownList in the Web Control, the
code is below;

Here is the Web Control:
<%@ Control Language="C#" AutoEventWireup="true"
CodeFile="CallInformation.ascx.cs" Inherits="CallInformation" %>
<asp:Label ID="Label5" runat="server" Text="Call Information"
style="font-weight: 700"></asp:Label><br />
<asp:Label ID="Label1" runat="server" Text="Call Type:"></asp:Label>

<asp:DropDownList ID="lstCallType" runat="server" Height="16px" Width="200px">
</asp:DropDownList>
-------------------------------------
Here is where I populate the Web Control:

public partial class CallInformation : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
DataSet rtcar = new DataSet();
DataSet dscar = new DataSet();
cardata allcars = new cardata();
dscar = allcars.getcar(rtcar);
lstCallType.DataSource = dscar.Tables[0];
lstCallType.DataTextField =
dscar.Tables[0].Columns["CarID"].ColumnName.ToString();
lstCallType.DataTextField =
dscar.Tables[0].Columns["Make"].ColumnName.ToString();
lstCallType.DataTextField =
dscar.Tables[0].Columns["Color"].ColumnName.ToString();
lstCallType.DataTextField =
dscar.Tables[0].Columns["PetName"].ColumnName.ToString();
lstCallType.DataBind();
lstCallType.SelectedIndex = 0;
}
}
-----------------------------------------
Here is the default.aspx attempts to display the
first items value in the DropDownList in the Web Control:

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

DropDownList b;
b = (DropDownList)CallInformation1.FindControl("lstCallType");
if (b != null)
{
MessageBox.Show("lstCallType was found" +
CallInformation1.Visible.ToString());
}
else
MessageBox.Show("lstCallType not found");

string carname = b.Items[1].Value;
MessageBox.Show("this is the value in the DropDownList" +
carname.ToString());

}
}
 
M

Mark Fitzpatrick

Think more object oriented. Just expose your dropdown list as a property of
your control. Add this to your contrl

public System.Web.UI.WebControls.DropDownList CallType
{
get{ return lstCallType;}
set{lstCallType = value;}
}

Then in the main page use the CallInformation.CallType to get the value.

Also, there is no messagebox in asp.net. It's a windows form object and not
available. Enabling tracing in your web.config and take a look at
Trace.Write() and the trace.axd log page, it's great for debugging.

Hope this helps,
Mark Fitzpatrick

JB said:
Hello

In my ASP.NET application I have Web Control containing a DropDownList
which I populate
from a datset in the Web Controls codebehind page. I then put the Web
Controls on the default.aspx .
From the default.aspx I access the Web Control's id tag to access the
DropDownList in the Web
Control. The FindControl method apparently finds the DropDownList in the
Web Control but an error display when I try to display the value of the
first
item in the DropDownList residing in the Web Control.

How do I access the first item in the DropDownList in the Web Control, the
code is below;

Here is the Web Control:
<%@ Control Language="C#" AutoEventWireup="true"
CodeFile="CallInformation.ascx.cs" Inherits="CallInformation" %>
<asp:Label ID="Label5" runat="server" Text="Call Information"
style="font-weight: 700"></asp:Label><br />
<asp:Label ID="Label1" runat="server" Text="Call Type:"></asp:Label>

<asp:DropDownList ID="lstCallType" runat="server" Height="16px"
Width="200px">
</asp:DropDownList>
-------------------------------------
Here is where I populate the Web Control:

public partial class CallInformation : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
DataSet rtcar = new DataSet();
DataSet dscar = new DataSet();
cardata allcars = new cardata();
dscar = allcars.getcar(rtcar);
lstCallType.DataSource = dscar.Tables[0];
lstCallType.DataTextField =
dscar.Tables[0].Columns["CarID"].ColumnName.ToString();
lstCallType.DataTextField =
dscar.Tables[0].Columns["Make"].ColumnName.ToString();
lstCallType.DataTextField =
dscar.Tables[0].Columns["Color"].ColumnName.ToString();
lstCallType.DataTextField =
dscar.Tables[0].Columns["PetName"].ColumnName.ToString();
lstCallType.DataBind();
lstCallType.SelectedIndex = 0;
}
}
-----------------------------------------
Here is the default.aspx attempts to display the
first items value in the DropDownList in the Web Control:

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

DropDownList b;
b = (DropDownList)CallInformation1.FindControl("lstCallType");
if (b != null)
{
MessageBox.Show("lstCallType was found" +
CallInformation1.Visible.ToString());
}
else
MessageBox.Show("lstCallType not found");

string carname = b.Items[1].Value;
MessageBox.Show("this is the value in the DropDownList" +
carname.ToString());

}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,708
Latest member
SherleneF1

Latest Threads

Top