Solved

Autopopulating textboxes in Gridview using linqdatasource

Posted on 2008-10-09
1
1,229 Views
Last Modified: 2013-11-11
I have several gridviews working very well with a LinqDataSource.  I am able to Insert, Update and Delete records.

In the footertemplate of one of my grids, I have created a small form to insert a new record into the database, and that is working great.  The form consists of address related textboxes such as address, city, state and zip and none of the controls are bound to any data source.

What I would like to do is provide a way for the user to simply enter a zip code in the zip code text box and have the city and state textboxes populated from a table in the DB.  When the txtZipCode control looses focus, I would like to populate the city and state text boxes with a partial post back.  I have a script manager in my master page, and the webpart I am using is wrapped in an update panel.

I have several Linq data sources.  The GridView uses ldsPersonAddresses and there is a relationship to the postal_code table which contains the zip, city, and state.  I think I could use that datasource, but I don't know how to cause the postback and use txtZipCode.Text.  I also have a stored procedure available which would return the data...

How do I bind the txtCity and txtState controls with a partial postback?
<asp:LinqDataSource ID="ldsPersonAddresses" runat="server" ContextTypeName="VBCODE.jhadbDataClassesDataContext"

            TableName="person_addresses" Where="PersonID == @PersonID" EnableDelete="True"

            EnableInsert="True" EnableUpdate="True">

            <WhereParameters>

                <asp:QueryStringParameter Name="PersonID" QueryStringField="PersonID" Type="Int32" />

            </WhereParameters>

        </asp:LinqDataSource>

        <asp:LinqDataSource ID="ldsAddressTypes" ContextTypeName="VBCODE.jhadbDataClassesDataContext"

            TableName="address_types" runat="server">

        </asp:LinqDataSource>

        <asp:LinqDataSource ID="ldsStates" runat="server" ContextTypeName="VBCODE.jhadbDataClassesDataContext"

            TableName="states">

        </asp:LinqDataSource>

        <asp:LinqDataSource ID="ldsCountries" ContextTypeName="VBCODE.jhadbDataClassesDataContext"

            TableName="countries" runat="server">

        </asp:LinqDataSource>

        <asp:LinqDataSource ID="ldsZipCodeLookup" runat="server" 

            ContextTypeName="VBCODE.jhadbDataClassesDataContext" Select="new (City, State)" 

            TableName="postal_codes" Where="Zip == @Zip">

            <WhereParameters>

                <asp:FormParameter FormField="txtNewZip" Name="Zip" Type="String" />

            </WhereParameters>

        </asp:LinqDataSource>

Open in new window

0
Comment
Question by:abenage
1 Comment
 
LVL 4

Accepted Solution

by:
novynov earned 500 total points
ID: 22690048
I may be misunderstanding your problem, and there are lots of ways to tackle this, here's an approach that comes to mind:

- Put the footer row content in it's own updatepanel. I believe this works fine with the gridview and will minimize the postback payload for the zip lookup.
- Put a LinqDataSource in the footer row updatepanel that is bound to the postal_code table. For its whereparameter, bind it to the zip text field.
- Place the city and state text boxes (not the zip) in a FormView. This will allow both controls to be easily databound to the LinqDataSource. Set the DataSourceID to point at the LDS.
- Make the zip text field AutoPostback = true. This will cause the postback to happen on focus loss.

Because the value has changed when the text box postback happens, the LDS will rebind and grab the right data.

I did a small demo of this (see snippet), not in a grid view, and it worked fairly well. The only issue that I saw was that the two fields of the formview didn't show up if there was no data. I imagine there is an easy way around this - though I haven't investigated. If it's a pain, I'd ditch the FormView and just place the TextBox controls in my footer, and manage the databinding by hand in my OnTextChanged handler. In fact, I might not even use a LDS at that point, I'd just fire of a query to the db, grab the results, and stuff the text boxes.

If you do your insert logic in this same updatepanel, you'll need to call UpdatePanel.Update() on the UpdatePanel that owns the whole gridview. Otherwise, your new row will be in the db, but won't show up in the gridview until you refresh the whole page.

BTW, it may be none of my business, but aren't there cases where one zip code can map to multiple cities. It seems like I've run into this before. Does your db schema, and your approach to this problem account for that?

I hope this half-baked answer helps. Let me know.

  <asp:UpdatePanel runat="server" ID="upZip" UpdateMode="Conditional">

    <ContentTemplate>

     <asp:LinqDataSource runat="server" ID="lds" 

        ContextTypeName="WebApplication2.TestDataContext" TableName="ZipCodes" 

        Where="Zip == @Zip">

        <WhereParameters>

            <asp:ControlParameter ControlID="textZip" Name="Zip" PropertyName="Text" 

                Type="String" />

        </WhereParameters>

    </asp:LinqDataSource>

    

    <asp:FormView runat="server" DataSourceID="lds" ID="fv" DataKeyNames="Zip" DefaultMode="ReadOnly">

        <ItemTemplate>

            City:

            <asp:TextBox ID="CityTextBox" runat="server" Text='<%# Bind("City") %>' />

            <br />

            State:

            <asp:TextBox ID="StateTextBox" runat="server" Text='<%# Bind("State") %>' />

            <br />

          

        </ItemTemplate>

    </asp:FormView>

    <asp:TextBox runat="server" ID="textZip" AutoPostBack="true" Text="" />

    </ContentTemplate>

    </asp:UpdatePanel>

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now