我有一个int数组作为Web用户控件的属性.如果可能,我想使用以下语法设置该属性内联:
<uc1:mycontrol runat="server" myintarray="1,2,3" />
Run Code Online (Sandbox Code Playgroud)
这将在运行时失败,因为它将期望一个实际的int数组,但正在传递一个字符串.我可以创建myintarray一个字符串并在setter中解析它,但我想知道是否有更优雅的解决方案.
我通常禁用我的ASP.net控件的viewstate,除非我明确知道我将要求他们的视图状态.我发现这可以显着减少HTML生成的页面大小.
这是好习惯吗?什么时候应该启用或禁用?
我有一个用户控件,如果执行某个操作,我想重定向到用户所在的页面,并带有一些额外的查询字符串参数.
所以,如果UserControl.ascx在Home.aspx上,我想重定向到Home.aspx?action = true,如果UserControl.ascx在Profile.aspx上,我想重定向到Profile.aspx?action = true
基本上,在我的UserControl.ascx.cs中,我想获取父页面的URL.我怎么才能得到它?
我正试图围绕asp.net.我有一个很长时间的PHP开发人员的背景,但我现在面临学习asp.net的任务,我遇到了一些麻烦.这可能是因为我试图强迫框架进入它不适合的东西 - 所以我想学习如何"正确的方式".:-)
我的问题是如何在运行时以编程方式向页面添加控件.据我所知,你需要在page_init创建控件,否则它们会在下一个PostBack中消失.但很多时候我遇到的问题是我不知道要在page_init中添加哪些控件,因为它依赖于之前PostBack的值.
一个简单的场景可能是在设计器中添加了下拉控件的表单.下拉列表设置为AutoPostBack.当发生PostBack时,我需要根据下拉控件中的所选值来渲染一个或多个控件,并且最好使这些控件的行为好像它们已经被设计添加(如"回发时,行为正确").
我在这里走错了路吗?
有人可以解释何时使用这些?在许多情况下,它们几乎可以互换.
自定义控件将添加到工具栏,而用户控件(ascx)则不能.用户控件执行时,自定义控件不会在Designer中呈现.除此之外,您如何选择合适的使用方法?
此外,我正在寻找从JavaScript(GetElementById)访问控件的最佳方法.因此,添加客户端支持的正确方向将是很好的.
我将一些自制的Web用户控件放在一个单独的Project"WebControls"中,现在想要从另一个Project中重用它们
我的控制包括:
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="TestControl.ascx.cs" Inherits="WebControls.TestControl" %>
<asp:Label ID="lblTest" runat="server" ></asp:Label>
<asp:TextBox ID="textBox" runat="server" Width="" />
<asp:HiddenField ID="hiddenFieldId" runat="server" />
Run Code Online (Sandbox Code Playgroud)
代码背后:
namespace WebControls
{
public partial class TestControl : System.Web.UI.UserControl
{
public Unit Width
{
get { return textBox.Width; }
set { textBox.Width = value; }
}
public string SelectedId
{
get { return hiddenFieldId.Value; }
set { hiddenFieldId.Value = value; }
}
public string SelectedText
{
get { return textBox.Text; }
set { textBox.Text = value;}
} …Run Code Online (Sandbox Code Playgroud) 如果我在ASP.NET页面中注册用户控件,控件是否会加载到页面?
<%@ Register Src=".." TagName="tag" TagPrefix="pre" %>
即使我没有在页面中使用它来调用它?
<pre:tag id='control123' />
因为即使我没有调用控件,我的部分代码也会被执行,我发现当我进行性能分析时.
我知道有一种方法可以强制asp web用户控件(ascx)使用静态ID,因此它们不会附加所有附加的命名垃圾......但是有没有办法对"name"属性做同样的事情?具体来说,在这种情况下,对于DDL的?JQuery需要一个特定的名称,我需要阻止.NET这样做.
我有一个Web用户控件名称作为Chart Control,我在Chart Control中有一个下拉列表我想填充DropDown Chart Control of List Control,如下所示:
<UC:ChartControl ID="abc" runat="server">
<ChartDropDownItems>
<asp:ListItem Text="Graph_Amount_Items_Sold" Value="0"></asp:ListItem>
<asp:ListItem Text="Graph_Ranking" Value="1"></asp:ListItem>
<asp:ListItem Text="Graph_Share_Amount_Items_Sold" Value="2"></asp:ListItem>
<asp:ListItem Text="Graph_Share_Unit_items_Sold" Value="3" Selected="True"></asp:ListItem>
<asp:ListItem Text="Graph_Unit_items_Sold" Value="4"></asp:ListItem>
</ChartDropDownItems>
</UC:ChartControl>
Run Code Online (Sandbox Code Playgroud)
在.cs代码中
[DefaultProperty("ChartDropDownItems"),
ParseChildren(true, "ChartDropDownItems"), PersistChildren(false),
ToolboxData("<{0}:ChartControl runat=\"server\"> </{0}:ChartControl>")]
public partial class ChartControl : System.Web.UI.UserControl
{
private List<ListItem> lst;
[Browsable(true)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
[PersistenceMode(PersistenceMode.InnerProperty)]
public List<ListItem> ChartDropDownItems
{
set
{
lst = value;
Bind_ddChartChange();
}
}
private void Bind_ddChartChange()
{
if (lst != null)
{
ddChartChange.DataSource = lst;
ddChartChange.DataTextField = "Text";
ddChartChange.DataValueField = …Run Code Online (Sandbox Code Playgroud) 我在asp.net中有一个Web用户控件 - 一个键盘.它具有所有数字的按钮,用于显示输入的文本框以及清除和提交按钮.用户控件位于AJAX更新面板中,因此可以轻松显示/隐藏.(应用程序用于平板电脑浏览器)我需要在AJAX更新面板内的用户控件中添加一个委托给subMitButton ClickEventHandler.有什么建议?它甚至可能吗?我不想使用平板电脑的软数字键盘,因为它是大的方式.我可以分开我的用户控件并在我的网站中使用它的代码,但我宁愿按预期使用用户控件.
ASP.NET中的用户控件和自定义控件之间有什么区别
很惊讶,我自己也找不到这个,但无论如何.假设我使用我的网络用户控件,如下所示:
<myprefix:mytag userid="4" runat="server">Some fancy text</myprefix:mytag>
Run Code Online (Sandbox Code Playgroud)
如何从代码隐藏("一些花哨的文本")中访问标签内的文本?期待它通过this.Text,this.Value或类似的东西暴露.
编辑:我甚至在尝试使用它的页面上收到以下警告: 元素'mytag'的开始和结束标记之间不允许有内容.
EDIT2:
public partial class mytag: UserControl
{
public int ItemID { get; set; }
protected void Page_Load(object sender, EventArgs e)
{
}
}
Run Code Online (Sandbox Code Playgroud) 我有一个Web用户控件,在客户端上生成以下html.
我想使用JavaScript引用特定的RadioButton控件.
我不知道如何做到这一点因为asp.net生成了RadioButton ID.
例如,我给出RadioButton ID = 1_RadioButton
asp.net生成一个ID = ctl00_ContentPlaceHolder1_Material_1_RadioButton
这个javascript代码怎么能找到Radio Button控件?
<script type="text/javascript">
$(document).ready(function() {
if (document.getElementById("1_RadioButton").checked) {
$("#1_other").hide();
}
});
</script>
Run Code Online (Sandbox Code Playgroud)
以下是asp.net为客户端生成的内容.
<input id="ctl00_ContentPlaceHolder1_Material_1_RadioButton" type="radio" name="ctl00$ContentPlaceHolder1$Material$Academic" value="1_RadioButton" onclick="javascript:$('#1_other').show('fast');" />
<label for="ctl00_ContentPlaceHolder1_Material_1_RadioButton">Yes</label>
<input id="ctl00_ContentPlaceHolder1_Material_2_RadioButton" type="radio" name="ctl00$ContentPlaceHolder1$Material$Academic" value="2_RadioButton" checked="checked" onclick="javascript:$('#1_other').hide('fast');" />
<label for="ctl00_ContentPlaceHolder1_Material_2_RadioButton">No</label>
<input id="ctl00_ContentPlaceHolder1_Material_3_RadioButton" type="radio" name="ctl00$ContentPlaceHolder1$Material$Academic" value="3_RadioButton" onclick="javascript:$('#1_other').hide('fast');" />
<label for="ctl00_ContentPlaceHolder1_Material_3_RadioButton">Uncertain</label>
<div id='1_other'>
<input name="ctl00$ContentPlaceHolder1$Material$4_TextBox" type="text" value="bbb chabng" id="ctl00_ContentPlaceHolder1_Material_4_TextBox" />
</div>
Run Code Online (Sandbox Code Playgroud) asp.net ×13
c# ×4
ascx ×2
.net ×1
client-side ×1
controls ×1
javascript ×1
jquery ×1
webforms ×1