0

I have a little detailsview where i can add some data to my DB.

The detailsview's code:

<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>

Here is the code behind for the page_load event:

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;
        }
    }

As you can see i fill the first boundfield from the page_load. The problem is that the first boundfield (userid, or in my native language "azonosító") is important because of the database, but i don't want to let the user see his/her userid. If i make the boundfield hidden (visible=false), it won't be reachable with the code in the page_load. How can i bound the current userID to that field, when the field is hidden, or how can i get this work without boundfield for the ID?

Thanks in advance!

amman
  • 142
  • 4
  • 12

2 Answers2

4

If the user doesn't need to know or interact with the value you are inserting, then it is quite easy to inject parameter values into the insert call that the DetailsView sends to your datasource control. Just use the DetailsView.Inserting event DetailsViewInsertEventArgs parameter as shown.

Code:

    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);
        }
    }

As you might expect, this method makes the BoundField completely unnecessary :)

pseudocoder
  • 4,314
  • 2
  • 25
  • 40
2

Use Template Field and place a label and bind it with userid. You can access the label in your codebehind irrespective of it is invisible.

<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
kristof
  • 52,923
  • 24
  • 87
  • 110
Waqar Janjua
  • 6,113
  • 2
  • 26
  • 36
  • This is an interesting solution, but there's no reason to store the userid value in the DetailsView at all, based on the question posed. See my answer for more... – pseudocoder Aug 14 '12 at 21:32
  • Thanks Janjua, your solution is good as well, but it needs more magic because of the FindControl, and i like the easy way better. Anyway thank you too! – amman Aug 14 '12 at 22:01
  • @pseudocoder yeah you are right, I saw your solution, it is much better then my solution. +1 – Waqar Janjua Aug 14 '12 at 22:03
  • @Waqar I changed the last line of code to use Text() method on the Label instead of ToString() - I belive that is what you ment. also +1 as your approach could be used in some situations – kristof Feb 13 '13 at 12:23