Solved

Autopopulating textboxes in Gridview using linqdatasource

Posted on 2008-10-09
1
1,223 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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

762 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