英文:
How to add a space for a DropDown Text which combines two columns
问题
我有一个asp.NET WebForm应用程序。
表单上有一个下拉菜单。我想让下拉菜单文本显示表中两个字段的组合。coulmnA(年龄)和columnB(姓名)。
这是我的代码:
DataSet d =GetDateSet();
EnumerableRowCollection a = from t in d.Tables[0].AsEnumerable()
select new
{ txt = t[0] +"___ " + t[1] , val = t[0]};
ddlCustomer.DataSource = a;
ddlCustomer.DataBind();
ddlCustomer.Items.Insert(0, "选择客户");
到目前为止,这是一个简单的任务。下拉菜单将显示类似以下内容:
24___Joe
25___Biden
26___Donald
27___Trump
问题是,如果我尝试用空格替换下划线,它会被忽略。有什么办法解决这个问题吗?
具体来说,下面的代码不会添加空格:
EnumerableRowCollection a = from t in d.Tables[0].AsEnumerable()
select new
{ txt = t[0] +" " + t[1] , val = t[0] };
英文:
I have a asp.NET webform app.
There is a drop down on the form. I want the drop down text show combination of
2 fields in a table. coulmnA(age) and columnB(Name).
Here is my code:
DataSet d =GetDateSet();
EnumerableRowCollection a = from t in d.Tables[0].AsEnumerable()
select new
{ txt = t[0] +"___ " + t[1] , val = t[0]};
ddlCustomer.DataSource = a;
ddlCustomer.DataBind();
ddlCustomer.Items.Insert(0, "Select Customer Here");
So far above is an easy task. Dropdown will show something like below:
24___Joe
25___Biden
26__Donald
27___Trump
The problem is if I try to replace underlines with space it will be ignored. Any ideas how to fix this?
To be specific below code will not add space:
EnumerableRowCollection a = from t in d.Tables[0].AsEnumerable()
select new
{ txt = t[0] +" " + t[1] , val = t[0] };
答案1
得分: 3
以下是翻译好的部分:
在“标记”中,普通空格当然不会显示。
所以,假设我有一个包含酒店名称的下拉菜单。
但是,我想要两列,并包括城市。
因此,这个标记:
<asp:DropDownList ID="cboHotels" runat="server"
style="font-family:Courier New, Courier, monospace;height:22px"
DataValueField="ID"
DataTextField="MyHotelName">
</asp:DropDownList>
请注意我使用了一个固定的空格。现在,您可以在文本中包含 "nbsp"(不间断空格)。您还必须强制服务器端代码以确保 HTML 输出编码。
所以,假设这个代码:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
LoadData();
}
void LoadData()
{
string strSQL =
@"SELECT ID, HotelName, City FROM tblHotelsA
ORDER BY HotelName";
DataTable rstData = General.MyRst(strSQL);
// 合并酒店名称和城市
rstData.Columns.Add("MyHotelName");
foreach (DataRow dr in rstData.Rows)
{
dr["MyHotelName"] = MyPad(dr["HotelName"], 20) + MyPad(dr["City"], 15);
}
cboHotels.DataSource = rstData;
cboHotels.DataBind();
cboHotels.Items.Insert(0, new ListItem("Select Hotel", ""));
}
string MyPad(object MyColum,int NumChars)
{
string sResult = "";
if (MyColum != null)
sResult = ((string)MyColum);
sResult = sResult.PadRight(NumChars).Substring(0, NumChars);
sResult = sResult.Replace(" ", " ");
return Server.HtmlDecode(sResult);
}
因此,为了减少一些 "混乱" 的代码,我使用了一个辅助程序,它将字符串 "填充" 到我指定的长度。
结果现在如下:
因此,要使其工作,您需要使用 "nbsp"。
(不间断空格)。
当然,您还希望使用固定大小的字体 - 否则,额外的列将不与以前的列对齐。
通过将数据值保持为主键 ID,然后上面的允许多列,但选择的值仍然是 "数据值" 隐藏列,本例中为 "id"(酒店数据库主键行 ID)。
英文:
Well, simple spaces in "markup" of course don't show.
So, say I have a drop down with hotel name.
but, I want 2 columns, and to include the city.
So, this markup:
<asp:DropDownList ID="cboHotels" runat="server"
style="font-family:Courier New, Courier, monospace;height:22px"
DataValueField="ID"
DataTextField="MyHotelName">
</asp:DropDownList>
Note how I am using a fixed space. So, you can now include "nbsp" in the text (non breaking spaces). You also have to force the server side code to ensure html output encoding.
So, say this code:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
LoadData();
}
void LoadData()
{
string strSQL =
@"SELECT ID, HotelName, City FROM tblHotelsA
ORDER BY HotelName";
DataTable rstData = General.MyRst(strSQL);
// combine hotel name and city
rstData.Columns.Add("MyHotelName");
foreach (DataRow dr in rstData.Rows)
{
dr["MyHotelName"] = MyPad(dr["HotelName"], 20) + MyPad(dr["City"],15);
}
cboHotels.DataSource = rstData;
cboHotels.DataBind();
cboHotels.Items.Insert(0, new ListItem("Select Hotel", ""));
}
string MyPad(object MyColum,int NumChars)
{
string sResult = "";
if (MyColum != null)
sResult = ((string)MyColum);
sResult = sResult.PadRight(NumChars).Substring(0, NumChars);
sResult = sResult.Replace(" ", "&nbsp;");
return Server.HtmlDecode(sResult);
}
So, to save some "messy" code, I use a helper routine, and it "pads" out the string to the length I specify.
The result is now thus this:
so, to make this work, you need to use "nbsp".
(non breaking space).
You also want of course to use a fixed sized font - else the extra column will not line up with previous.
And by keeping the data value as the PK id, then above allows several columns, but the selected value remains the "data value" hidden column, which in this case is "id" (the hotel database PK row id).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论