【问题标题】:To get values from custom control从自定义控件中获取值
【发布时间】:2014-04-09 22:21:07
【问题描述】:

在下面的代码中,我有文本框、下拉列表、复选框等用户控件,并将其绑定到数据列表。现在我已将控件引用到 .aspx 网页,现在我的目标是获取 .aspx 页面中自定义控件的值。请帮助我做到这一点。我的目标是从 .aspx 中的usercontrols 获取文本框、下拉列表、复选框的值。

GmatField.ascx

<asp:TextBox ID="txtField" runat="server" width="200Px" CssClass="style22"  ></asp:TextBox>
<asp:DropDownList ID="cbField" runat="server" width="200Px" >
</asp:DropDownList>
<asp:CheckBox ID="chField" runat="server" width="200Px"  />

GmatField.ascx

<%@ Register TagPrefix="gmat" TagName="FieldCont" Src="~/Search/GmatField.ascx" %>

<asp:DataList ID="dlFields" runat="server"  Height="100px" 
    Width="50px" BorderColor="Beige">
    <ItemTemplate>
        <gmat:FieldCont ID="gmatFieldCont" runat="server" />
    </ItemTemplate>
</asp:DataList>

NewDocument.aspx

<%@ Register TagPrefix="gmat" TagName="GmatFieldsControl" Src="~/Search/GmatFields.ascx" %>
    <gmat:GmatFieldsControl ID="gmatFieldsContr" runat="server" />

【问题讨论】:

  • 不太清楚你想要什么我的朋友..请解释更多
  • 我不知道你在说什么。
  • @ Karim AG 我在 GmatField.ascx 中有文本框、下拉列表、复选框等控件。现在我想在 NewDocument.aspx 中获取这些值

标签: c# asp.net


【解决方案1】:

简单。

创建一个从文本框或 selectedItem 下拉列表中获取值的公共属性 然后从实现该用户控件的页面中,您可以访问该属性:

// usercontrol:
public string TxtField
{
   get 
   {
      return this.txtField.Text;
   }
}

// from the ASPX page that implements the usercontrol:

string txtFieldValue = this.gmatFieldsContr.TxtField;

【讨论】:

  • 这表明您是否缺少 this.gmatFieldsContr.TxtField 中的任何指令;
  • 确保您引用的是用户控件的正确名称。这是您给出的想法/描述,即用户控件位于您尝试引用的同一页面上。
猜你喜欢
  • 2012-10-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-20
  • 1970-01-01
相关资源
最近更新 更多