开发者

ASP.Net Label change in C#

I am usually dealing with c# code , but recently developing a asp.net page , I have a Calendar that Appears and the user selects the date he/sh开发者_StackOverflow中文版e wants. On that page is a asp lbl that i would like to display the currently selected date, which is normally easy for me but i am having trouble referencing/finding the control . Also I am unsure of the best way to do achieve this and i'm sure to come across this problem in the future.

This is where I would like to set the lbl text and have tried using the FindControl method but it's not working for me , thinking its possibly nested as i have some divs?.

 public void Calendar1_SelectionChanged(object sender, System.EventArgs e)
    {

        Control Lbl = FindControl("inputField");
        if (Lbl != null)
        {
            //Control mycontrol2 = Lbl.Parent;
            Lbl.Text = Calendar1.SelectedDate.ToShortDateString();
        }   

and this is in asp.

  <div id="date">


         <input type="text" size="12" id="inputField"   />
        <script>
            $("#inputField").click(function () {
                $("#box").show("slow");
            });
</script>
    </div>

How do I accomplish setting the inputfield text to the Calendar.SelectedDate ?. (and any tips you have come across yourself if any, for good practice)

Thanks For any help.


Any reason why you're not using an asp:Label? You can't find the "label" because it is an html control, aka a client side control. Use an <asp:Label id="lblCal" runat="server"... and you should have no problem changing its text in code behind:

lblCal.Text = Calendar1.SelectedDate.ToShortDateString();


Asp.Net considers only server tags as being controls available at C# level.

All those DIVs and other tags are just plain text for the Asp.Net, they will no be considered in the control hierachy.

To make Asp.Net consider it in the server, place the runat="server" attribute on the tags of your interest. They will become accessible in the code-behind by the name placed in the id attribute.

Asp.Net will never be abled to find the 'inputField' in your sample code.

Solution:

To solve your problem I recommend you use an Asp.Net WebForm control called TextBox... if what you want is an input:

<asp:TextBox runat="server" id="inputField" />

By doing this, you will be abled to access the inputField, in the code-behind by name:

inputField.Text = Calendar1.SelectedDate.ToShortDateString();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