如何从main.aspx页面的代码后面访问usercontrol.ascx中的字段集? [英] how to access fieldset in usercontrol.ascx from codebehind of the main.aspx page?

查看:84
本文介绍了如何从main.aspx页面的代码后面访问usercontrol.ascx中的字段集?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

实际上,我正在使用asp.net和c#开发模板.我有一个面板,其中在我的用户控制页面中包含2个字段集.我想从main.aspx页面后面的代码访问这2个字段集,这意味着当用户单击main.aspx页面上的link1时,面板将刷新并显示fielset1以及当用户单击link2时,面板将刷新,并且面板显示fieldset2.使用updatepanel对页面进行部分刷新.你能指导我如何解决这个问题吗?
感谢您的考虑.

actually i''m developing template using asp.net and c#. i have one panel which contain 2 fieldset in my usercontrol page. i wand to access these 2 fieldset from the code behind of the main.aspx page, which means when the user click on the link1 at the main.aspx page the panel will be refresh and shows the fielset1 and when the user click on the link2, the panel will be refresh and panel shows the fieldset2. for the partial refreshing of the page i''m using the updatepanel. could you please guide me how to get ride of this problem.
appreciate your consideration.

推荐答案

在default.aspx中将用户控件注册为:

Register the user control in default.aspx as:

<%@ Register TagPrefix="so" TagName="UserControl" Src="~/myUserControl.ascx" %>



用户控件的实例创建为:



Instance of the user control is created as:

<so:UserControl ID="UserControl1" runat="server" Title="Control 1" OnRemoving="UserControl1_Removing" />



在后面的代码中,将用户控件evnt处理为:



In code behind, handle the user control evnt as:

protected void UserControl1_Removing(object sender, EventArgs e)
{
    myUserControl ctrl = (myUserControl)sender;
    ctrl.Visible = false;
}


我已经解决了这个问题.
我在我的usercontrol页面上放置了一个HiddenField变量:
i have fixed the problem.
i have put one HiddenField variable at my usercontrol page:
<asp:hiddenfield id="hid_choosingField" value="" runat="server" xmlns:asp="#unknown" />


然后我已经从aspx.cs页面访问并更改了它:


then i have accessed and changed it from aspx.cs page:

Control hidField = WebUserControl31.FindControl("hid_choosingField");
    HiddenField ucHidField = (HiddenField)hidField;
    ucHidField.Value = "1";



然后我在ascx页上放置了一个if条件,以检查什么是HiddenField值,并根据该值显示相关的字段集:



then i have put a if condition at the ascx page to check what is the HiddenField value and base on the value i show the related fieldset:

<% if (hid_choosingField.Value == "1")
   { 
%>
    <fieldset id="uc3Fieldset1" style=" height:350px;">
    <legend>New Module Details</legend>

        <asp:label id="Label2" runat="server" forecolor="blue" text="This is User Control 3 Panel 1 Fieldset 1" />
    </fieldset>

   }
   else if (hid_choosingField.Value == "2")
   { 

    <fieldset style=" height:350px;">
    <legend>New Module Details</legend>

        <asp:label id="Label1" runat="server" forecolor="blue" text="This is User Control 3 Panel 1 Fieldset 2" />
</fieldset>



我希望这会有所帮助.谢谢



i hope it would be helpful. thanks


,您可以尝试一下.如果您有任何疑问或问题,请问我.

you can try this. if you have question or problem ask me.

foreach (Control c in UpdatePanel1.Controls)
        {
            if (c.GetType().Name.ToLower() == "uc_ascx")//YOUR USER CONTROL NAME:for exacmple:uc_ascx
            {
                UserControl uc = (UserControl)c;
               
                TextBox txtbox1 = (TextBox)uc.FindControl("Textbox1");//you must CAST type of destination control. for example textbox or button, label , hyper link
                Label label = (Label)uc.FindControl("Labl1");
                Button button = (Button)uc.FindControl("button1");
                // done your job
                string value1 = txtbox1.Text;
                string value2 = label.Text;
                string value3 = button.Text;
            }
        }



在用户控件中放置一些控件,例如文本框或标签
然后在Main.aspx后面的代码中在Main.aspx上添加此用户控件时,请在事件(例如:button click event)上添加此函数
那么您可以获取所需的控件的所有属性,但是必须使用控件而不是:



in user control put some control like textbox or label
then when you added this user control on Main.aspx in the code behind of Main.aspx, add this function at event(for example: button click event )
then you can get all of the property of control that you like, but you must use of your control instead of :

TextBox txtbox1 = (TextBox)uc.FindControl("Textbox1");


string value1 = txtbox1.Text;


这篇关于如何从main.aspx页面的代码后面访问usercontrol.ascx中的字段集?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