新手请教dropdownlist与textbox绑定的问题。
--------------------编程问答-------------------- 在dropdownlist changed事件里改变textbox得值就行了 --------------------编程问答--------------------改变的值是个ID,得到数据库里查询出单价的,忘记说了,是两个表。价格得靠查询出来,这个查询和显示在textbox不会写,请教。谢谢。 --------------------编程问答-------------------- sql语句会写不?
--------------------编程问答--------------------
是各种绑定,比如dataset什么的,好像一个SQL语句没法输出到textbox吧? --------------------编程问答--------------------
例如:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
SqlConnection conn = new SqlConnection("server=.;database=sheji;uid=sa;pwd=;");
conn.Open();
SqlDataAdapter da = new SqlDataAdapter("select * from userinfo", conn);
DataSet ds = new DataSet();
da.Fill(ds, "userinfo");
ddl.DataSource = ds.Tables["userinfo"];
ddl.DataValueField = "username";
ddl.DataTextField = "username";
ddl.DataBind();
SqlCommand scdc = new SqlCommand("select userpwd from userinfo where username=" + ddl.SelectedValue, conn);
this.TextBox1.Text = Convert.ToString(scdc.ExecuteScalar());
}
}
我就是搞不懂那些DataBind和SqlDataAdapter之类的,新手,谢谢。 --------------------编程问答--------------------
对就是这样 ,只不过protected void Page_Load 换成dropdownlist_selectchaged事件就行了 --------------------编程问答--------------------
上面这个写法,是不是感觉每次查询都从userinfo里查出所有记录,再显示一条的,这样的话如果userinfo表很大,那岂不是这个简单的查询我得耗费巨大资源? --------------------编程问答-------------------- 不要似是而非的以为
实际上只是查出一条来,没有查出所有记录。 --------------------编程问答-------------------- 对代码不熟悉的,SqlDataSource控件里可以可视化操作的。
其中参数源是Control表示参数来自下拉菜单,文本框等控件
ControlID表示你所选控件的ID。
--------------------编程问答-------------------- 就是ajax操作而已 --------------------编程问答-------------------- 老弟,你太执着了!!!
你要非想用数据源绑定TextBox也行,林子大了什么需求都有!
页面里加入两个数据源,第一个绑定DropdownList,指定查询语句、返回字段、连接字符串、连接类型;
第二个指定连接字符串、连接类型,不绑定
前台代码:
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true"
onselectedindexchanged="DropDownList1_SelectedIndexChanged">
</asp:DropDownList>
<asp:TextBox ID="TextBox1" ReadOnly="true" runat="server"></asp:TextBox>
<asp:SqlDataSource ID="下列列表的数据源" runat="server"
ConnectionString="<%$ ConnectionStrings:BridgeConnectionString %>"
SelectCommand="SELECT [商品ID], [商品名称] FROM [商品表]"></asp:SqlDataSource>
<asp:SqlDataSource ID="文本框的数据源" runat="server"
ConnectionString="<%$ ConnectionStrings:BridgeConnectionString %>" ></asp:SqlDataSource>
</div>
</form>
后台代码:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DropDownList1.DataSource = 下列列表的数据源;
DropDownList1.DataValueField = "商品ID";
DropDownList1.DataTextField = "商品名称";
DropDownList1.DataBind();
}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
string 商品ID = DropDownList1.SelectedValue;
文本框的数据源.SelectCommandType = SqlDataSourceCommandType.Text;
文本框的数据源.SelectCommand = "SELECT [商品单价] FROM [商品单价表] WHERE [商品ID] = " + 商品ID;
var 查询结果 = 文本框的数据源.Select(new DataSourceSelectArguments()) as System.Data.DataView;
if (查询结果.Count > 0)
TextBox1.Text = 查询结果[0][0].ToString();
}
把相关的名称改成你需要的,例如变量名 --------------------编程问答-------------------- 在dropdownlist里面有个SelectedIndexChanged事件,在这里面写就可以了
比如textbox1.text="1";等随便你写了
当然dropdownlist的回发autopostback要开启 --------------------编程问答--------------------
还是兄弟好 --------------------编程问答--------------------
哦,只是查出一条哈?谢谢。 --------------------编程问答--------------------
就是嫌自带的那个不够自定义,所以才来求助手动写的。谢谢。
补充:.NET技术 , ASP.NET