【问题标题】:View button with if else statement带有 if else 语句的查看按钮
【发布时间】:2017-07-06 21:16:22
【问题描述】:

我有一个gridview,我想添加一个按钮'VIEW',它重定向到(Customers.aspx?CustomerID=)页面。每个客户都有一个 ID,每个 ID 都与一个报告相关联。我不希望 ID 显示在我的 gridview 中,我希望它被隐藏。 另外,我希望仅当用户是“管理员”或“销售人员”类别时才显示此按钮。我有办法将我的 CustomerID 绑定到 UserCategory 以分别为每个 ID 生成报告。

我也尝试查看其他一些帖子,但我的按钮由于某种原因没有重定向。 有人可以帮我弄清楚我在这里做错了什么,或者我是否遗漏了什么。任何帮助将不胜感激。 谢谢。

    <asp:TemplateField ShowHeader="False">
             <ItemTemplate>
                 <asp:Button ID="btnView" runat="server" CausesValidation="false" CommandName="Select" Text="VIEW"  />
              </ItemTemplate>
              <ControlStyle CssClass="button" />
           </asp:TemplateField>



 protected void btnView(object sender, EventArgs e)
{
    Button button = (Button)sender;
    GridViewRow row = (GridViewRow)button.NamingContainer;
    Label lblCustomerID = (Label)row.FindControl("lblCustomerID");

    txtHFUserCategory.Value = Session["UserCategoryPC"].ToString();
    String strUserCat = MTProcs.GetSingleDatabaseResult("SELECT tblUsers.UserCategoryID FROM tblUsers WHERE UserID = " + txtHFUserID.Value);
    if (strUserCat == "4" || strUserCat == "12")
    {
        ScriptManager.RegisterStartupScript(Page, typeof(Page), "OpenWindow", "window.open(Customers.aspx?CustomerID=" + lblLeadID.Text + "');", true);

    }
}

【问题讨论】:

  • 用户和客户表在数据库中是如何链接的?
  • 您需要加入这两个表。否则逐行查询子表效率不高。
  • 您希望&lt;asp:Button&gt; 存在于GridView 中吗? (即在列的单元格内)还是您希望它只是在页面上的某个地方?答案将决定您是否需要在 GridView 上使用 _RowDataBound() 事件处理程序,或者真的只需要在页面上使用 if-then 来显示或隐藏按钮。
  • 另外,我现在想知道这个问题是否与如何添加按钮的混淆有关,或者更多的是你被困在如何使用查询进行连接......如果是后者,您需要从CUSTOMER 表中对CATEGORY 表的ID (我假设这是一个PK)有一个FK,然后在查询中执行一个简单的联接来选择所有客户信息当 Customer. == Category.ID
  • 你是说按钮是否可见取决于当前会话用户是谁,以及他们在用户表中的类别值是什么?然后您还希望gridview 中的每一行都有一个按钮,对吗?每一行都是不同的客户?所以按钮将您带到该客户的页面,您也不知道如何让每个按钮知道它与哪个客户相关?我认为这里有两个稍微不同的问题。

标签: c# asp.net button grid response.redirect


【解决方案1】:

您好,您只需在按钮中添加可见属性即可 喜欢

<asp:button ID="yourBtnID" runat="server" Text="WotEver" visible='<%# Convert.toInt(Eval("admin"))==1 && Convert.toInt(Eval("SALESPERSON"))==1 %>'/>

【讨论】:

    猜你喜欢
    • 2018-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-03
    • 1970-01-01
    相关资源
    最近更新 更多