我有几个用户控件,包含下拉列表,各种验证控件和SQL数据源控件。我想知道是否有可能在设计时使数据源控件不可见。
当我使用这些控件来构建各种表单时,数据源控件显示在设计视图中并占用空间和它的烦恼。
<asp:DropDownList ID="ddlAccounts" runat="server" DataSourceID="dsAccounts"
DataTextField="name" DataValueField="accountId"
AppendDataBoundItems="True">
<asp:ListItem Value="NOT SET">- Select an account -</asp:ListItem>
</asp:DropDownList>
<asp:RequiredFieldValidator ID="rfvAccounts" runat="server"
ControlToValidate="ddlAccounts" CssClass="ValidationError" Display="Dynamic"
ErrorMessage="!" InitialValue="NOT SET" ToolTip="You must select an account."></asp:RequiredFieldValidator>
<asp:SqlDataSource ID="dsAccounts" runat="server"
ConnectionString="<%$ ConnectionStrings:MySqlConnectionString %>"
ProviderName="MySql.Data.MySqlClient"
SelectCommandType="StoredProcedure"
SelectCommand="AccountSelector_Select">
</asp:SqlDataSource>
答案 0 :(得分:3)
我使用这种方法对跨度进行了更多控制:
<span style="display: none;" id="obsPopUp" runat="server">
<asp:SqlDataSource ID="MySqlDataSource" runat="server" ...>
</asp:SqlDataSource>
</span>
在加载事件中改变跨度:
Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
obsPopUp.Attributes("style") = "display: inherits;"
End Sub
工作正常!!!
答案 1 :(得分:2)
好吧,您可以通过代码隐藏编程方式添加它,然后您就不会在设计器中看到它。
SqlDataSource s = new SqlDataSource();
s.ID = "mySqlSourceControl";
Page.Controls.Add(s);
s.ConnectionString = ConfigurationManager.ConnectionStrings["MyDatabase"].ConnectionString;
s.SelectCommand = "AccountSelector_Select";
s.SelectCommandType = "StoredProcedure";
s.Select(DataSourceSelectArguments.Empty);
答案 2 :(得分:0)
如果您仍想利用设计器工具,将其包装在显示器中:none span似乎运行良好:
<span style="display: none;">
<asp:SqlDataSource ID="MySqlDataSource" runat="server"
...>
</asp:SqlDataSource>
</span>