从代码隐藏隐藏DetailsView中的字段 [英] Hiding fields in DetailsView from codebehind

查看:79
本文介绍了从代码隐藏隐藏DetailsView中的字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对不起,如果这是基本知识,我是.Net的新手,并且四处看看,但也许我没有搜索正确的术语.

Sorry if this is elementary, I'm new to .Net and have looked around, but maybe I'm not searching the right terms.

我有一个在设计视图中加载的DetailsView.我希望其中一些字段仅显示给某些用户.所以我在代码隐藏中思考,我可以隐藏其他字段.我还想在代码隐藏中更改其中一些字段的HeaderText.

I have a DetailsView that I loaded in design view. I'd like some of these fields to only show to certain users. So I was thinking in codebehind, I could hide the other fields. I'd also like to change the HeaderText for some of these fields in codebehind.

但是,我还需要这个DetailsView是可编辑的.如果这些字段对于某些用户是隐藏的,则我不确定这将如何影响任何事情.它将更新所有字段,甚至是隐藏的字段吗?

However, I also need this DetailsView to be editable. If the fields are hidden for some users, I wasn't sure how that would effect anything. Will it update all fields, even the hidden ones?

只有某些用户也应该看到编辑"按钮.

Only certain users should see the edit button as well.

有人对如何进行这样的事情有任何提示吗?

Does anyone have any tips on how to go about something like this?

推荐答案

在页面类中,创建一个布尔值字段以表示控件是否应可见,并在page_load中设置此值.(注意:Authentication.IsAuthorized只是如何确定该字段的示例,请将其替换为您自己的代码)

In your page class create a boolean field to represent whether the control should be visible and set this value in page_load. (Note: Authentication.IsAuthorized is just an example of how to determine the field, replace this with your own code)

public partial class MyPage : System.Web.UI.Page
{
    protected bool showField = false;
    protected void Page_Load(object sender, EventArgs e)
    {
        showField = Authentication.IsAuthorized(User.Identity.Name);
    }
}

现在绑定到控件中的该字段.请注意,这需要使用模板字段而不是绑定字段.

Now bind to this field in your control. Note that this requires the use of template fields rather than bound fields.

<asp:Label ID="lblHiddenField" runat="server" Visible='<%# showField %>' />

这同样适用于您的按钮,只要它们在模板字段中.过去,我曾使用绑定字段和一些讨厌的foreach循环来查找要隐藏的控件,但老实说,这是执行此恕我直言的更直接的方法.

This will work for your buttons as well provided they're in a template field. I've used bound fields in the past and some nasty foreach loops to find the controls I want to hide, but honestly this is the more straight-forward way to do this IMHO.

这篇关于从代码隐藏隐藏DetailsView中的字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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