gridview中的查看按钮对应于用户catergory [英] View button in gridview corresponding to a user catergory

查看:58
本文介绍了gridview中的查看按钮对应于用户catergory的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个gridview,我想添加一个按钮'VIEW',它重定向到链接到报告的(Customers.aspx?CustomerID =)页面。每个客户都有一个ID,每个ID都与一个报告相关联。



我不希望ID显示在我的gridview中,我希望它被隐藏。此外,我希望仅当USER是管理员或销售人员类别时才会显示此按钮。有没有办法我可以将我的CustomerID绑定到UserCategory以分别为每个ID生成报告。



我也试过看其他帖子但我的按钮没有由于某种原因重定向。有人可以帮我弄清楚我在这里做错了什么,或者我错过了什么。此外,有一个更简单的方法这样做任何帮助将不胜感激。谢谢。



我尝试了什么:



< asp:TemplateField ShowHeader =False> 
< ItemTemplate>
< asp:Button ID =btnViewrunat =serverCausesValidation =falseCommandName =SelectText =VIEW/>
< / ItemTemplate>
< ControlStyle CssClass =button/>
< / asp:TemplateField>







 protected void btnView(object sender,EventArgs e )
{
txtHFUserCategory.Value = Session [UserCategoryPC]。ToString();


if(txtHFUserCategory.Value ==A|| txtHFUserCategory.Value ==B)

{

按钮btn =(按钮)发件人;
GridViewRow row =(GridViewRow)btn.NamingContainer;

Label lblCustomerID =(Label)row.FindControl(lblCustomerID);
String strCustomerID = lblCustomerID.Text;
Session [UserID] = txtHFUserID.Value;
Response.Redirect(Customer.aspx?CustomerID =);

}


}

解决方案



如何使用gridview,隐藏ID,使用组件的 DataKeyNames 属性。你没有看到id。

 <   asp :GridView     ID   =  grd_result    DataKeyNames   =  id    runat   =  server    OnRowCommand   =  grd_result_RowCommand >  



要激活按钮,请不要在TemplateField中使用按钮组件,请使用 ButtonField 组件,更容易。此按钮由 OnRowCommand 事件执行。

您可以在网格中使用许多ButtonField,以使用 CommandName 属性区分它们。

 <   asp:ButtonField     ButtonType   = 链接    ItemStyle-Horizo​​ntalAlign   =  中心    ItemStyle-Width   =  100px    CommandName   =  view    HeaderText   =    文字  = 查看    ControlStyle-BorderColor   = 黑色    /  >  



以下是帮助您的示例。

HTML代码

 <   div  >  
< asp:GridView < span class =code-attribute> ID = grd_result DataKeyNames = id runat < span class =code-keyword> = server EmptyDataText = :: No Data :: AutoGenerateColumns = 错误 BorderStyle = 实体 OnRowCommand = grd_result_RowComman d FooterStyle-CssClass = GridFooter AllowPaging = false ShowFooter = True >
< >
< asp:ButtonField ButtonType = 链接 ItemStyle-Horizo​​ntalAlign = 中心 ItemStyle-Width = 100px CommandName = view < span class =code-attribute> HeaderText = 文字 = 查看 ControlStyle-BorderColor = 黑色 / >
< asp:BoundField HeaderText < span class =code-keyword> =
名称 datafield = name HeaderStyle-Horizo​​ntalAlign = / >
< /列 >
< / asp:GridView >
< span class =code-keyword>< / div >
< br / >
< asp:标签 ID = lblmsg runat = server 文字 = - > < / asp:Label >



CODE-BEHIND

< pre lang =c#> protected void Page_Load( object sender,EventArgs e)
{
if (!IsPostBack)
{
// 使用数据填充gridview。
DataTable dt = new DataTable();
dt.Columns.Add( id); dt.Columns.Add(< span class =code-string> name);

DataRow dr = dt.NewRow();
dr [ id] = 1; dr [ name ] = John; dt.Rows.Add(dr);
dr = dt.NewRow();
dr [ id] = 2; dr [ name ] = Mary; dt.Rows.Add(dr);
dr = dt.NewRow();
dr [ id] = 3; dr [ name] = Sarah; dt.Rows.Add(dr);
dr = dt.NewRow();
dr [ id] = 4; dr [ name ] = Peter; dt.Rows.Add(dr);

this .grd_result.DataSource = dt;
this .grd_result.DataBind();
}
}

private bool user_permission( string user)
{
// 此处,验证用户是管理员还是销售员的代码
// 这个信息通常记录在数据库中。
return true ;
}

protected void grd_result_RowCommand( object sender,GridViewCommandEventArgs e)
{
try
{
// this.Page.User.Identity.Name。如果使用用户和密码对系统进行身份验证,则使用此代码。
if this .user_permission( this .Page.User.Identity.Name))
{
// 找到名称按钮。
string current_command = e.CommandName ;
// 找到我点击的行的索引。
int current_row_index = Int32 .Parse(e.CommandArgument.ToString());
// 找到该行的ID。
string id = this .grd_result.DataKeys [current_row_index] .Value.ToString();

switch (current_command)
{
case view
{
this .lblmsg.Text = 我的ID是 + id + 我的名字是 + this .grd_result。行[current_row_index] .Cells [ 1 ]。文本;
break ;
}
}
}
其他 { .lblmsg.Text = 您无权执行此任务。;}
}
catch (例外情况)
{
.lblmsg.Text = 错误: + ex.Message;
}
}


