如何根据数据库值隐藏/显示按钮 [英] How to hide/show button based on database value

查看:195
本文介绍了如何根据数据库值隐藏/显示按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I have a gridview and this Code(down below) I want to Show the button if the Status is open, otherwise the button should be invisible.
But this Code, does not work. The button is always visible.





我尝试过:





What I have tried:

<pre lang="c#"> protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (((GridView)sender).SelectedRow != null)
        {
            GridViewRow g = ((GridView)sender).SelectedRow;

            string assign= GridView1.SelectedRow.Cells[5].Text;

            btnassign.Visible = (assign == "open");
        }

    }



















<asp:Button Text="assign" ID="btnassign"  Visible="false" OnClick="btnassign_Click" runat="server" />

推荐答案

你可以用不同的代码方式做。



1.根据你的逻辑从数据库中获取状态值为True / False。然后在gridview的项目模板中添加一个按钮在其可见属性中,您可以通过Eval的帮助直接设置状态值

Ex:



You can do in different way of code.

1. Get the status value from the database as True/False as per your logic.Then inside item template of gridview add one button and in its visible properties you can directly set the status value by the help of "Eval"
Ex:

<asp:GridView ID="gv" runat="server" AutoGenerateColumns="False" 

                Width="486px" >
                <columns>
                    <asp:TemplateField HeaderText="Show Button">
                        <itemtemplate>
                            
                            <asp:Button runat="server" Visible='<%# Eval("Status") %>' Text="ButtonName" >











2.您可以使用网格视图gv_RowDataBound方法。



在此网格视图中,在项目模板的帮助下将状态值绑定在隐藏字段中。然后从 gv_RowDataBound 中找到隐藏字段值方法和pe状态显示按钮。



Ex:






2. You can use the grid view gv_RowDataBound method.

Inside this grid view bind the status value in a hidden field by the help of item template.Then find the hidden field value from gv_RowDataBound method and as per the status show the button.

Ex:

protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
        {

            foreach (GridViewRow rowInfo in gv.Rows)
            {
                
              HiddenField  hfFProductId = (HiddenField)rowInfo.FindControl("hdfId");
              Button buttonID = (Button)rowInfo.FindControl("buttonID");
              if (hfFProductId=="Open") buttonID.Visible=true;
              else buttonID.Visible=false;
                
                            }
        }





查看此代码可能会对您有所帮助。

快乐编码..



Check this code it may help you.
Happy coding..


检查内联评论



Check inline comments

protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            int statusColumnIndex = 5;  // ensure the correct column index (zero based index )
            string assign = GridView1.SelectedRow.Cells[statusColumnIndex].Text;
            assign = assign.Trim().ToLower(); // trim for extra spaces, and lower the case for exact comparison 
            btnassign.Visible = (assign == "open");
        }





如果您使用绑定字段 column。

如果您使用 template 列,例如标签控件然后使用



The above will work if you are using bound field column.
if you are using template column, for example a label control in it, then use

string assign = (GridView1.SelectedRow.FindControl("lblStatusID") as Label).Text;





首先你应该在里面放一个断点该事件并探索 SelectedRow 对象并查看其中的内容,这是解决任何问题的最佳和最有效的方法。

参考本文调试掌握调试Visual Studio 2010 - 初学者指南 [ ^ ]



First of all you should put a break point inside the event and explore the SelectedRow object and see actually what you are getting inside it, this is the best and effective way to solve any issue.
refer this article for debugging Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]


这篇关于如何根据数据库值隐藏/显示按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