2011-01-10 197 views
0

我对ASP.NET很新。从组合框填充ASP.NET文本框选择索引更改

我有一个带有AJAX Combobox和TextBox的ASP.NET页面。组合框从数据库填充并具有ID值并显示Name。该文本框应显示地址。我想要做的是当名称组合框上的索引更改时,更改地址文本框中的值。 然后可以更改地址并将其保存回数据库。 我该如何做这个(简单?)任务?

到目前为止的代码...

<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> 

回答

1

试着用下面的标记。我用过DropDownList,它可以用AJAX组合框代替。 DetailsView可以通过CSS和ItemTemplate进一步增强。 您可以将更多的字段放入CityTree,Country等项目模板中。

<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 [email protected]' 
     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> 
0

在你ComboBox1_SelectedIndexChanged事件。设置TextBox1.Text = CurrentAddress变量。 实际上,我不是直接在asp中绑定命令的忠实粉丝。

我会创建一个名为LoadMyComboBox()的子页面,它将在Page_Load()事件中触发。

我会假设你将有一个按钮或某种类型的事件,将被解雇,以执行您的更新? TextChanged会在这里矫枉过正。添加更新按钮,然后在后面的代码中构建更新命令将处理您的更新。完成更新后,您可以再次调用LoadMyComboBox()来刷新组合框以刷新所有必要的更改,并在此时对文本框执行任何您想要的操作。

0

你可以通过使用下拉列表本身来做到这一点。在DropDownList的SelectedIndexChanged事件中,根据下拉列表选择将地址绑定到TextBox控件,并将其作为参数传递并将其保存到数据库中。