[英]how to get multiple values from text-box in asp.net
I have text-box that contain multiple names that are separated by comma. 我有一个文本框,其中包含多个用逗号分隔的名称。 I want to get each name from the text-box and insert into database. 我想从文本框中获取每个名称,然后插入数据库中。 How can i do this? 我怎样才能做到这一点? Names in the text-box are like this: 文本框中的名称如下所示:
Mike Drew, Andy Moore, John Edward and so on...
asp textbox: ASP文本框:
<asp:TextBox ID="txtSearch" runat="server" CssClass="form-control" ></asp:TextBox>
<asp:HiddenField ID="hfUID" runat="server" />
<asp:Button ID="Button1" Text="Submit" runat="server" OnClick="Submit" />
code behind 背后的代码
protected void Submit(object sender, EventArgs e)
{
string customerName = Request.Form[txtSearch.UniqueID];
string UID = Request.Form[hfUID.UniqueID];
ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Name: " + customerName + "\\nID: " + UID + "');", true);
}
You can use LINQ to seperate the comma seperated values in the textbox like this: 您可以使用LINQ在文本框中分隔逗号分隔的值,如下所示:
string[] str = customerName.Split(',').Select(x => x.Trim()).ToArray();
and then you can add the values from the string array to the database. 然后可以将字符串数组中的值添加到数据库中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.