【问题标题】:Hidden field in detailsview - aspnet membership userID详细信息视图中的隐藏字段 - aspnet 会员用户 ID
【发布时间】:2013-02-13 20:16:51
【问题描述】:

我有一个小细节视图,我可以在其中向我的数据库添加一些数据。

详情视图的代码:

<asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False" 
    DataSourceID="orderSqlDataSource" DefaultMode="Insert" Height="50px" 
    Width="333px" DataKeyNames="ID" CellPadding="4" ForeColor="#333333" 
    GridLines="None" oniteminserted="DetailsView1_ItemInserted" 
                onprerender="DetailsView1_PreRender">
    <CommandRowStyle BackColor="#E2DED6" Font-Bold="True" />
    <EditRowStyle BackColor="#E9ECF1" />
    <FieldHeaderStyle BackColor="#E9ECF1" Font-Bold="True" />
    <Fields>
        <asp:BoundField DataField="userid" HeaderText="Azonosító" 
            SortExpression="userid" ReadOnly="True" />
        <asp:BoundField DataField="quantity" HeaderText="Mennyiség (kg)" 
            SortExpression="quantity" />
        <asp:TemplateField HeaderText="Mit rendel" SortExpression="Mit rendel">
            <InsertItemTemplate>
                <asp:DropDownList ID="ordertypeDropDownList" runat="server" 
                    DataSourceID="ordertypeDDLSqlDataSource" DataTextField="name" 
                    DataSource='<%# Bind("ordertype") %>'
                    SelectedValue='<%# Bind("ordertype") %>'
                    DataValueField="ID">
                </asp:DropDownList>
                <asp:SqlDataSource ID="ordertypeDDLSqlDataSource" runat="server" 
                    ConnectionString="<%$ ConnectionStrings:dotnetConnectionString %>" 
                    SelectCommand="SELECT [name], [ID] FROM [product]"></asp:SqlDataSource>
            </InsertItemTemplate>
        </asp:TemplateField>
        <asp:CommandField ShowInsertButton="True" CancelText="Mégsem" 
            InsertText="Elküld" />
    </Fields>
    <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
    <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
    <PagerStyle BackColor="#284775" ForeColor="White" 
        HorizontalAlign="Center" />
    <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
</asp:DetailsView>

下面是 page_load 事件的代码:

protected void Page_Load(object sender, EventArgs e)
    {
        if (User.Identity.IsAuthenticated)
        {
            MembershipUser user = Membership.GetUser(User.Identity.Name);
            string userID = user.ProviderUserKey.ToString();
            ((TextBox)DetailsView1.Rows[0].Cells[1].Controls[0]).Text = userID;
        }
    }

如您所见,我从 page_load 填充了第一个 boundfield。 问题是第一个boundfield(userid,或者用我的母语“azonosító”)因为数据库很重要,但我不想让用户看到他/她的userid。 如果我将边界域隐藏(可见 = false),则无法使用 page_load 中的代码访问它。 我如何将当前的用户 ID 绑定到该字段,当该字段被隐藏时,或者我如何在没有 ID 的绑定字段的情况下完成这项工作?

提前致谢!

【问题讨论】:

    标签: asp.net detailsview boundfield


    【解决方案1】:

    如果用户不需要知道或与您正在插入的值进行交互,那么将参数值注入到DetailsView 发送到您的数据源控件的插入调用中是很容易的。只需使用DetailsView.Inserting 事件DetailsViewInsertEventArgs 参数,如图所示。

    代码:

        protected void DetailsView1_ItemInserting(object sender, DetailsViewInsertEventArgs e)
        {
            if (User.Identity.IsAuthenticated)
            {
                MembershipUser user = Membership.GetUser(User.Identity.Name);
                string userID = user.ProviderUserKey.ToString();
                e.Values.Add("userid", userID);
            }
        }
    

    如您所料,此方法完全不需要 BoundField :)

    【讨论】:

    • 不错的解决方案!正是我需要的,我也学到了一些新东西。谢谢!
    【解决方案2】:

    使用模板字段并放置标签并将其与用户ID绑定。您可以访问代码隐藏中的标签,无论它是不可见的。

    <asp:DetailsView   runat="server"  ID="dv" >
    <Fields>
            <asp:TemplateField Visible="false" >
                <ItemTemplate>
                    <asp:Label ID="lb" runat="server" Text='<%# Bind("userid") %>' />
                </ItemTemplate>
            </asp:TemplateField>
    </Fields>
    </asp:DetailsView> 
    
    // In your code behind 
    string userid = ((Label)dv.Rows[0].FindControl("lb")).Text(); // give the correct index
    

    【讨论】:

    • 这是一个有趣的解决方案,但根据提出的问题,根本没有理由将 userid 值存储在 DetailsView 中。有关更多信息,请参阅我的答案...
    • 感谢 Janjua,您的解决方案也很好,但由于 FindControl,它需要更多的魔力,我更喜欢简单的方法。无论如何也谢谢你!
    • @pseudocoder 是的,你是对的,我看到了你的解决方案,它比我的解决方案好得多。 +1
    • @Waqar 我将最后一行代码更改为在 Label 上使用 Text() 方法而不是 ToString() - 我相信这就是您所说的。也 +1,因为您的方法可以在某些情况下使用
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-28
    • 1970-01-01
    • 2022-10-13
    • 1970-01-01
    • 2010-12-06
    相关资源
    最近更新 更多