none
管理员权限设置为何不成功呢,求大神解救 RRS feed

  • 问题

  •  if (e.CommandName == "role")
            {
                string UserName = e.CommandArgument.ToString();
                string roleText = ((Button)(e.Item.FindControl("btnSetRole"))).Text.ToString();
                bool role = (roleText == "取消管理员权限") ? true : false;
                string sqlStr = "update tb_User set Role='" + role + "' where UserName='" + UserName + "'";
                int reValue = db.sqlEx(sqlStr);
                if (reValue == 0)
                    Response.Write("<script>alert('管理员设置失败!');</script>");
                DataListBind();
            }

    设置了Role类型为bit,

    前台代码

    <asp:Button ID="btnSetRole" runat="server" CommandName="role" Text='<%# Trans2Bool(DataBinder.Eval(Container.DataItem, "Role"))

    == true ? "取消管理员权限" : "设为管理员权限" %>' CommandArgumet = '<%# DataBinder.Eval(Container.DataItem,"UserName") %>' CausesValidation="False" />


    为什么一直实现不了设置或取消管理员的功能呢

    2014年3月19日 7:01

答案

  • SQLSERVER Role类型为bit,ture和false 分别对应1和0, 那你尝试这样的代码:

    int role = (roleText == "取消管理员权限") ? 0: 1;
                string sqlStr = "update tb_User set Role=" + role + " where UserName='" + UserName + "'";
                int reValue = db.sqlEx(sqlStr);
                if (reValue <= 0)
                    Response.Write("<script>alert('管理员设置失败!');</script>");
    



    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place. &lt;br/&gt; Click &lt;a href=&quot;http://support.microsoft.com/common/survey.aspx?showpage=1&amp;scid=sw%3Ben%3B3559&amp;theme=tech&quot;&gt; HERE&lt;/a&gt; to participate the survey.


    2014年3月21日 2:06
    版主

全部回复

  •  你好,

    你前台逻辑是 Text='<%# Trans2Bool(DataBinder.Eval(Container.DataItem, "Role"))== true ? "取消管理员权限" : "设为管理员权限" %>' , 也就是说Role=true时 text='取消管理员权限', 当role=false时text='设为管理员权限'.

      bool role = (roleText == "取消管理员权限") ? true : false;
                string sqlStr = "update tb_User set Role='" + role + "' where UserName='" + UserName + "'";
    

    在后台代码中, 当role=true, 你更新回去仍然是true, 你的逻辑有问题。

    你能尝试这样试试:

    bool role = (roleText == "取消管理员权限") ?false: true  ;



    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place. &lt;br/&gt; Click &lt;a href=&quot;http://support.microsoft.com/common/survey.aspx?showpage=1&amp;scid=sw%3Ben%3B3559&amp;theme=tech&quot;&gt; HERE&lt;/a&gt; to participate the survey.

    2014年3月20日 3:14
    版主
  • 谢谢,我已经修改了,但是依然无法实现该功能,是不是bool类型无法转换为bit类型呢?我设置的SQLSERVER Role类型为bit

    if (e.CommandName == "role")
            {
                string UserName = e.CommandArgument.ToString();
                string roleText = ((Button)(e.Item.FindControl("btnSetRole"))).Text.ToString();
                bool role = (roleText == "取消管理员权限") ? false : true;
                string sqlStr = "update tb_User set Role='" + role + "' where UserName='" + UserName + "'";
                int reValue = db.sqlEx(sqlStr);
                if (reValue == 0)
                    Response.Write("<script>alert('管理员设置失败!');</script>");
                DataListBind();
            }

    2014年3月20日 7:14
  • SQLSERVER Role类型为bit,ture和false 分别对应1和0, 那你尝试这样的代码:

    int role = (roleText == "取消管理员权限") ? 0: 1;
                string sqlStr = "update tb_User set Role=" + role + " where UserName='" + UserName + "'";
                int reValue = db.sqlEx(sqlStr);
                if (reValue <= 0)
                    Response.Write("<script>alert('管理员设置失败!');</script>");
    



    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place. &lt;br/&gt; Click &lt;a href=&quot;http://support.microsoft.com/common/survey.aspx?showpage=1&amp;scid=sw%3Ben%3B3559&amp;theme=tech&quot;&gt; HERE&lt;/a&gt; to participate the survey.


    2014年3月21日 2:06
    版主