Javascript Dropdownlist“;启用“真”;Asp.net不工作

Javascript Dropdownlist“;启用“真”;Asp.net不工作,javascript,c#,jquery,asp.net,Javascript,C#,Jquery,Asp.net,我已经设置了下拉启用设置为假在一个按钮点击,我将设置启用=“真”是不工作的页面加载 这是我的aspx <asp:DropDownList ID="ddlJournal" runat="server" OnSelectedIndexChanged="ddlJournal_SelectionChanged" AutoPostBack="true" CssClass="drop" /> 以下是我的点击事件: protected void btnTemplate_click(ob

我已经设置了下拉启用设置为假在一个按钮点击,我将设置启用=“真”是不工作的页面加载

这是我的aspx

 <asp:DropDownList ID="ddlJournal" runat="server" OnSelectedIndexChanged="ddlJournal_SelectionChanged"  AutoPostBack="true" CssClass="drop" />

以下是我的点击事件:

 protected void btnTemplate_click(object sender, EventArgs e)
    {
        check.Value = "1";
        Page.ClientScript.RegisterStartupScript(this.GetType(), "CallMyFunction", "Load_functions()", true);


        //txtAddJournal.Attributes.Add("Style", "display:block");
        //btnUpload.Attributes.Add("Style", "display:block");
        //if (fileuploader.HasFile)
        //{
        try
        {
            string Filename = Path.GetFileName(fileuploader.FileName);
            //fileuploader.SaveAs(Server.MapPath("~/") + Filename);
            // fileuploader.SaveAs(Server.MapPath("D:\\Req Sep16\\") + Filename);
            OleDbConnection myconnectionini = default(OleDbConnection);
            OleDbDataAdapter mycommandini = default(OleDbDataAdapter);
            //if (fileuploader.PostedFile.FileName.EndsWith(".xls") == false & fileuploader.PostedFile.FileName.EndsWith(".xlsx") == false)
            //{
            //    // lbl_Error.Text = "Upload only excel format";
            //    Response.Write(@"<script language='javascript'>alert('Upload only excel format');</script>");
            //    return;

            //}
            //else
            //{
            gvDetails.DataSource = null;

            string pathToSave = HttpContext.Current.Server.MapPath("~/UploadFiles/") + "Copy of Database_HBM";
            //fileuploader.PostedFile.SaveAs(pathToSave);
            //strFilePath = "D:\\Files\\" + fileuploader.FileName;

            string constrini = "provider=Microsoft.Jet.OLEDB.4.0;data source=" + pathToSave + ";Extended Properties=Excel 8.0;";
            DataSet ds = new DataSet();
            // DataTable dt = new DataTable();        
            myconnectionini = new OleDbConnection(constrini);
            mycommandini = new OleDbDataAdapter("select * from [Sheet1$]", myconnectionini);
            ds = new DataSet();
            mycommandini.Fill(ds);

            gvDetails.DataSource = ds.Tables[0];
            gvDetails.DataBind();

            ddlJournal.SelectedIndex = -1;
            ddlJournal.Enabled = false;

            //ddlJournal.Attributes.Add("disabled", "disabled");


            //}

        }
        catch (Exception ex)
        {
            string msg = ex.Message;
        }

        //}
    }
protectedvoidbtntemplate\u单击(对象发送方,事件参数e)
{
选中。Value=“1”;
Page.ClientScript.RegisterStartupScript(this.GetType(),“CallMyFunction”,“Load_functions()”,true);
//添加(“样式”,“显示:块”);
//添加(“样式”,“显示:块”);
//if(fileuploader.HasFile)
//{
尝试
{
字符串Filename=Path.GetFileName(fileuploader.Filename);
//fileuploader.SaveAs(Server.MapPath(“~/”)+文件名);
//fileuploader.SaveAs(Server.MapPath(“D:\\Req Sep16\\”)+文件名);
OleDbConnection myconnectionini=默认值(OleDbConnection);
OleDbDataAdapter mycommandini=默认值(OleDbDataAdapter);
//if(fileuploader.PostedFile.FileName.EndsWith(“.xls”)==false&fileuploader.PostedFile.FileName.EndsWith(“.xlsx”)==false)
//{
////lbl_Error.Text=“仅上传excel格式”;
//响应。写入(@“警报(‘仅上传excel格式’);”;
//返回;
//}
//否则
//{
gvDetails.DataSource=null;
字符串pathToSave=HttpContext.Current.Server.MapPath(“~/UploadFiles/”)+“数据库副本”\u HBM”;
//fileuploader.PostedFile.SaveAs(路径保存);
//strFilePath=“D:\\Files\\”+fileuploader.FileName;
string constrini=“provider=Microsoft.Jet.OLEDB.4.0;数据源=“+pathToSave+”;扩展属性=excel8.0;”;
数据集ds=新数据集();
//DataTable dt=新的DataTable();
myconnectionini=新的OLEDB连接(constrini);
mycommandini=新的OleDbDataAdapter(“从[Sheet1$]中选择*”,myconnectionini);
ds=新数据集();
支原体填充物(ds);
gvDetails.DataSource=ds.Tables[0];
gvDetails.DataBind();
ddlJournal.SelectedIndex=-1;
ddlJournal.Enabled=false;
//ddlJournal.Attributes.Add(“disabled”、“disabled”);
//}
}
捕获(例外情况除外)
{
字符串msg=例如消息;
}
//}
}
我的页面加载事件是

