Click here to Skip to main content
15,867,835 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
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

C#
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 !!
Posted
Updated 15-Apr-13 23:31pm
v3

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

For ex:

UserControl.ascx:
C#
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:

C#
TextBox1.text = UserControl.MonthName;
 
Share this answer
 
Comments
Rabbil 16-Apr-13 5:28am    
Thank you for your response...!!
I've defined the property but in the aspx page that textbox is not acessing the UserControl.MonthName.... what to do ?
Vani Kulkarni 16-Apr-13 5:31am    
Would you please post the code?
Rabbil 16-Apr-13 5:47am    
I've used one textbox in .ascx just to check whether GetValue() Property defined is working fine or not and this is supported by Jquery using css class...
Any more clarification ?
Below UC I'm using in my .aspx page

C#
<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
ASP.NET
<pre>
<td>
       <asp:LinkButton ID="Cell1" runat="server" OnClick="Cell1_Click"></asp:LinkButton>
</td>


C#
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 ?
 
Share this answer
 
v4
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.
C#
<![CDATA[<%@ Register Src="~/WebUserControl1.ascx" TagName="UC" TagPrefix="UC" %>


4. Now create Text box and submit button in aspx page.
XML
 <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.
C#
protected void btnInsert_Click(object sender, EventArgs e)
        {
           txtMonth.Text=userControl1.MonthName.ToString();
        }


6. Done
 
Share this answer
 
v2
Comments
Rabbil 16-Apr-13 9:58am    
Thank you for your valuable response....
Yeah,I've registered UserControl....
upto step 5 I've done (according to your above code) but after this what I want is how to pass the respective values i.e if Jan=1 then 1 must be displayed in the textbox after onclick event of the text from UC,rather I'm getting JAN displayed into the textbox.Also, I'm not supposed to use btnInsert.So which event will be fired to get this result ?
Or should I display JAN in the txtMonth textbox and rather convert its value from the code behind so that its value (i.e value of JAN=1 ) will only be sent to DB and not JAN... ??

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900