从下拉列表中选择新项目时,更改标签中的文本

本文关键字:标签 文本 下拉列表 选择 新项目 | 更新日期: 2023-09-27 18:11:17

我想在下拉列表中选择项目后立即更改标签的文本。这是我的代码

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

在上面的代码中,我试图使用SelectIndexChanged和TextChanged事件来做到这一点。但是,我终于得到它按下上传按钮,但我希望标签的文本被改变,只要下拉列表的值被选中。这是我的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>

标签中的值将使用TASK ID下拉列表从tasklistManager表中获取,上面所示的代码为

 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();

从下拉列表中选择新项目时,更改标签中的文本

代码的唯一问题是DropDownList在其选择更改时不触发回发,因此没有触发相应的事件。要解决这个问题,只需添加AutoPostBack="true"属性:

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

同时处理TextChangedSelectedIndexChanged事件可能不是一个好主意。选择一个,最好是后者。