各位大侠好!!
我现在有一个前台注册页面,有一个是兴趣爱好项,为CheckBox多项选择,存入数据库时存的是它的Value,如:前台页面选择项为看书,听音乐,存入数据库的则是1;2(1对应看书,2对应听音乐,中间还有个分号 - -!)
我用GridView读取数据也是读的1;2,有没有办法在读取时让它显示为看书,听音乐!!
谢谢了!!急~~
我现在有一个前台注册页面,有一个是兴趣爱好项,为CheckBox多项选择,存入数据库时存的是它的Value,如:前台页面选择项为看书,听音乐,存入数据库的则是1;2(1对应看书,2对应听音乐,中间还有个分号 - -!)
我用GridView读取数据也是读的1;2,有没有办法在读取时让它显示为看书,听音乐!!
谢谢了!!急~~
还是复选框打勾的效果
前者麻烦点,后者简单
1,前者需要转换成字符串数组后再读取
2.后者直接转着数组就可以绑定了
string str=...;
foreach(listItem li in chk.items)
{
if(str==li.value)
li.checked=true;
}
逻辑判断自己想
我是想在后台写个方法,在前台GridView绑定时调用这个方法。
public string[] str(string array)
{
string tempstring;
foreach(string s in array)
{
tempstring+=....//通过sql语句查询出来的名字,,select name from table where id=s
tempstring+=tempstring+";";
}
return tempstring
}
在rowdatabound里,判断,row[i].cell[j].text中的值,若是1 ,则把它重新赋值
if(row[i].cell[j].text==1)
{
row[i].cell[j].text=“tingyinyue
}
呵呵,不知道对不对。
只放值的话
if(row[e.RowIndex].cell[列位置].text==1)
{
row[e.RowIndex].cell[列位置].text=“tingyinyue
}
还可以在aspx页面调用cs方法,就是你吧“1,2”传进这个方法处理后
return你想要的结果就可以
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType==DataControlRowType.DataRow)
{
if (e.Row.Cells[4].Text.Trim() == "1")//注意cell[]里面的取值
{
e.Row.Cells[4].Text = "<font color=red>看书</font>";
}
else
{
e.Row.Cells[4].Text = "听音乐";
}
}
}
string []arr=绑定.split(new char[]{':'})
arr[0]=看书
arr[i]=听音乐
GridView1.DataKeyNames = new string[] { "UserId" };
GridView1.DataBind();
在<asp:TemplateField HeaderText="兴趣">
<ItemTemplate>
<asp:Label ID="lblInterest" runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
然后在
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
int UserId= Convert.ToInt32((GridView1.DataKeys[e.Row.RowIndex]["UserId"]).ToString());
Label lblInterest= (Label)e.Row.FindControl("lblInterest") as Label;
UserBL userBL = new UserBL();
SqlDataReader dr = userBL.SelectInterest(UserId);//多表连接查询Interest,User_Interest
string s = "";
while (dr.Read())
{
s +="["+ dr["name"].ToString()+"]"+"";
}
lblInterest.Text =s;
}
}
以后如果增加或者修改兴趣,程序代码不用改!