通过webform更改DropDownList中可见的属性

时间:2014-03-29 01:30:07

标签: asp.net vb.net

我在控件里面有一个用户控件有一个组合框,按钮,DropDownList等在我的代码中添加引用:

   <%@ Register Src="~/UserControls/UCCom.ascx" TagName="UCCom" TagPrefix="ucc" %>

   <ucc:UCCom ID="UCCom" runat="server" MuestraFiltros="true" />

在后面的代码中我有一个对象,例如:这段代码:

 Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
    Dim ctrx As New UCCom
 End 

但是当我尝试修改某些组合框或文本框时,我看到一条消息: 无法访问受保护的字段组合框

用户控件中的代码:

   <td style="width: 100px">
    <asp:DropDownList ID="CboUsersCom" runat="server" DataTextField="name" AutoPostBack="true"
     DataValueField="cve_user" ClientIDMode="Static" Enabled="false" Width="100px">
    </asp:DropDownList>                                   
    </td>

感谢您的评论。

2 个答案:

答案 0 :(得分:1)

在您的用户控件设计器页面中,即usercontrol.ascx.designer将组合框的访问修饰符从'protected'更改为'public',如下所示。然后,您可以在全局其他页面中访问该控件。

示例代码演示文本框设置为'public'。

“---------------------------------------------- -------------------------------- “ '这段代码是由一个工具生成的。 “ '对此文件的更改可能会导致错误的行为,如果失败则会丢失 '代码重新生成。 “ “------------------------------------------------- -----------------------------

Option Strict On
Option Explicit On


Partial Public Class UserControl

'''<summary>
'''txtDateTime control.
'''</summary>
'''<remarks>
'''Auto-generated field.
'''To modify move field declaration from designer file to code-behind file.
'''</remarks>
Public WithEvents txtDateTime As Global.System.Web.UI.WebControls.TextBox

'''<summary>
'''btnCalendar control.
'''</summary>
'''<remarks>
'''Auto-generated field.
'''To modify move field declaration from designer file to code-behind file.
'''</remarks>
Protected WithEvents btnCalendar As Global.System.Web.UI.WebControls.ImageButton

'''<summary>
'''ajaxCalendar control.
'''</summary>
'''<remarks>
'''Auto-generated field.
'''To modify move field declaration from designer file to code-behind file.
'''</remarks>
Protected WithEvents ajaxCalendar As Global.AjaxControlToolkit.CalendarExtender
End Class

答案 1 :(得分:0)

Page_Load方法中,您可以执行以下操作:

Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
    Dim dropDownList As DropDownList= TryCast(UCCom.FindControl("CboUsersCom"), DropDownList)
    dropDownList.Enabled = True
End