1

I have a formview, connected to an objectDataSource.

It's a pretty easy code in the "EditItemTemplate" of the formview, implemented in the .aspx class:

<EditItemTemplate>
    Var1: <asp:TextBox ID="txtVar1" runat="server" Text='<%# Bind("var1") %>'  />
    Var2: <asp:TextBox ID="txtVar2" runat="server" Text='<%# Bind("var2") %>'  />
    <asp:Button ID="btnSave" runat="server" Text="Save" OnClick="ButtonSave_Click" />
</EditItemTemplate>

When pressing "save" the data of "var1" and "var2" passes with the object to the next update method, that is implemented in the business logic class:

[System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Update, true)]
public void UpdateMyObject(MyObject updatedObject)
{
    if (updatedObject.EntityState == EntityState.Detached)
            mContext.MyObjects.Attach(updatedObject);
        mContext.ObjectStateManager.ChangeObjectState(updatedObject, System.Data.EntityState.Modified);
        int numberOfAffectedRows = mContext.SaveChanges();
        if (numberOfAffectedRows == 0)
            throw new DataNotUpdatedException("No object updated!");
}

The thing is that I need to pass a 3rd variable from the aspx to the business logic, without exposing it to the user through a textbox. It's a Guid and there is no need to change it. With the current code, the Guid that is assigned to the "updatedObject" is only zeros and not the original Guid of the object, that it has and would have passed if I had binded it to a textbox, using a <%# Bind("entryGuid") %>.

So, my question is how can I pass this Guid (or any other variable) to the business logic layer, without the need to expose it? Is there an elegant way to bind data to the updated object not through a textbox?

Thanks!

2 Answers 2

1

Since @tzerb's answer left out an example, I thought I'd provide one.

<EditItemTemplate> 
    Var1: <asp:TextBox ID="txtVar1" runat="server" Text='<%# Bind("var1") %>'  /> 
    Var2: <asp:TextBox ID="txtVar2" runat="server" Text='<%# Bind("var2") %>'  /> 
    Var3: <asp:HiddenField id="hdnVar3" runat="server" Value='<%# Bind("var3") %>' />
    <asp:Button ID="btnSave" runat="server" Text="Save" OnClick="ButtonSave_Click" /> 
</EditItemTemplate> 
3
  • It's working Great! Sorry I can't upvote, not enough rep yet. Cheers!
    – Urik
    May 19, 2012 at 13:18
  • @OriKlipstein - Just accept it. When you get enough points, you can come back through and upvote May 19, 2012 at 13:21
  • Done, promise to get back when I get those 2 extra rep points ;)
    – Urik
    May 19, 2012 at 13:43
1

I think you're looking for input type="hidden"

This will hide the input from the users and will be available in the post back. The users will be able to see it if they 'view code'.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.