Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Assign Label Text from Variable

Status
Not open for further replies.

dhrehor

IS-IT--Management
Jun 16, 2005
23
US
Wondering how I do this. I have a SQL query that pulls three fields from the database.

SELECT quarter, startdate, enddate from quarters

this then populates a drop down box with the quarter.

I want to put two labels next to the drop down and have the text of the two labels populate from the startdate and enddate variables.

Tried this:

<asp:Label ID="Label1" runat="server" Text="@startdate"></asp:Label>


But obviously I only get the text "@startdate" printed.

Thanks

Don
 
Are you using a DataSet to fill the dropdown? If so, when the selected index is changed of the DropDown, you can search the dataset for that selection (in a datarow array or datarowview) in the SelectedIndexChanged event and then you'd set the labels using Label1.Text = mydataRow["startdate"].
 
Also:

I have the drop down set to autopostback and I want the startdate and enddate fields to populate each time the dropdown is changed.

Hope that helps.

Thanks again

Don
 
I am not using a dataset. Here is the whole code.

<%@ Page Language="C#" MasterPageFile="~/GCSO_MasterPage.master" Title="Untitled Page" %>

<script runat="server">

protected void Page_Load(object sender, EventArgs e)
{

}
</script>

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
Report Screen<br />
<br />
<asp:Label ID="officerName" runat="server" Text="Officer Name"></asp:Label>: &nbsp;<asp:DropDownList
ID="soNumberDropDown" runat="server" DataSourceID="sonumber" DataTextField="sonumber"
DataValueField="sonumber">
</asp:DropDownList><asp:SqlDataSource ID="sonumber" runat="server" ConnectionString="<%$ ConnectionStrings:OfficerFullName %>"
SelectCommand="SELECT [sonumber] FROM [officers]"></asp:SqlDataSource>
<br />
<asp:Label ID="quarterLabel" runat="server" ></asp:Label>:
<asp:DropDownList ID="quarterDropDown" runat="server" AutoPostBack="True" DataSourceID="quarterlist"
DataTextField="quarter" DataValueField="quarter">
</asp:DropDownList>&nbsp;
<asp:Label ID="Label1" runat="server" Text="[startdate]"></asp:Label>
<asp:SqlDataSource ID="quarterlist" runat="server" ConnectionString="<%$ ConnectionStrings:OfficerFullName %>"
SelectCommand="SELECT [quarter], [startdate], [enddate] FROM [quarters]"></asp:SqlDataSource>
&nbsp;&nbsp;&nbsp;<br />
<br />
&nbsp;&nbsp;
<br />
test end&nbsp;
</asp:Content>

 
Create a SqlDataSource and FormView for each Label.

Code:
<asp:SqlDataSource id="startdate" runat="server" ConnectionString="<%$ ConnectionStrings:OfficerFullName %>" SelectCommand="SELECT [startdate] from [quarters] where [quarter] = @quarter">
<SelectParameters>
  <asp:ControlParameter Name="quarter" ControlID="soNumberDropDown" PropertyName="SelectedValue" />
</SelectParameters>
</asp:SqlDataSource>

You need to wrap a label in a FormView tag so you can bind the FormView to your datasource.



<asp:FormView ID="FormView1" runat="server" DataSourceID="startdate">
<asp:Label ID="Label1" runat="server" Text='<%# Eval("startdate")%>'></asp:Label>


</asp:FormView>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top