protected void Page_Load(object sender, EventArgs e)
    {
        Page.ClientScript.RegisterStartupScript(this.GetType(), "Grid", "headerLock();", true);

        // ScriptManager.RegisterStartupScript(Page, this.GetType(), "Key", "<script>headerLock();</script>", true );
        if (!IsPostBack)
        {
            Bindddl();
            BindGrid(null);
            ddlJournal.Enabled = true;

        }
        else
        {              
            ddlJournal.Enabled = true;

        }

    }
受保护的无效页面加载(对象发送方,事件参数e)
{
Page.ClientScript.RegisterStartupScript(this.GetType(),“Grid”,“headerLock();”,true);
//ScriptManager.RegisterStartupScript(第页,this.GetType(),“Key”,“headerLock();”,true);
如果(!IsPostBack)
{
binddl();
BindGrid(空);
ddlJournal.Enabled=true;
}
其他的
{              
ddlJournal.Enabled=true;
}
}
按钮:

<asp:Button ID="btnUpload" runat="server" Text="Template 1" OnClientClick="return Validate();"
                                    OnClick="btnTemplate_click" CssClass="btn" />

但我的下拉列表仍然是禁用的

建议我找到解决办法
提前感谢

您可以通过其控件将下拉列表设置为Enabled false,如下所示

<asp:DropDownList ID="ddlJournal" runat="server" OnSelectedIndexChanged="ddlJournal_SelectionChanged"  AutoPostBack="true" CssClass="drop" Enabled="false"/>

剩下的代码应该可以正常工作


请标记它有助于理解Page_Load()方法中的
if-else
条件是罪魁祸首。无论发生什么情况,您总是将
ddlJournal.Enabled=true
。似乎您没有正确理解
IsPostBack
的概念<当
IsPostBack
为true时,应该禁用code>ddlJournal,因为这正是您想要的。否则,应该启用它

这是关于
IsPostBack
是什么的一个非常简洁的解释:

在asp.net页上的控件执行操作时触发的事件中回发。例如,当您单击按钮时,页面上的数据将被发回服务器进行处理。IsPostback通常用于页面加载事件,以检测页面是否是由于页面上的控件请求的发回而生成的,或者页面是否是第一次加载的

[来自的评论]

因此,基于此,您应该按照以下方式更改代码:

protected void Page_Load(object sender, EventArgs e)
{
    Page.ClientScript.RegisterStartupScript(this.GetType(), "Grid", "headerLock();", true);

    if (!IsPostBack)
    {
        //When IsPostBack is false, ddlJournal should be enabled
        Bindddl();
        BindGrid(null);
        ddlJournal.Enabled = true;

    }
    else
    {
        //Else, IsPostBack is true, so, ddlJournal should be disabled              
        ddlJournal.Enabled = false;

    }

}
另外,在
btnTemplate\u click()
方法中不需要此选项,因为您在页面加载时执行此操作:

ddlJournal.Enabled = false;

您想让下拉列表隐藏起来还是让其他内容清楚地显示出来?我想在触发按钮单击事件时将Enable设置为false,否则它将始终启用true@Bala我发现很难100%理解您想要什么,您是希望整个ddl为enable false,因此无法单击它,还是希望ListItems为enable false,因此,您可以查看项目,但不能单击它们?只有当按钮单击ddl集以启用false时,ddl才会设置为true。然后我按f5 ie,页面加载我的ddl不启用true,但它仍然是启用false。我现在能做什么?这很让人困惑。为什么在
if
条件和
else
条件下都有
ddlJournal.Enabled=true
?在
else
条件下,它不是应该为false吗?不,我不想设置false默认值。仅在该按钮单击中启用false,因此您可以为按钮单击事件编写代码,您必须在if-else条件中检查该事件的条件。按钮的HTML相关代码在哪里。请把它也贴上