c# - When selected index changed in combo box, how to add this new index to another text box? -
c# - When selected index changed in combo box, how to add this new index to another text box? -
i have 2 controls in web form. 1 combo box control, , other text box. want when user selects item in combo box, new item added text box , shown user automatically.
for example: 1. when user select '001' item in combo box, text box shows '001' in text area. 2. when user select '002' item in combo box, text box shows this: '001','002'
here code:
class webform : system.web.ui.page{ private stringbuilder sb = new stringbuilder(); protected void combobox1_selectedindexchanged(object sender, eventargs e) { //todos: add together selection of combo box text box2. this.localsb.append(this.combobox1.text); this.localsb.append(","); this.text2.text = this.localsb.tostring(); } ...... } here front end end code:
<%@ page language="c#" autoeventwireup="true" codebehind="pca_form.aspx.cs" inherits="webapplication2.pca_form" culture="auto" meta:resourcekey="pageresource1" uiculture="auto" %> <!doctype html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>pca_form</title> <style type="text/css"> #text1 { width: 147px; margin-top: 0px; } #form1 { height: 718px; width: 963px; } .auto-style1 { font-size: large; } #checkbox1 { width: 112px; } #select1 { width: 162px; } </style> <script> $(document).ready(function () { $('.combobox1').on('blur', function () { if ($(this).prop('checked')) { var initial = $('#text2').val(); var checkbox = $(this).text(); $('#text2').val(initial + ',' + checkbox).change(); } }); }); </script> </head> <body> <form id="form1" runat="server"> <asp:scriptmanager id="scriptmanager1" runat="server"></asp:scriptmanager> <div style="height: 51px; width: 906px;" aria-atomic="false" draggable="auto"> <span class="auto-style1">new price</span> <asp:textbox id="text1" runat="server" class="text" onkeypress="if (event.keycode!=46 && (event.keycode < 48 || event.keycode >57)) event.returnvalue = false;" textmode ="multiline"> </asp:textbox> <span class="auto-style1">item number</span> <ajaxtoolkit:combobox id="combobox1" textmode="multiline" runat="server" autopostback="true" datasourceid="sqldatasource1" datatextfield="code" datavaluefield="code" style="display: inline;" autocompletemode="suggest" dropdownstyle="dropdown" onselectedindexchanged="combobox1_selectedindexchanged"> </ajaxtoolkit:combobox> <asp:button id="button1" text="submit" onclick="submitbutton_click" runat="server" /> <asp:button id="button2" runat="server" text="excel" onclick="button2_click" /> <asp:textbox id="text2" runat="server" textmode="multiline"></asp:textbox> </div> <div style="height: 466px; width: 943px; margin-top: 35px"> <span class="auto-style1"> <asp:sqldatasource id="sqldatasource1" runat="server" connectionstring="data source=xxx;initial catalog=xxxxx;integrated security=true" providername="system.data.sqlclient" selectcommand="select [code] [item]"></asp:sqldatasource> <br /> <asp:gridview id="pcadatagrid" runat="server" width="938px" onselectedindexchanged="pcadatagrid_selectedindexchanged" autogeneratecolumns ="true"> </asp:gridview> </span> </div> </form> <p> </p> </body> </html> in combo box tag, trigger function called combobox1_selectedindexchanged() when user changes selection in box.
well, indeed possible. however, approach require update panel , agony follows if intend avoid lovely screen-flicker pesky postback's. recommend doing client side javascript:
$(document).ready(function() { $('.checkbox').on('blur', function() { if($(this).prop('checked')) { var initial = $('#txtfield').val(); var checkbox = $(this).text(); $('#txtfield').val(initial + ', ' + checkbox).change(); } }); }); that bit rough , rough, should meet needs. avoid issues bound introduce using asp.net page life cycle. points in direction.
as said above approach will:
require postback or update panelproblem:
postback - screen flicker, plus can create managing view state quite difficult.
update panel - pain work with, hinder performance. each 'postback' becomes ajax phone call of page. stored in memory, every postback becomes stored in memory during life-cycle. isn't efficient.
update:
you asked question about:
.checkbox #txtfield on front-end, in source code elements created like:
<asp:textbox id="txtfield" runat="server" placeholder="initial content..." /> there more can place on control, reply question correlate id or class name given element manipulating. id can bound single element, class can used on several elements. example-
<asp:checkbox id="chkfield" runat="server" cssclass="checkbox" /> so in instance . anchor on case, .checkbox. next # anchor id, #chkfield. when utilize javascript utilize $('.checkbox') or $('#chkfield') selector elements on page. clarify you?
c# asp.net combobox
Comments
Post a Comment