编写一段代码来拆分字符串“test”,并将Name的值赋给textbox,将ok的值赋给Checkbox。由于OK的值为YES,因此应选中该复选框
在.aspx页面中,
名称:(文本框)
Ok:(复选框)
在aspx.cs页面中,
protected void Page_Load (object sender, EventArgs e)
{
String test = “Name = ADP India Pvt LTD;OK = YES “;
}
发布于 2016-04-05 14:20:31
这只需要使用String.Split()
方法将现有字符串分成两个部分,然后检查每个部分的值:
// Example string
string test = "Name = ADP India Pvt LTD;OK = YES ";
// Split this into two sections (using the ';' as a delimiter)
var sections = test.Split(';');
// Now the first entry will be the name, so we need the section after
// the equals sign
Name.Text = sections[0].Split('=')[1].Trim();
// Based on the value of your "OK", determine if your checkbox should be checked
Ok.Checked = (sections[1].Split('=')[1].Trim()== "YES");
这不是最安全的例子,但它应该让您了解如何解决这样的问题。
如果你不介意使用LINQ,你可以通过将字符串映射到字典来更容易地解决这个问题:
// Example string
string test = "Name = ADP India Pvt LTD;OK = YES";
// Map each key (e.g. "Name") and value (e.g. "ADP Index Pvt LTD")
// to an entry in a dictionary
var dictionary = test.Split(';')
.ToDictionary(k => k.Split('=')[0].Trim(),
v => v.Split('=')[1].Trim());
// Now reference what you need by it's key
Name.Text = dictionary["Name"]; // yields "ADP India Pvt LTD";
Ok.Checked = dictionary["OK"] == "YES"; // checks the checkbox if "YES"
https://stackoverflow.com/questions/36428866
复制