开发者

Populate ASP.NET textbox from combobox selected index change

开发者 https://www.devze.com 2023-02-03 05:27 出处:网络
I a开发者_C百科m very new to ASP.NET. I have a an ASP.NET page with an AJAX Combobox and a TextBox. The combobox populates from a database and has value of ID and displays Name. The textbox should di

I a开发者_C百科m very new to ASP.NET.

I have a an ASP.NET page with an AJAX Combobox and a TextBox. The combobox populates from a database and has value of ID and displays Name. The textbox should display Address. All i want to do is change the value in the Address textbox when the index on the Name combo box changes. And then be able to change the address and save it back to the database. How do I do this (simple?) task?

Code so far...

<form id="form1" runat="server">
    <div>
        <asp:ComboBox ID="ComboBox1" runat="server" DataSourceID="AccessDataSource1" DataTextField="CompositeName"
            DataValueField="Id" MaxLength="0" Style="display: inline;" 
            AutoCompleteMode="SuggestAppend" 
            onselectedindexchanged="ComboBox1_SelectedIndexChanged">
        </asp:ComboBox>
        <asp:AccessDataSource ID="AccessDataSource1" runat="server" DataFile="~/App_Data/StudentDB.accdb"
            SelectCommand="SELECT [Id], [Name], [Address] FROM [tblStudents]">
        </asp:AccessDataSource>
    </div>
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    </form>


Try with the below markup. I've used DropDownList, it could be replaced with AJAX ComboBox. The DetailsView could be further enhanced with CSS and ItemTemplate. You could put more fields into the ItemTemplate like City, Country and so on.

<asp:AccessDataSource DataFile="App_Data/Students.accdb"  ID="AccessDataSource1" 
         DataSourceMode='DataSet' SelectCommand='Select ID, [First Name], [Last Name] from Students'
        runat='server' >

    </asp:AccessDataSource>

     <asp:AccessDataSource DataFile="App_Data/Students.accdb"  ID="AccessDataSource2" 
        SelectCommandType="Text" SelectCommand='Select [ID], [Address] from [Students] where ID=@id'
        runat='server'>
        <SelectParameters>
            <asp:ControlParameter ControlID='DropDownList1' Name='id'/>
        </SelectParameters>
    </asp:AccessDataSource>

    <asp:DropDownList ID='DropDownList1' runat='server' AutoPostBack='true' DataSourceID='AccessDataSource1'
        DataTextField="First Name" DataValueField='ID'>
    </asp:DropDownList>

    <asp:DetailsView ID='DetailsView' runat="server" DataSourceID='AccessDataSource2' DataKeyNames='ID'>

        <Fields>
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:TextBox ID='AddressTextBox' runat='server' Text='<%# Bind("Address") %>'></asp:TextBox>
                    <asp:Button ID='AddressSaveButton' runat='server' Text='Save' UseSubmitBehavior='true' />
                </ItemTemplate>
            </asp:TemplateField>
        </Fields>

    </asp:DetailsView>


In your ComboBox1_SelectedIndexChanged event. Set TextBox1.Text = CurrentAddress variable. In reality, I am not a big fan of bindings commands directly in the asp.

I would create a Sub that is called LoadMyComboBox() which would fire in the Page_Load() event.

I would assume your going to have a button or some type of event that would be fired to perform your update? TextChanged would be overkill here. Adding an update button and then building your update command in the code behind would handle your update. Once that update is complete you can simply call LoadMyComboBox() again to refresh your combobox to refresh any necessary changes and do whatever you want with your textbox at this time as well.


u can do this by using a dropdownlist itself. In the DropDownList's SelectedIndexChanged event, bind the address to the TextBox control according the dropdownlist selection and pass this as a parameter and save it in database.

0

精彩评论

暂无评论...
验证码 换一张
取 消