从usercontrol(.ascx)页面(月份显示日历)获取值并将其传递到父页面文本框中的文本框? [英] Get the value from usercontrol(.ascx) page(monthsdisplaying calender) and pass it to textbox in parent page textbox?

查看:54
本文介绍了从usercontrol(.ascx)页面(月份显示日历)获取值并将其传递到父页面文本框中的文本框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个用户控件,它有一个只有几个月显示的日历控件,即在JAN,FEB等!这个用户控制日历从父页面的文本框切换(使用Jquery),我的代码很好,直到这里。



现在我想要的是显示那些日历以数字形式的值;为了达到这个目的,我宣布了一个枚举即



 枚举 monthsOfYear 
{
Jan = 1 ,2月,3月,4月,5月,6月,7月,8月,9月,10月,11月,12月
};

int x =( int )monthsOfYear.Jan; // x = 1此处此值将传递到
// 其他页面(.aspx)





现在因为所有月份都分配了它的值(在用户calender.ascx页面后面的代码中),我想要将这些相应的值传递到父页面的文本框(此日历切换的位置)任何上述月份(用户控件中的月份链接按钮)。

因为我在.ascx页面中获取x中的值,但是这个值我要传递到3个文本框中我的parent.aspx页面。



我也尝试过使用代理但仍然没有得到结果。因为,事件是在用户的链接按钮上触发的控制页面和此事件后的值(来自linkbutton的文本必须传递给aspx页面文本框。



如果有任何澄清,请告诉我。

请帮我完成这项任务。

提前致谢!!

解决方案

尝试定义属性到用户控制中的访问变量。



例如:



UserControl.ascx:

 枚举 monthsOfYear 
{
Jan = 1 ,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec
};

int x =( int )monthsOfYear.Jan;
public int MonthName
{
get { return x; }
}





Page.aspx:



< pre lang =c#> TextBox1.text = UserControl.MonthName;


UC以下我在我的.aspx页面使用



< asp:textbox id =   txtMonths runat =   server cssclass =   controlMonYr xmlns:asp =   #unknown >  < /   asp:textbox  >   //  这里我想传递用户控件中的选定值 
< cc:calenderctrl id = ccd runat = < span class =code-string> server getvalue = txtMonths xmlns:cc = #unknown < span class =code-keyword>> < / cc:calenderctrl < span class =code-keyword>> // getvalue是.ascx页面中定义的属性,以下是定义

在userControl.ascx页面中

< pre lang = < span class =code-string> HTML > < td>
< asp:TextBox ID = txtB runat = server > < / asp:TextBox > // 此文本框在cell1_click上填充字符串值
< / td >





下面的标签是一个标签,像这样直到cell12

 <  < span class =code-leadattribute> pre  >  
< td >
< asp:LinkBut​​ton ID = Cell1 runat = 服务器 OnClick = Cell1_Click > < < span class =code-leadattribute> / asp:LinkBut​​ton >
< / td >





  public   int  MonthName 
{
get { return x; }
}

private string _getValue;

/// < 摘要 >
/// 从usercontrol访问值并传递给usercontrol本身的文本框
/// < / summary < span class =code-summarycomment>>
public string GetValue
{
get { return _getValue; }
set {_ getValue = value ; }
}

受保护 void Cell1_Click( object sender,EventArgs e)
{
LinkBut​​ton senderId =(LinkBut​​ton)sender;
int passValue = 1 ;
if (!int.TryParse(senderId.Text.ToString(), out passValue) )
{
GetValue = senderId.Text.ToString();
txtB.Text = GetValue;
}
if int .TryParse(senderId.Text.ToString() , out passValue))
{
lnkYear.Text = senderId.Text.ToString();
DateTime passDate = DateTime.Parse( 01-01 - + senderId.Text。的ToString());
CellValues(passDate, M);
}

}

