Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net detailsview中的隐藏字段-aspnet成员身份用户ID_Asp.net_Detailsview_Boundfield - Fatal编程技术网

Asp.net detailsview中的隐藏字段-aspnet成员身份用户ID

Asp.net detailsview中的隐藏字段-aspnet成员身份用户ID,asp.net,detailsview,boundfield,Asp.net,Detailsview,Boundfield,我有一个小细节视图,可以在其中向数据库添加一些数据 detailsview的代码: <asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False" DataSourceID="orderSqlDataSource" DefaultMode="Insert" Height="50px" Width="333px" DataKeyNames="ID" CellPadding="4" For

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

detailsview的代码:

<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>
如您所见,我填充了页面加载中的第一个边界字段。 问题是第一个边界字段(userid,或者在我的母语“azonosító”)很重要,因为数据库很重要,但我不想让用户看到他/她的userid。 如果我将boundfield隐藏(visible=false),那么页面加载中的代码将无法访问它。 当字段被隐藏时,如何将当前用户ID绑定到该字段,或者如何在没有ID的boundfield的情况下获得此工作


提前谢谢

使用模板字段并放置标签,然后将其与userid绑定。您可以访问codebehind中的标签,而不管它是否不可见

<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=((标签)dv.Rows[0].FindControl(“lb”)).Text();//给出正确的索引

使用模板字段,放置标签,并用userid绑定。您可以访问codebehind中的标签,而不管它是否不可见

<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=((标签)dv.Rows[0].FindControl(“lb”)).Text();//给出正确的索引

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

代码:


正如您所料,此方法使BoundField完全不必要:)

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

代码:


正如您所料,此方法使BoundField完全不必要:)

这是一个有趣的解决方案,但根据提出的问题,根本没有理由在DetailsView中存储userid值。更多信息请参见我的答案…谢谢Janjua,您的解决方案也很好,但由于FindControl,它需要更多的魔力,我更喜欢简单的方法。无论如何,也谢谢你@伪编码器是的,你是对的,我看到了你的解决方案,它比我的解决方案好得多+1@Waqar我将最后一行代码更改为在标签上使用Text()方法,而不是ToString()-我相信这就是您需要的。另外+1因为您的方法可以在某些情况下使用这是一个有趣的解决方案,但是根据提出的问题,根本没有理由在DetailsView中存储userid值。更多信息请参见我的答案…谢谢Janjua,您的解决方案也很好,但由于FindControl,它需要更多的魔力,我更喜欢简单的方法。无论如何,也谢谢你@伪编码器是的,你是对的,我看到了你的解决方案,它比我的解决方案好得多+1@Waqar我将最后一行代码更改为在标签上使用Text()方法,而不是ToString()-我相信这就是您需要的。另外+1,因为您的方法可以在某些情况下使用很好的解决方案!这正是我需要的,我也学到了一些新东西。谢谢很好的解决方案!这正是我需要的,我也学到了一些新东西。谢谢
    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);
        }
    }