在Page_Load上禁用ListBox

时间:2015-10-14 10:16:28

标签: javascript asp.net telerik

我正在尝试在加载我的网络表单时禁用两个列表框,但列表框没有禁用。我尝试了下面的代码,但它仍然没有处理我的情况。请让我知道我哪里出错了。

设计师代码

<telerik:RadListBox ID="lbSelectedDepartmentContacts" runat="server" Width="220px" AutoPostBackOnTransfer="true" AutoPostBackOnReorder="true" AutoPostBackOnDelete="true" CheckBoxes="false" AllowTransfer="true" AllowTransferOnDoubleClick="true" TransferToID="lbDepartmentContacts"
                                                                            Height="276px" SelectionMode="Single" AllowReorder="True">
                                                                            <ButtonSettings ShowTransfer="false" ShowTransferAll="false" ReorderButtons="Common" ShowReorder="true" />
                                                                        </telerik:RadListBox>
                                                                    </td>
                                                                </tr>
                                                                <tr>
                                                                    <td align="right">
                                                                        <telerik:RadListBox ID="lbDepartmentContacts" runat="server" Width="200px" CheckBoxes="false" AutoPostBackOnTransfer="true" AllowTransfer="true" AllowTransferOnDoubleClick="true" TransferToID="lbSelectedDepartmentContacts"
                                                                            Height="250px" SelectionMode="Single">
                                                                            <ButtonSettings TransferButtons="Common" ShowTransfer="false" ShowTransferAll="false" />
                                                                        </telerik:RadListBox>

BACKCODE

public void DisableControls()
        {
 System.Text.StringBuilder sb = new System.Text.StringBuilder();
            sb.Append(@"<script language='javascript'>function DisableControls(){");
            sb.Append(@"var lbl = document.getElementById('lbSelectedDepartmentContacts').disabled = true;");
            sb.Append(@" var Txt = document.getElementById('txtDepartmentCategoryContactsFilter').disabled = true;");
            sb.Append(@" var btn = document.getElementById('RadButton1').disabled = true;");

            sb.Append("}</script>");

            if (!ClientScript.IsClientScriptBlockRegistered("JSScriptBlock"))
            {
                ClientScript.RegisterClientScriptBlock(this.GetType(), "JSScriptBlock",
                sb.ToString());
            }

            string funcCall = "<script language='javascript'>DisableControls();</script>";

            if (!ClientScript.IsStartupScriptRegistered("JSScript"))
            {
                ClientScript.RegisterStartupScript(this.GetType(), "JSScript", funcCall);
            } 
}

也试过:

Page.ClientScript.RegisterStartupScript(this.GetType(), "text", "DepartmentLoad();", true);

JAVASCRIPT CODE

function DepartmentLoad(){
            document.getElementById("lbSelectedDepartmentContacts").disabled = true;
            document.getElementById("lbDepartmentContacts").disabled = true;
            document.getElementById("txtDepartmentCategoryContactsFilter").disabled = true;
            ducument.getElementById("RadButton1").disabled = true;}

2 个答案:

答案 0 :(得分:1)

仅限C#禁用2个列表框...

删除或注释掉现有的DisableControls()方法,然后添加以下内容...

protected void Page_Load(object sender, EventArgs e)
{
    if (Page.IsPostBack) return;
    DisableControls();
}

private void DisableControls()
{
    lbSelectedDepartmentContacts.Enabled = lbDepartmentContacts.Enabled = false;
}

答案 1 :(得分:0)

您通常应该:

在客户端上似乎没有set_enabled()或set_disabled()方法(至少在撰写本文时),所以JS似乎不可能这样做,所以你应该选择另一个告诉你的答案使用服务器代码。