我正在尝试根据用户选择的六个按钮中的哪一个将 CSS 用户首选项保存到我的数据库中。为了做到这一点,我试图为每个按钮点击事件分配一个整数值;无论单击哪个,都会将相应的整数作为参数传递给我的数据访问对象以更新数据库。
我的方法是这样的:
protected void SetCSS(object sender, EventArgs e)
{
Users setCss = new Users();
if (IsPostBack)
{
if (sender.ToString() == "Blue")
{
setCss.StylePreference = 0;
}
else if (sender.ToString() == "Khaki")
{
setCss.StylePreference = 1;
}
else if (sender.ToString() == "Night")
{
setCss.StylePreference = 2;
}
else if (sender.ToString() == "Pink")
{
setCss.StylePreference = 3;
}
else if (sender.ToString() == "White")
{
setCss.StylePreference = 4;
}
else if (sender.ToString() == "Yellow")
{
setCss.StylePreference = 5;
}
setCss.UserLoginName = Session["eMail"].ToString(); // current user
setCss.SetStylePreference(setCss.UserLoginName, setCss.StylePreference);
}
在每个按钮的点击事件中:
protected void btnBlue_Click(object sender, EventArgs e)
{
SetCSS(btnBlue, null);
}
protected void btnKhaki_Click(object sender, EventArgs e)
{
SetCSS(btnKhaki, null);
}
ETC...
I put a watch on the sender object and, when the Pink button is selected, the value assigned to the sender reads {Text="Pink"} However, as I step through the if statement in the SetCSS method, when I come to the else if (sender.ToString() == "Pink") 条件不满足,而不是按照应有的方式将样式偏好设置为 3,程序继续到语句的末尾,最后总是分配一个值0 到属性。
我究竟做错了什么?非常感谢帮助...