简体   繁体   中英

Set Focus on Dynamically added control in AJAX Update Panel

I need to set the focus on a Label i have dynamically added. Here is my AJAX Panel

<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
    <ContentTemplate>
        <asp:Panel ID="pnlProgress" runat="server" class="summaryBox" 
                   Style="width: 650px; height: 225px" ScrollBars="Auto">
            <asp:PlaceHolder ID="plcHolder" runat="server"></asp:PlaceHolder>
            <asp:Button ID="btnTrigger" runat="server" OnClick="Button1_Click"
                        Style="visibility: hidden" /></asp:Panel>
    </ContentTemplate>
</asp:UpdatePanel>

Here is my c# code

label.Text = "SomeText";
label.ID = "lblMessage" + messageNumber;
if (heading)
{
    label.Attributes.Add("style", "font-weight: bold;")                
}
UpdatePanel1.ContentTemplateContainer.Controls.Add(label);
plcHolder.Controls.Add(label);

I tried to SetFocus(label) after the label.Attributes , but it didn't work. I can't do it in JScript as I can't GetElement on any of the Labels I've created dynamically. Any clues? I need to focus as the Panel has scrollbars and I want it to scroll to the last dynamically added Label

You can call focus directly in label like:

    label.Text = "SomeText";
    label.ID = "lblMessage" + messageNumber;
    if (heading)
    {
        label.Attributes.Add("style", "font-weight: bold;")                
    }
    UpdatePanel1.ContentTemplateContainer.Controls.Add(label);
    plcHolder.Controls.Add(label);
    label.Focus();

But if what you want is scroll to that element then label will not work, only input fields I believe. You can use textbox and style to look like a label.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM