简体   繁体   English

使用下拉列表中的值自动填充Detailsview插入文本框

[英]Auto-populate Detailsview insert textbox with value from dropdown list

I am trying to auto populate the textbox for the field clientId while the detailsview is in insert mode. 我正在尝试在detailsview处于插入模式下自动填充字段clientId的文本框。

It needs to end up like this... http://s7.postimg.org/o7d48cfvv/Untitled.png 它需要像这样结束... http://s7.postimg.org/o7d48cfvv/Untitled.png

name *textbox*
address *textbox*
phone *textbox*
email *textbox*
clientId *AUTOPOPULATED* - based on dropdownlist1.value

I looked around and couldnt find a solution anywhere. 我环顾四周,找不到任何解决方案。

Thanks! 谢谢!

to achieve this you need to make the last field(clientId) on your detailsview control as a template field and then set the value of InsertItemTemplate to the value of drop down list, here is an example for you: 为此,您需要将detailsview控件上的最后一个字段(clientId)作为模板字段,然后将InsertItemTemplate的值设置为下拉列表的值,这是为您提供的示例:

your .aspx web form: //eg this is your dropdown list 您的.aspx网络表单://例如,这是您的下拉列表

  <asp:DropDownList ID="DropDownList1" runat="server"> <asp:ListItem>id1</asp:ListItem> <asp:ListItem>id2</asp:ListItem> <asp:ListItem>id3</asp:ListItem> </asp:DropDownList> 

//eg your detailsview control //例如您的detailsview控件

  <asp:DetailsView ID="DetailsView1" runat="server" Height="50px" Width="125px" DataSourceID="SqlDataSource1" AutoGenerateRows="False"> <Fields> <asp:BoundField DataField="Firstname" HeaderText="Firstname" SortExpression="Firstname" /> <asp:BoundField DataField="Address" HeaderText="Address" SortExpression="Address" /> <asp:BoundField DataField="Suburb" HeaderText="Suburb" SortExpression="Suburb" /> <asp:BoundField DataField="State" HeaderText="State" SortExpression="State" /> <asp:BoundField DataField="Postcode" HeaderText="Postcode" SortExpression="Postcode" /> <asp:BoundField DataField="MPhone" HeaderText="MPhone" SortExpression="MPhone" /> <asp:TemplateField HeaderText="Username" SortExpression="Username"> <EditItemTemplate> <asp:Label runat="server" Text='<%# Eval("Username") %>' ID="Label1"></asp:Label> </EditItemTemplate> <InsertItemTemplate> <asp:TextBox runat="server" Text='<%# DropDownList1.SelectedItem.Value %>' ID="TextBox1"></asp:TextBox> </InsertItemTemplate> <ItemTemplate> <asp:Label runat="server" Text='<%# Bind("Username") %>' ID="Label1"></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:CommandField ShowInsertButton="True"></asp:CommandField> </Fields> </asp:DetailsView> 

so when you select a value from the dropdownlist above and click on new command on your detailsviewcontrol it will pickup the value from the list and bind it to the textbox as a value, like the snapshot below: 因此,当您从上面的下拉列表中选择一个值,然后在detailsviewcontrol上单击new命令时,它将从列表中提取该值并将其作为值绑定到文本框,例如下面的快照:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM