0

DB にデータを追加できる小さな詳細ビューがあります。

詳細ビューのコード:

<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 から最初のバインド フィールドを埋めます。問題は、最初の境界フィールド (userid、または私の母国語で "azonosító") がデータベースのために重要であるということですが、ユーザーに自分のユーザー ID を見せたくないのです。バインドされたフィールドを非表示 (visible=false) にすると、page_load のコードでは到達できなくなります。フィールドが非表示の場合、現在のユーザーIDをそのフィールドにバインドするにはどうすればよいですか、またはIDのバインドフィールドなしでこの作業を行うにはどうすればよいですか?

前もって感謝します!

4

2 に答える 2

4

挿入する値をユーザーが認識したり操作したりする必要がない場合は、 が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 を完全に不要にします:)

于 2012-08-14T21:29:51.543 に答える
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
于 2012-08-14T21:05:11.300 に答える