locked
How do you retrieve a control value in a FormView onItemUpdating? RRS feed

  • Question

  • User-1072848465 posted

    I want to update the users email address in an asp.net identity table when saving. The issue that I see is in the method UserProfileFormView_ItemUpdating. I am not able to retrieve the new entered value in the TextBox with ID="email_txt". How do you get the value?

    Thanks in advance

    Brad

    <asp:FormView ID="FormView1" runat="server" DataKeyNames="Id" DataSourceID="ProfileEDS" DefaultMode="Edit" OnItemUpdating="UserProfileFormView_ItemUpdating">
        <EditItemTemplate>
                    <asp:TextBox ID="firstName_txt" runat="server" Text='<%# Bind("firstName") %>'></asp:TextBox>
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="firstName_txt" ErrorMessage=" - first name is required" CssClass="text-danger small" Display="Dynamic"></asp:RequiredFieldValidator>
    
                    <asp:TextBox ID="lastName_txt" runat="server" Text='<%# Bind("lastName") %>'/>
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="lastName_txt" ErrorMessage=" - title is required" CssClass="text-danger small" Display="Dynamic"></asp:RequiredFieldValidator>
    
                    <asp:TextBox ID="email_txt" runat="server"  />
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" ControlToValidate="email_txt" ErrorMessage=" - email is required" CssClass="text-danger small" Display="Dynamic"></asp:RequiredFieldValidator>
    
                <asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True" CommandName="Update" Text="Update" CssClass="btn btn-primary" />
                <asp:LinkButton ID="UpdateCancelButton" runat="server" CausesValidation="False" Text="Cancel" CssClass="btn btn-default" OnClick="UpdateCancelButton_Click" />
        </EditItemTemplate>
    </asp:FormView>
    <asp:EntityDataSource ID="EntityDataSource1" runat="server" ConnectionString="name=modelContainer" DefaultContainerName="modelContainer" EnableFlattening="False" EntitySetName="UserProfiles" EnableUpdate="true" Where="it.[Id] = @nId">
        <WhereParameters>
            <asp:QueryStringParameter Name="nId" QueryStringField="profileid" DbType="Int32" />
        </WhereParameters>
    </asp:EntityDataSource>

    Code Behind C#

        protected void UserProfileFormView_ItemUpdating(object sender, FormViewUpdateEventArgs e)
        {
            TextBox emailTxt = (TextBox)UserProfileFormView.Row.FindControl("email_txt");
            string email = emailTxt.Text;
    
            UserManager Manager = new UserManager();
            var user = Manager.FindById(userId);
    
            user.Email = email.ToString();
    
            var result = Manager.Update(user);
    
        }

    Friday, May 22, 2015 9:56 AM

Answers

  • User1577371250 posted

    Hi,

    Keep the code in Page_Load inside the Postback block.

     protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    var uId = Request.QueryString["profileid"];
                    int profileId = Convert.ToInt32(uId);
                    getProfile(profileId);
                }
    
            }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, May 26, 2015 8:30 AM

