积极答复者
如何让FormView中DropdownList的值写到TextBox中去?

问题
-
如何让FormView中DropdownList的值选择后更新到TextBox中去?
一个简单的问题,不过前提是DropdownList、TextBox都在FormView中。
下面是初步的代码,我想应该是在SelectedIndexChanged事件中写,但具体该怎么写呢?各位能否给出具体的代码?
---------------------------------前台-----------------------------
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="TRY_GridViewToDetailsView_DropInForm_Default" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:FormView ID="FormView1" runat="server"> <EditItemTemplate> <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" onselectedindexchanged="DropDownList1_SelectedIndexChanged"> <asp:ListItem>1</asp:ListItem> <asp:ListItem>2</asp:ListItem> <asp:ListItem>3</asp:ListItem> <asp:ListItem>4</asp:ListItem> <asp:ListItem>5</asp:ListItem> </asp:DropDownList> <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> </EditItemTemplate> </asp:FormView> </div> </form> </body> </html>
-----------------------------------后台---------------------------------------
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class TRY_GridViewToDetailsView_DropInForm_Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) { } }
C# 菜鸟中的雏鸟!提的问题也许很幼稚,但我是认真的。希望看在党国的面子上拉兄弟一把!
- 已编辑 linjiangxian11 2013年5月3日 8:41
答案
-
我直接用下面的代码解决了,原来就是Bind一个DropDownList。谢谢你的帮助!
<asp:DropDownList ID="DropDownList_ContractCompany" runat="server" BorderStyle="None" Height="30px" Width="100%" Font-Size="12px" AutoPostBack="True" DataSourceID="SqlDataSource_ContractCompany" DataTextField="ContractCompany" DataValueField="ContractCompany" SelectedValue='<%# Bind("ContractCompany") %>' > </asp:DropDownList>
C# 菜鸟中的雏鸟!提的问题也许很幼稚,但我是认真的。希望看在党国的面子上拉兄弟一把!
- 已标记为答案 linjiangxian11 2013年5月10日 7:33
全部回复
-
请问您是前端获取,还是后端获取呢
如果是前端,因为TextBox在生成html时客户端id改变了。在【TextBox1】前加了很多前缀
获取方法如下:
document.getElementById("<%=TextBox1.ClientID%>")
如果是后端,方法如下:
TextBox1.Text
e-mail:ist_te@hotmail.com
- 已编辑 Tt.string() 2013年5月7日 1:16
-
谢谢您的解答!我反复尝试了没成功。前台我不知道我错在哪里了,后台TextBox1.Text肯定行不通,因为它是在FormView中,不能直接这样写!
其实我的初衷很简单,就是用FormView配合SqlDateSource做了一个修改的界面,FormView中的EditTemplate中都是TextBox,我希望把EditTemplate中的部分TextBox改成DropdownList。有合适的办法么?
下面是我尝试的代码和图,行不通。。。
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="TRY_GridViewToDetailsView_DropInForm_Default" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <script type="text/javascript"> function change(val) { var textBox = document.getElementById("CityTextBox"); textBox.value = val; } </script> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:FormView ID="FormView1" runat="server" DataKeyNames="CID" DataSourceID="SqlDataSource1"> <EditItemTemplate> CID: <asp:Label ID="CIDLabel1" runat="server" Text='<%# Eval("CID") %>' /> <br /> City: <asp:TextBox ID="CityTextBox" runat="server" Text='<%# Bind("City") %>' /> <br /> <asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True" CommandName="Update" Text="更新" /> <asp:LinkButton ID="UpdateCancelButton" runat="server" CausesValidation="False" CommandName="Cancel" Text="取消" /> <asp:DropDownList ID="DropDownList_City" runat="server" onchange="change(this.value)"> <asp:ListItem>长沙市</asp:ListItem> <asp:ListItem>株洲市</asp:ListItem> <asp:ListItem>娄底市</asp:ListItem> </asp:DropDownList> </EditItemTemplate> <InsertItemTemplate> City: <asp:TextBox ID="CityTextBox" runat="server" Text='<%# Bind("City") %>' /> <br /> <asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True" CommandName="Insert" Text="插入" /> <asp:LinkButton ID="InsertCancelButton" runat="server" CausesValidation="False" CommandName="Cancel" Text="取消" /> </InsertItemTemplate> <ItemTemplate> CID: <asp:Label ID="CIDLabel" runat="server" Text='<%# Eval("CID") %>' /> <br /> City: <asp:Label ID="CityLabel" runat="server" Text='<%# Bind("City") %>' /> <br /> <asp:LinkButton ID="EditButton" runat="server" CausesValidation="False" CommandName="Edit" Text="编辑" /> <asp:LinkButton ID="DeleteButton" runat="server" CausesValidation="False" CommandName="Delete" Text="删除" /> <asp:LinkButton ID="NewButton" runat="server" CausesValidation="False" CommandName="New" Text="新建" /> </ItemTemplate> </asp:FormView> <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:长铁物业公司资产数据库ConnectionString %>" DeleteCommand="DELETE FROM [ContractTable] WHERE [CID] = @CID" InsertCommand="INSERT INTO [ContractTable] ([City]) VALUES (@City)" SelectCommand="SELECT [CID], [City] FROM [ContractTable]" UpdateCommand="UPDATE [ContractTable] SET [City] = @City WHERE [CID] = @CID"> <DeleteParameters> <asp:Parameter Name="CID" Type="Int32" /> </DeleteParameters> <UpdateParameters> <asp:Parameter Name="City" Type="String" /> <asp:Parameter Name="CID" Type="Int32" /> </UpdateParameters> <InsertParameters> <asp:Parameter Name="City" Type="String" /> </InsertParameters> </asp:SqlDataSource> </div> </form> </body> </html>
-------------------------图1-------------------------
C# 菜鸟中的雏鸟!提的问题也许很幼稚,但我是认真的。希望看在党国的面子上拉兄弟一把!
-
尝试使用:
var id = (Form1.FindControl("TextBox1") as TextBox).ClientID;
然后使用#+id即可。
If you think one reply solves your problem, please mark it as An Answer, if you think someone's reply helps you, please mark it as a Proposed Answer
Help by clicking:
Click here to donate your rice to the poor
Click to Donate
Click to feed Dogs & Cats
Found any spamming-senders? Please report at: Spam Report -
如果你的下拉列表在编辑状态下,请把代码写到ItemEditing事件中。
If you think one reply solves your problem, please mark it as An Answer, if you think someone's reply helps you, please mark it as a Proposed Answer
Help by clicking:
Click here to donate your rice to the poor
Click to Donate
Click to feed Dogs & Cats
Found any spamming-senders? Please report at: Spam Report- 已编辑 ThankfulHeart 2013年5月9日 9:07
-
我直接用下面的代码解决了,原来就是Bind一个DropDownList。谢谢你的帮助!
<asp:DropDownList ID="DropDownList_ContractCompany" runat="server" BorderStyle="None" Height="30px" Width="100%" Font-Size="12px" AutoPostBack="True" DataSourceID="SqlDataSource_ContractCompany" DataTextField="ContractCompany" DataValueField="ContractCompany" SelectedValue='<%# Bind("ContractCompany") %>' > </asp:DropDownList>
C# 菜鸟中的雏鸟!提的问题也许很幼稚,但我是认真的。希望看在党国的面子上拉兄弟一把!
- 已标记为答案 linjiangxian11 2013年5月10日 7:33