Set focus on textbox w/in FormView

Last post 07-06-2009 4:09 AM by alessandro. 5 replies.

Sort Posts:

  • Set focus on textbox w/in FormView

    07-05-2009, 11:20 AM
    • Member
      10 point Member
    • rvaldivia001
    • Member since 09-10-2008, 10:19 PM
    • Posts 50

    How do I set the focus on a textbox within a form view?  I guess my real question is how do I even access a textbox within the formview?

  • Re: Set focus on textbox w/in FormView

    07-05-2009, 1:06 PM
    Answer
    • Contributor
      6,768 point Contributor
    • alessandro
    • Member since 06-25-2002, 10:05 AM
    • Italy
    • Posts 1,103

    The page class exposes a setfocus method, use it to set the focus. NOte the code in the FormViews ItemCreated handler below eg:

    <%@ Page Language="c#" %>
    <%@ Import Namespace="System.Data" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0
    Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> runat="server">
      
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                FormView1.DataSource = new string[] { "apples" };
                FormView1.DataBind();
            }
        }

    <script

        protected void FormView1_ItemCreated(object sender, EventArgs e)
        {
            TextBox tb1 = (TextBox)FormView1.FindControl("TextBox1");
            if (tb1 != null)
                Page.SetFocus(tb1);
        }
    </script>

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title>Untitled Page</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <asp:FormView ID="FormView1" runat="server" OnItemCreated="FormView1_ItemCreated">
        <ItemTemplate>
            <asp:TextBox ID="TextBox1" Text='<%#Container.DataItem %>'
            runat="server"></asp:TextBox>
        </ItemTemplate>
        </asp:FormView>
        </form>
    </body>
    </html>

    Alessandro Zifiglio
    www.jiffycms.net - opensource HTML Editor for ASP.NET
    http://weblogs.asp.net/alessandro


    In the land of the blind, the man with one eye is king!:x
  • Re: Set focus on textbox w/in FormView

    07-05-2009, 4:03 PM
    • Member
      10 point Member
    • rvaldivia001
    • Member since 09-10-2008, 10:19 PM
    • Posts 50

    I'm not able to get it to work.  I can't seem to cast it to a textbox.  what might i be doing wrong:


    Protected Sub frmCheckEntry_ItemCreated(ByVal sender As Object, ByVal e As System.EventArgs) Handles frmCheckEntry.ItemCreated

            Dim tb1 As TextBox
            tb1 = (TextBox)(frmCheckEntry.FindControl("txtEditChkDate"))

            Page.SetFocus(tb1)

        End Sub

  • Re: Set focus on textbox w/in FormView

    07-05-2009, 4:37 PM
    • Contributor
      6,768 point Contributor
    • alessandro
    • Member since 06-25-2002, 10:05 AM
    • Italy
    • Posts 1,103

    ouch! vb.net Laughing

    Try this then :

    Dim tb1 As TextBox = DirectCast(frmCheckEntry.FindControl("txtEditChkDate"), TextBox)
    If tb1 IsNot Nothing Then
        Page.SetFocus(tb1)
    End If

    in vb.net when you cast, you can use DirectCast or CType function.

    http://msdn.microsoft.com/en-us/library/4x2877xb(VS.71).aspx

    http://msdn.microsoft.com/en-us/library/7k6y2h6x(VS.71).aspx

    Alessandro Zifiglio
    www.jiffycms.net - opensource HTML Editor for ASP.NET
    http://weblogs.asp.net/alessandro


    In the land of the blind, the man with one eye is king!:x
  • Re: Set focus on textbox w/in FormView

    07-05-2009, 7:46 PM
    • Member
      10 point Member
    • rvaldivia001
    • Member since 09-10-2008, 10:19 PM
    • Posts 50

    Thanks!!  HOw would I get to the value of a gridview?  I want to set a textbox to the value in the itemtemplate of a gridview.


    and, am I on the forum if I'm using vb.net?

  • Re: Set focus on textbox w/in FormView

    07-06-2009, 4:09 AM
    Answer
    • Contributor
      6,768 point Contributor
    • alessandro
    • Member since 06-25-2002, 10:05 AM
    • Italy
    • Posts 1,103

    here, try something in this direction. I assume you want to use the RowDataBound handler as this is fired for each row as it gets bound to data. If you prefer RowCreated, the code remains the same Wink

    Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs)
            If (e.Row.RowType = DataControlRowType.DataRow) Then
                Dim tb1 As TextBox = DirectCast(e.Row.FindControl("TextBox1"), TextBox)
                If (tb1 IsNot Nothing) Then
                    tb1.Text = "something"
                End If
            End If
        End Sub

    and wire the handler for RowDataBound declaratively or use the handles clause eg :

    <asp:GridView ID="GridView1" runat="server" OnRowDataBound="GridView1_RowDataBound"...

    Alessandro Zifiglio
    www.jiffycms.net - opensource HTML Editor for ASP.NET
    http://weblogs.asp.net/alessandro


    In the land of the blind, the man with one eye is king!:x
Page 1 of 1 (6 items)