All replies

  • User1577371250 posted

    Hi,

    try this

     protected void UserProfileFormView_ItemUpdating(object sender, FormViewUpdateEventArgs e)
            {
                TextBox emailTxt = (TextBox)UserProfileFormView.FindControl("email_txt");
    string email = emailTxt.Text; UserManager Manager = new UserManager(); var user = Manager.FindById(userId); user.Email = email.ToString(); var result = Manager.Update(user); }

    Monday, May 25, 2015 3:27 AM
  • User61956409 posted

    Hi bdassow,

    Thanks for your post.

    <asp:FormView ID="FormView1" runat="server" DataKeyNames="Id" DataSourceID="ProfileEDS" DefaultMode="Edit" OnItemUpdating="UserProfileFormView_ItemUpdating"> 

    TextBox emailTxt = (TextBox)UserProfileFormView.Row.FindControl("email_txt");

    According to your code, we could find the ID of FormView is “FormView1”, if you’d like to find control in this FormView, please try this.

    TextBox emailTxt = (TextBox)FormView1.FindControl("email_txt"); 

    Best Regards,

    Fei Han

    Monday, May 25, 2015 3:36 AM
  • User-1072848465 posted

    I tried your suggestion, but I am getting the same results. The application does not seem to capture the new entered value in the Textbox control. I only have the EditTemplate and LayoutTemplate defined in the FormView. Is it a viewstate issue?

    Monday, May 25, 2015 9:56 AM
  • User1577371250 posted

    Hi,

    It works perfectly and here is the sample.

         <asp:FormView ID="FormView1" runat="server" DefaultMode="Edit" OnItemUpdating="UserProfileFormView_ItemUpdating">
                    <EditItemTemplate>
                        <asp:TextBox ID="firstName_txt" runat="server" Text='<%# Bind("firstName") %>'></asp:TextBox>
                        <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="firstName_txt" ErrorMessage=" - first name is required" CssClass="text-danger small" Display="Dynamic"></asp:RequiredFieldValidator>
    
                        <asp:TextBox ID="lastName_txt" runat="server" Text='<%# Bind("lastName") %>' />
                        <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="lastName_txt" ErrorMessage=" - title is required" CssClass="text-danger small" Display="Dynamic"></asp:RequiredFieldValidator>
    
                        <asp:TextBox ID="email_txt" runat="server" />
                        <asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" ControlToValidate="email_txt" ErrorMessage=" - email is required" CssClass="text-danger small" Display="Dynamic"></asp:RequiredFieldValidator>
    
                        <asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True" CommandName="Update" Text="Update" CssClass="btn btn-primary" />
                        <asp:LinkButton ID="UpdateCancelButton" runat="server" CausesValidation="False" Text="Cancel" CssClass="btn btn-default" OnClick="UpdateCancelButton_Click" />
                    </EditItemTemplate>
                </asp:FormView>
     protected void Page_Load(object sender, EventArgs e)
            {
                if(!IsPostBack)
                {
                    DataTable dt = new DataTable();
                    dt.Columns.Add("firstName");
                    dt.Columns.Add("lastName");
    
                    dt.Rows.Add("1", "A");
                    dt.Rows.Add("145", "Scott");
                    dt.Rows.Add("248","Tom");
                    dt.Rows.Add("268", "Amy");
                    dt.Rows.Add("478", "John");
                    dt.Rows.Add("10", "B");
    
                    FormView1.DataSource = dt;
                    FormView1.DataBind();
                }
            }
    
          
    
            protected void UpdateCancelButton_Click(object sender, EventArgs e)
            {
    
            }
    
            protected void UserProfileFormView_ItemUpdating(object sender, FormViewUpdateEventArgs e)
            {
                TextBox emailTxt = (TextBox)FormView1.FindControl("email_txt");
    
                string email = emailTxt.Text; // Check here
            
    
            }


    Monday, May 25, 2015 10:02 AM
  • User-1072848465 posted

    Lokesh,

    Yes, your sample does work and I also got it to work when the email TextBox is not pre-populated on Page Load. I can put the users current email address in a Label control and have a blank TextBox that allows them to update their email and it works. I was hoping to eliminate the Label control. 

    Thanks

    Brad

        protected void Page_Load(object sender, EventArgs e)
        {
             UserManager Manager = new UserManager();
             var user = Manager.FindById(userId);
    
             TextBox emailTxt = (TextBox)UserProfileFormView.FindControl("email_txt");
             emailTxt.Text = user.Email.ToString();
        }

    Monday, May 25, 2015 10:27 AM
  • User1577371250 posted

    Hi,

    I think you are doing something wrong. can you post your complete code?

    Tuesday, May 26, 2015 2:53 AM
  • User-1072848465 posted

    Sure, here is the code. Thanks for taking a look at it.

    <asp:FormView ID="FormView1" runat="server" DataKeyNames="Id" DataSourceID="ProfileEDS" DefaultMode="Edit" OnItemUpdating="UserProfileFormView_ItemUpdating">
        <EditItemTemplate>
                    <asp:TextBox ID="firstName_txt" runat="server" Text='<%# Bind("firstName") %>'></asp:TextBox>
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="firstName_txt" ErrorMessage=" - first name is required" CssClass="text-danger small" Display="Dynamic"></asp:RequiredFieldValidator>
    
                    <asp:TextBox ID="lastName_txt" runat="server" Text='<%# Bind("lastName") %>'/>
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="lastName_txt" ErrorMessage=" - title is required" CssClass="text-danger small" Display="Dynamic"></asp:RequiredFieldValidator>
    
                    <asp:TextBox ID="email_txt" runat="server"  />
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" ControlToValidate="email_txt" ErrorMessage=" - email is required" CssClass="text-danger small" Display="Dynamic"></asp:RequiredFieldValidator>
    
                <asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True" CommandName="Update" Text="Update" CssClass="btn btn-primary" />
                <asp:LinkButton ID="UpdateCancelButton" runat="server" CausesValidation="False" Text="Cancel" CssClass="btn btn-default" OnClick="UpdateCancelButton_Click" />
        </EditItemTemplate>
    </asp:FormView>
    <asp:EntityDataSource ID="EntityDataSource1" runat="server" ConnectionString="name=modelContainer" DefaultContainerName="modelContainer" EnableFlattening="False" EntitySetName="UserProfiles" EnableUpdate="true" Where="it.[Id] = @nId">
        <WhereParameters>
            <asp:QueryStringParameter Name="nId" QueryStringField="profileid" DbType="Int32" />
        </WhereParameters>
    </asp:EntityDataSource>

    Code Behind

        protected void Page_Load(object sender, EventArgs e)
        {
            var uId = Request.QueryString["profileid"];
            int profileId = Convert.ToInt32(uId);
            getProfile(profileId);
    
        }
    
        protected void getProfile(int id)
        {
            var context = new ModelContainer();
            var uInfo = (from u in context.UserProfiles
                         where u.Id == id
                         select new
                         {
                             id = u.Id,
                             userId = u.userId
                         }).ToList();
    
            if (uInfo.Any())
    	    {
    		 userId = uInfo[0].userId.ToString();
    
             UserManager Manager = new UserManager();
             var user = Manager.FindById(userId);
    
             TextBox usernameTxt = (TextBox)UserProfileFormView.FindControl("username_txt");
             usernameTxt.Text = user.UserName.ToString();
    
             TextBox emailTxt = (TextBox)UserProfileFormView.FindControl("email_txt");
             emailTxt.Text = user.Email.ToString();
         }
    
        protected void UserProfileFormView_ItemUpdating(object sender, FormViewUpdateEventArgs e)
        {
            TextBox emailTxt = (TextBox)UserProfileFormView.FindControl("email_txt");
            string email = emailTxt.Text;
    
                UserManager Manager = new UserManager();
                var user = Manager.FindById(userId);
    
                user.Email = email.ToString();
    
                var result = Manager.Update(user);
    
    
        }

    Tuesday, May 26, 2015 8:28 AM
  • User1577371250 posted

    Hi,

    Keep the code in Page_Load inside the Postback block.

     protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    var uId = Request.QueryString["profileid"];
                    int profileId = Convert.ToInt32(uId);
                    getProfile(profileId);
                }
    
            }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, May 26, 2015 8:30 AM
  • User-1072848465 posted

    Thanks Lokesh. Sometimes it is the simpliest of things that are overlooked.

    Tuesday, May 26, 2015 8:50 AM