krishb591993
krishb591993

Reputation: 303

Change text in Label on selecting new item from dropdownlist

I want to change the label's text as soon as I select the item in dropdownlist. Here's my code

protected void Page_Load(object sender, EventArgs e)
{
    tbusrcompdate.Attributes.Add("readonly", "readonly");
    if (!IsPostBack)
    {
        //populating ddlTaskId
        string query = "select * from Compliance_Tracker.dbo.tasklistManager where STATUS='1';";
        string columnname = "TASK ID";
        string datavaluefield = "TASK ID";
        obj7.PopulateCombo(ddlTaskID, query, columnname, datavaluefield);
        //default values in labels
        string query1 = "select top 1 [DESC] from Compliance_Tracker.dbo.tasklistManager where STATUS = '1';";
        lblDescOutput.Text = obj7.ExecuteScalar(query1).ToString();
        string query2 = "select top 1 FREQUENCY from Compliance_Tracker.dbo.tasklistManager where STATUS = '1';";
        lblFrequencyOutput.Text = obj7.ExecuteScalar(query2).ToString();

    }
}
protected void UploadButton_Click(object sender, EventArgs e)
{
    StatusLabel.Text = "Upload status:";
    if (FileUploadControl.HasFile)
    {
        try
        {

            string filename = Path.GetFileName(FileUploadControl.FileName);
            FileUploadControl.SaveAs(Server.MapPath("~/") + filename);
            //string test = Server.MapPath("~/").ToString() + filename.ToString();
            StatusLabel.Text = "Upload status: File uploaded!";

        }
        catch (Exception ex)
        {
            StatusLabel.Text = "Upload status: The file could not be uploaded. The following error occured: " + ex.Message;
        }
    }
}
 protected void ddlTaskID_TextChanged(object sender, EventArgs e)
{

}
protected void ddlTaskID_SelectedIndexChanged(object sender, EventArgs e)
{
    string selectedtext = ddlTaskID.SelectedItem.Text;
    string query = "select [DESC] from Compliance_Tracker.dbo.tasklistManager where Compliance_Tracker.dbo.tasklistManager.[TASK ID] ='" + selectedtext + "';";
    lblDescOutput.Text = obj7.ExecuteScalar(query).ToString();
}

As in the code above I have tried to do it using the SelectIndexChanged and TextChanged events . But, I finally get it on pressing the upload button but I want the label's text to be changed as soon as the value of dropdown list is selected. Here' my html

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder3" Runat="Server">
<form id="Form1" runat="server" style="border-style: none; border-width: inherit; border-color: #008000; background-color:#33CC33; height:588px; width:669px; background-image: url('new.jpg'); background-repeat: no-repeat;" method="post" enctype="multipart/form-data">
    <h1 style="height: 34px">
        TRANSACTION MANAGER TABLE
    </h1>
    <asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server"></asp:ToolkitScriptManager>
     <table id="table1" style="border-style: none; height:188px; width:549px; margin-left:30px; border-collapse: collapse; margin-top: 0px;">
        <tr>
            <td style="width: 137px; height:30px;"><asp:Label ID="lblTaskID" runat="server" Text="TASK ID" Width="70px"></asp:Label></td>
            <td style="height:30px"><asp:DropDownList ID="ddlTaskID" runat="server" Height="20px" style="margin-left: 50px" Width="126px" OnTextChanged="ddlTaskID_TextChanged" OnSelectedIndexChanged="ddlTaskID_SelectedIndexChanged" ></asp:DropDownList></td>
        </tr>
        <tr>
            <td style="width:137px; height:30px;"><asp:Label ID="lblDesc" runat="server" Text="DESC" Width="70px"></asp:Label></td>
            <td style ="height:30px"><asp:Label ID="lblDescOutput" runat="server" style="margin-left:50px" Width="126px" Height="20px"></asp:Label></td>
        </tr>
        <tr>
            <td style="width: 137px; height:30px;"><asp:Label ID="lblFrequency" runat="server" Text="FREQUENCY" Width="132px"></asp:Label></td>
            <td style="height:30px"><asp:Label ID="lblFrequencyOutput" runat="server" style="margin-left:50px" Width="126px" Height="20px"></asp:Label></td>
        </tr>
        <tr>
            <td style="width: 137px; height:30px;"><asp:Label ID="lblDocpath" runat="server" Text="DOC PATH" Width="107px"></asp:Label></td>
            <td style="height:30px">
               <asp:FileUpload id="FileUploadControl" runat="server" />
               <asp:Button runat="server" id="UploadButton" text="Upload" onclick="UploadButton_Click" />
               <br /><br />
               <asp:Label runat="server" id="StatusLabel" text="Upload status: " />
            </td>
        </tr>
        <tr>
            <td style="width: 137px; height:30px;"><asp:Label ID="lblusrcompdate" runat="server" Text="USER COMPLETE DATE" Width="147px"></asp:Label></td>
            <td style="height:30px"><asp:TextBox ID="tbusrcompdate" runat="server"></asp:TextBox>

                <asp:ImageButton ID ="imgbtncalender" runat="server" ImageUrl="~/home-calendar-button.jpg" Height="17px" style="margin-left: 16px" Width="16px" />
                <asp:CalendarExtender ID="CalendarExtender1" runat="server" TargetControlID="tbusrcompdate" Format="dd/MM/yyyy" PopupButtonID="imgbtncalender"></asp:CalendarExtender>
            </td>
        </tr>
    </table><br />
    <asp:Button ID="btnAdd" runat="server" Text="ADD" Height="27px" Width="80px" style="margin-left:235px"/>
</form>

The values in the labels are to be fetched from the tasklistManager table using the TASK ID drop down list the code for that as shown above is

 string query1 = "select top 1 [DESC] from Compliance_Tracker.dbo.tasklistManager where STATUS = '1';";
        lblDescOutput.Text = obj7.ExecuteScalar(query1).ToString();
        string query2 = "select top 1 FREQUENCY from Compliance_Tracker.dbo.tasklistManager where STATUS = '1';";
        lblFrequencyOutput.Text = obj7.ExecuteScalar(query2).ToString();

Upvotes: 0

Views: 3337

Answers (1)

Andrei
Andrei

Reputation: 56688

The only problem with your code is that DropDownList does not fire a postback when its selection changes, so the corresponding event is not triggered. To fix this, simply add AutoPostBack="true" attribute to it:

<asp:DropDownList ID="ddlTaskID" runat="server" AutoPostBack="true" ...

Also handling both TextChanged and SelectedIndexChanged events might not be a good idea. Pick one, preferably the latter.

Upvotes: 4

Related Questions