private void CellValues(DateTime paramDate, string 操作) /// 在.ascx的paqe加载中,它调用
{
if (operation == M
{
lnkLstYear.Visible = false ;
ltrHyp.Text = ;
Cell1.Text = Jan;
Cell2.Text = 2月;
Cell3.Text = Mar;
Cell4.Text = Apr;
Cell5.Text = May;
Cell6.Text = Jun;
Cell7.Text = Jul;
Cell8.Text = Aug;
Cell9.Text = Sep;
Cell10.Text = Oct;
Cell11.Text = 11月;
Cell12.Text = Dec;
}
else
{
lnkLstYear.Visible = true ;
int providedYear = Convert.ToInt32(lnkYear.Text.ToString());
Cell1.Text = providedYear.ToString();
Cell2.Text =(providedYear + 1 )。ToString();
Cell3.Text =(providedYear + 2 )。ToString();
Cell4.Text =(providedYear + 3 )。ToString();
Cell5.Text =(providedYear + 4 )。ToString();
Cell6.Text =(providedYear + 5 )。ToString();
Cell7.Text =(providedYear + 6 )。ToString();
Cell8.Text =(providedYear + 7 )。ToString();
Cell9.Text =(providedYear + 8 )。ToString();
Cell10.Text =(providedYear + 9 )。ToString();
Cell11.Text =(providedYear + 10 )。ToString();
Cell12.Text =(providedYear + 11 )。ToString();
lnkLstYear.Text = Cell12.Text;
ltrHyp.Text = - ;
}
}





我应该更详细地放置JQuery或我的用户控件......?如果这还不够?


你是否用网页注册了用户控件?



这样做: -



1.首先创建用户控件

2.创建返回MonthName的属性



 public int MonthName 
{
get {return DateTime.Now.Month;}
}





3.现在创建网页(.aspx)并注册用户控制。

<![CDATA [<%@    注册    Src   = 〜/ WebUserControl1.ascx    TagName   =  UC    Ta gPrefix   =  UC   %>  





4.现在创建文本框并提交按钮在aspx页面。

 <   UC:UC     ID   =  userControl1      runat   =  server    /  >  
< asp:按钮 ID = btnInsert runat = server onclick = btnInsert_Click 文本 = 提交 / >
< br / >
< asp:TextBox ID = txtMonth runat = 服务器 > < / asp :TextBox >





5.现在点击按钮或页面加载时,您可以在文本框中设置MonthName。

  protected   void  btnInsert_Click( object  sender,EventArgs e)
{
txtMonth.Text = userControl1.MonthName.ToString( );
}





6.完成


I''ve created a usercontrol which has a calender control having only months displayed i.e in JAN,FEB,etc !! This user control calender is toggled from parent page''s textbox(using Jquery),my code is fine till here.

Now what I want is to display those calender value in numeric form ;to achieve this I''ve declared an enum i.e

enum monthsOfYear 
{ 
    Jan = 1, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec 
};

int x = (int)monthsOfYear.Jan;//x=1 here and this value will be passed to textbox of
                               //other page(.aspx)



Now as all the months has its value assigned (in the code behind of user calender.ascx page),these respective values I want to pass on to the textbox of parent page (where this calender is toggled) on an individual click of any of the above months(month link button in user control).
As I''m getting the value in "x" in my .ascx page ,but this values I want to pass into 3 textboxes of my parent.aspx page.

I''ve also tried using delegates but still not got the result.Because, the event is fired on the link button of user control page and after this event the value(from text of linkbutton) must be passed to the aspx page textboxes.

If any clarification then let me know.
please help me in achieving this task.
Thanks in advance !!

解决方案

Try defining property to access variable that is in user control.

For ex:

UserControl.ascx:

enum monthsOfYear 
{ 
    Jan = 1, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec 
};
 
int x = (int)monthsOfYear.Jan;
public int MonthName
{
   get { return x; }
}



Page.aspx:

TextBox1.text = UserControl.MonthName;


Below UC I''m using in my .aspx page

<asp:textbox id="txtMonths" runat="server" cssclass="controlMonYr" xmlns:asp="#unknown"></asp:textbox> //here i want to pass the selected value from user control
  <cc:calenderctrl id="ccd"  runat="server" getvalue="txtMonths" xmlns:cc="#unknown"></cc:calenderctrl>//getvalue is property defined in .ascx page, below is the defination

In userControl.ascx page  

<pre lang="HTML">  <td>
                        <asp:TextBox ID="txtB" runat="server"></asp:TextBox> //this textbox is getting filled with string value on cell1_click
                    </td>



Below tag is a single tag,like this till cell12

<pre>
<td>
       <asp:LinkButton ID="Cell1" runat="server" OnClick="Cell1_Click"></asp:LinkButton>
</td>



public int MonthName
    {
        get { return x; }
    }

    private string _getValue;

    /// <summary>
    /// To access the value from usercontrol and pass on to the textbox in usercontrol itself
    /// </summary>
    public string GetValue
    {
        get { return _getValue; }
        set { _getValue = value; }
    } 

 protected void Cell1_Click(object sender, EventArgs e)
    {
        LinkButton senderId = (LinkButton)sender;
        int passValue=1;
        if (!int.TryParse(senderId.Text.ToString(), out passValue))
        {
            GetValue = senderId.Text.ToString();
            txtB.Text = GetValue;
        }
        if(int.TryParse(senderId.Text.ToString(), out passValue))
        {
            lnkYear.Text = senderId.Text.ToString();
            DateTime passDate = DateTime.Parse("01-01-" + senderId.Text.ToString());
            CellValues(passDate, "M");            
        }   
        
    }

 private void CellValues(DateTime paramDate, string operation) ///In paqe load of .ascx it called
    {
        if (operation == "M")
        {            
            lnkLstYear.Visible = false;
            ltrHyp.Text = "";
            Cell1.Text = "Jan";
            Cell2.Text = "Feb";
            Cell3.Text = "Mar";
            Cell4.Text = "Apr";
            Cell5.Text = "May";
            Cell6.Text = "Jun";
            Cell7.Text = "Jul";
            Cell8.Text = "Aug";
            Cell9.Text = "Sep";
            Cell10.Text = "Oct";
            Cell11.Text = "Nov";
            Cell12.Text = "Dec";           
        }
        else
        {
            lnkLstYear.Visible = true;
            int providedYear = Convert.ToInt32(lnkYear.Text.ToString());
            Cell1.Text = providedYear.ToString() ;
            Cell2.Text = (providedYear + 1).ToString();
            Cell3.Text = (providedYear + 2).ToString();
            Cell4.Text = (providedYear + 3).ToString();
            Cell5.Text = (providedYear + 4).ToString();
            Cell6.Text = (providedYear + 5).ToString();
            Cell7.Text = (providedYear + 6).ToString();
            Cell8.Text = (providedYear + 7).ToString();
            Cell9.Text = (providedYear + 8).ToString();
            Cell10.Text = (providedYear + 9).ToString();
            Cell11.Text = (providedYear + 10).ToString();
            Cell12.Text = (providedYear + 11).ToString();
            lnkLstYear.Text = Cell12.Text;
            ltrHyp.Text = "-";
        }
    }



Should I place the JQuery or my usercontrol in more detail...?? if this much is not enough ?


Did you register user control with the web page??

Do like this :-

1. first create user control
2. create property that return MonthName

public int MonthName
{
   get{return DateTime.Now.Month;}
}



3.Now create web Page(.aspx) and register user control.

<![CDATA[<%@ Register Src="~/WebUserControl1.ascx" TagName="UC" TagPrefix="UC" %>



4. Now create Text box and submit button in aspx page.

 <UC:UC ID="userControl1"  runat="server" />
<asp:Button ID="btnInsert" runat="server" onclick="btnInsert_Click" Text="Submit" />
 <br />
 <asp:TextBox ID="txtMonth" runat="server"></asp:TextBox>



5. Now on click of button or on page load you can set MonthName in textbox.

protected void btnInsert_Click(object sender, EventArgs e)
        {
           txtMonth.Text=userControl1.MonthName.ToString();
        }



6. Done


这篇关于从usercontrol(.ascx)页面(月份显示日历)获取值并将其传递到父页面文本框中的文本框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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