Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Autopopulating textboxes in Gridview using linqdatasource

Posted on 2008-10-09
1
Medium Priority
?
1,249 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 2000 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
In real business world data are crucial and sometimes data are shared among different information systems. Hence, an agreeable file transfer protocol need to be established.
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses
Course of the Month12 days, 23 hours left to enroll

972 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