[英]Data Bind in ASP.Net for Dropdown box
我必须从名为dbo.Names的表中获取值(A,B,C,D),从SQL Server的下拉框中输入。我已经手动完成了,但是如何使用表中的select语句实现相同的目的。 。???或如果我必须从存储过程中选择包含值的变量?
<div class="col-sm-12">
<div class="col-sm-4 lhs-form-box" style="padding: 10px; height: 247px;">
<div class="form-group" style="width: 87%; float: left;">
<label for="sel1">ITEM</label>
<asp:DropDownList ID="ddl_QU" runat="server" class="form-control">
<asp:ListItem Text="A" Value="A"></asp:ListItem>
<asp:ListItem Text="B" Value="B"></asp:ListItem>
<asp:ListItem Text="C" Value="C></asp:ListItem>
<asp:ListItem Text="D" Value="D"></asp:ListItem>
</asp:DropDownList>
</div>
<span>
<img src="info.png" style="margin: 31px 0px 0px 5px">
</span>
</div>
</div>
DropDownList1.Items.Add(new ListItem("Select", ""));
DropDownList1.AppendDataBoundItems = true;
String strConnString = ConfigurationManager.ConnectionStrings["your connection"].ConnectionString;
String strQuery = "SELECT ID, Name FROM YourTable";
SqlConnection con = new SqlConnection(strConnString);
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = strQuery;
cmd.Connection = con;
try
{
con.Open();
DropDownList1.DataSource = cmd.ExecuteReader();
DropDownList1.DataTextField = "Name";
DropDownList1.DataValueField = "ID";
DropDownList1.DataBind();
}
catch (Exception ex)
{
throw ex;
}
finally
{
con.Close();
con.Dispose();
}
/////只需在此处使用Asp:Repeater ///
<div class="col-sm-4 lhs-form-box" style="padding: 10px; height: 247px;">
<div class="form-group" style="width: 87%; float: left;">
<label for="sel1">ITEM</label>
<asp:Repeater ID="rptNames" runat="server" class="form-control">
<asp:Label Text='<%# Eval("name") %>'></asp: Label>
</asp: Repeater>
</div>
<span>
<img src="info.png" style="margin: 31px 0px 0px 5px"></span>
</div>
/////只需在此处使用Asp:Repeater
并在您的sql查询中,从yourTable中选择名称
在后面的代码中///
rptNames.DataSource = datatable; //来自sql rptNames.Databind();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.