隐藏列

 int columnToHide = 4; 
if(role ==Admin){
GridView1.Columns [columnToHide] .Visible = false;
}





您应使用隐藏字段而不是标签隐藏和存储数据


I have a gridview and I want to add a button 'VIEW' which redirects to the (Customers.aspx?CustomerID=) page which links to a report. Every customer has a ID and each ID is associated with a report.

I don't want the ID to show up in my gridview and I want it to be hidden. Also, I want this button to show up only if the USER is an "Admin" or a "Salesperson" category. Is there a way i can bind my CustomerID to a UserCategory to generate reports for each ID separately.

I tried looking at some other posts as well but my button doesn't redirect for some reason. Can someone please help me figure out what i am doing wrong here or if i am missing something. Also, is there a simpler way of doing this Any help will be greatly appreciated. Thank you.

What I have tried:

<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)
   {
       txtHFUserCategory.Value = Session["UserCategoryPC"].ToString();


       if (txtHFUserCategory.Value == "A" || txtHFUserCategory.Value == "B" )

       {

           Button btn = (Button)sender;
           GridViewRow row = (GridViewRow)btn.NamingContainer;

           Label lblCustomerID = (Label)row.FindControl("lblCustomerID");
           String strCustomerID = lblCustomerID.Text;
           Session["UserID"] = txtHFUserID.Value;
           Response.Redirect("Customer.aspx?CustomerID=" );

       }


   }

解决方案

Hi,
How you are using a gridview, to hide the ID, use the DataKeyNames property of the component. You do not see the id.

<asp:GridView ID="grd_result" DataKeyNames="id" runat="server" OnRowCommand="grd_result_RowCommand">


To activate the button, do not use a button component in the TemplateField, use a ButtonField component, it is easier. This button is executed by the OnRowCommand event.
You can have many ButtonFields in your grid, to differentiate them use CommandName property.

<asp:ButtonField ButtonType="Link" ItemStyle-HorizontalAlign="Center" ItemStyle-Width="100px" CommandName="view" HeaderText="" Text="View" ControlStyle-BorderColor="Black" />


Here is an example to help you.
HTML CODE

<div>
        <asp:GridView ID="grd_result" DataKeyNames="id" runat="server" EmptyDataText=":: No Data ::" AutoGenerateColumns="False" BorderStyle="Solid" OnRowCommand="grd_result_RowCommand" FooterStyle-CssClass="GridFooter" AllowPaging="false"  ShowFooter="True">
            <Columns>
                <asp:ButtonField ButtonType="Link" ItemStyle-HorizontalAlign="Center" ItemStyle-Width="100px" CommandName="view" HeaderText="" Text="View" ControlStyle-BorderColor="Black" />
                <asp:BoundField HeaderText="Name"  datafield="name" HeaderStyle-HorizontalAlign="Left" />
            </Columns>
        </asp:GridView>    
    </div>
    <br />
    <asp:Label ID="lblmsg" runat="server" Text="-"></asp:Label>


CODE-BEHIND

protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //Fill the gridview with data.
                DataTable dt = new DataTable();
                dt.Columns.Add("id");dt.Columns.Add("name");

                DataRow dr = dt.NewRow();
                dr["id"] = "1";dr["name"] = "John";dt.Rows.Add(dr);
                dr = dt.NewRow();
                dr["id"] = "2";dr["name"] = "Mary";dt.Rows.Add(dr);
                dr = dt.NewRow();
                dr["id"] = "3"; dr["name"] = "Sarah";dt.Rows.Add(dr);
                dr = dt.NewRow();
                dr["id"] = "4";dr["name"] = "Peter";dt.Rows.Add(dr);

                this.grd_result.DataSource = dt;
                this.grd_result.DataBind();
            }
        }

        private bool user_permission(string user)
        {
            //Here, the code that verify if the user is admin or Salesperson
            //This information is normally record in the database.
            return true;
        }

        protected void grd_result_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            try
            {
                //this.Page.User.Identity.Name. This code is used if you authenticate the system with an user and password.
                if (this.user_permission(this.Page.User.Identity.Name))
                {
                    //find the name button.
                    string current_command = e.CommandName;
                    //find the index of line that i clicked.
                    int current_row_index = Int32.Parse(e.CommandArgument.ToString());
                    //find the id the line.
                    string id = this.grd_result.DataKeys[current_row_index].Value.ToString();

                    switch (current_command)
                    {
                        case "view":
                            {
                                this.lblmsg.Text = "My id is " + id + " and my name is " + this.grd_result.Rows[current_row_index].Cells[1].Text;
                                break;
                            }
                    }
                }
                else{this.lblmsg.Text = "You don't have permission to execute this task.";}
            }
            catch (Exception ex)
            {
                this.lblmsg.Text = "Error: " + ex.Message; 
            }
        }


Hide the column

int columnToHide =4;
if(role=="Admin"){
GridView1.Columns[columnToHide].Visible = false;
}



You shall use Hidden field instead of Label to hide and store the data


这篇关于gridview中的查看按钮对应于用户catergory的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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