Retrieve value to a textbox from SqlDataSource in UpdatePanel

Last post 07-09-2009 2:50 AM by Jian Kang - MSFT. 3 replies.

Sort Posts:

  • Retrieve value to a textbox from SqlDataSource in UpdatePanel

    07-03-2009, 4:20 PM
    • Member
      point Member
    • Jedicillo
    • Member since 06-28-2009, 9:43 PM
    • Posts 4

    HI all,

    I've been on this for 3 days now.

    I have 2 textbox and 1 sqldatasource in 1 updatepanel.

    The idea is: I type some cod in textbox1, when textbox1 lose focus, textbox2 is filled with the description of the cod in textbox1. All this in an updatepanel, because i have to put some other data in the same page.

    I have tried with the event TextBox1_TextChanged, but from the sentence Dim dv = CType(SacaNombre.Select(DataSourceSelectArguments.Empty), DataView), it just don't work. Also have read some stuff about webservices and webmethod, but since i am new in this, it seems a little complicated.

    What can i do? Which way can i follow to accomplish this?


    thanks.

    Filed under:
  • Re: Retrieve value to a textbox from SqlDataSource in UpdatePanel

    07-03-2009, 11:17 PM
    • Contributor
      4,304 point Contributor
    • RickNZ
    • Member since 01-01-2009, 8:43 AM
    • Nelson, New Zealand
    • Posts 745

    You could use Ajax to do what you're asking without an UpdatePanel.  With an UpdatePanel, you should be able to set AutoPostBack="true" on the TextBox control, and then process the postback either with a TextChanged event or in Page_Load(), to set the value of the other TextBox.

    If I haven't decoded your problem description correctly, it might help if you could post your code.


  • Re: Retrieve value to a textbox from SqlDataSource in UpdatePanel

    07-04-2009, 3:27 PM
    • Member
      point Member
    • Jedicillo
    • Member since 06-28-2009, 9:43 PM
    • Posts 4

    I use the updatepanel to make a partial update and show the descr in textbox2, actually i have AutoPostBack="True" . How can it be done whitout updatepanel? Can't use Page_Load() because there will be others inputs in the same page.

    Also to show the data in textbox2 i have to make a select to my database at TextChanged. But i hadn't found a way to make this without ado,  considering that the query is via stored procedure with parameters. Is this posible with sqldatasorce?

  • Re: Retrieve value to a textbox from SqlDataSource in UpdatePanel

    07-09-2009, 2:50 AM
    Answer

    Hi Jedicillo,

    From your description, I think you would like something like follows:

    WebForm1.aspx

    <form id="form1" runat="server">
        <asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>
        <div>
            <asp:UpdatePanel ID="UpdatePanel1" runat="server">
            <ContentTemplate>
                --Inside the UpdatePanel--<br /><br />
                CategoryID
            <asp:TextBox ID="TextBox1" runat="server" AutoPostBack="True"></asp:TextBox>
                (Press Enter to submit)<br />
                CategoryName
            <asp:TextBox ID="TextBox2" runat="server" ReadOnly="True"></asp:TextBox>
            <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
                ConnectionString="<%$ ConnectionStrings:northwindConnectionString %>" 
                SelectCommand="SELECT [CategoryName] FROM [Categories] WHERE ([CategoryID] = @CategoryID)">
                <SelectParameters>
                    <asp:ControlParameter ControlID="TextBox1" Name="CategoryID" 
                        PropertyName="Text" Type="Int32" />
                </SelectParameters>
            </asp:SqlDataSource>
            <br />
    
            </ContentTemplate>
            </asp:UpdatePanel>
            
        <br /><hr /><br />
    
        --Outside the UpdatePanel--<br /><br />
        <%=DateTime.Now%>
        </div>
    </form>


    WebForm1.aspx.vb

    Protected Sub TextBox1_TextChanged(ByVal sender As Object, ByVal e As EventArgs) Handles TextBox1.TextChanged
    
        Dim dv As DataView = DirectCast(Me.SqlDataSource1.Select(DataSourceSelectArguments.Empty), DataView)
    
        If dv.Table.Rows.Count > 0 Then
            TextBox2.Text = dv.Table.Rows(0)(0).ToString()
        Else
            TextBox2.Text = "There is no such a record."
        End If
    
    End Sub


    The TextChanged event is raised when the content of the text box changes between posts to the server.

    Related documents:

    TextBox.TextChanged Event
    http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.textbox.textchanged.aspx

    SqlDataSource.Select Method
    http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.sqldatasource.select.aspx

    Jian Kang
    Microsoft Online Community Support

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Page 1 of 1 (4 items)