BingWay原創作品,轉載請註明作者和出處。
在Web編程中,可編輯的下拉式清單一直是個不是問題的問題,隨之而出的第三方控制項、Js控制Select控制項的代碼也隨處可見,但是能相容所有瀏覽器,
下拉框和文字框緊密結合,實現起來卻不是那麼容易。最近做項目,想到一個DropDownList可編輯控制項比較好的實現方法,和大家分享一下。
我的方法是在DropDownList上加層和文字框,有人喜歡用像素定位文字框,這樣做不但很難定位,而且瀏覽器運行出來的效果可能有偏差;用層定位相對而言比較好控制。Code
1 <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true"
2 OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"
3 Width="180px" style="position:absolute; ">
4 <asp:ListItem>測試一</asp:ListItem>
5 <asp:ListItem>測試二</asp:ListItem>
6 <asp:ListItem>測試三</asp:ListItem>
7 </asp:DropDownList>
8 <div>
9 <iframe id="DivShims" src="javascript:false;" scrolling="no"
10 frameborder="0" style="position: absolute; height: 20px;" width="158px">
11 </iframe>
12 <input type="text" id="txtCName" runat="server" style="width: 158px; position:absolute;" />
13 </div>
下拉框和文字框緊密整合。
頁面載入及觸發DropDownList1_SelectedIndexChanged事件,給文字框賦值。 string text = DropDownList1.SelectedValue.ToString();//DropDownList1選中的值
txtCName.Value = text;//給txtCName賦值 運行測試。 更改下拉框的值。這是我想到的最簡單的方法,園子裡的朋友,有什麼更好的方法,請不吝賜教。