How to get the value of a textbox in a database in a repeater?

I have a relay that I populate from a database:

using (SqlConnection conn = new SqlConnection(connString))
{
   SqlCommand cmd = new SqlCommand(@"SELECT CommunityName, CID, Budget FROM Donation WHERE Year = year(getdate()) ORDER BY CommunityName", conn);
   conn.Open();
   SqlDataAdapter adp = new SqlDataAdapter(cmd);
   DataSet myDataSet = new DataSet();
   adp.Fill(myDataSet);
   myRep.ItemDataBound += new RepeaterItemEventHandler(myRep_ItemDataBound);
   myRep.DataSource = myDataSet;
   myRep.DataBind();
}
void myRep_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
   var textbox = e.Item.FindControl("community");
   textbox.ClientIDMode = ClientIDMode.Static;
   textbox.ID = "community" + (e.Item.ItemIndex + 1);
 }

      

Repeater:

<asp:UpdatePanel ID="UpdatePanel" runat="server" UpdateMode="Always">
    <ContentTemplate>
       <asp:Repeater ID="myRep" runat="server">
          <ItemTemplate>
             <div class="form-group">
                <asp:Label ID='thisLbl' runat="server" Text='<%# Eval("CommunityName") %>' />
                <asp:TextBox runat="server" ID="community" Text='<%# Eval("Budget") %>' CssClass="form-control" />
             </div>
          </ItemTemplate>
       </asp:Repeater>
    </ContentTemplate>
</asp:UpdatePanel>

      

This creates 6 text fields with labels and values, now my question is how to determine which of these fields belongs to the record originally pulled from the database? I want to be able to change the value in these fields and click a button to save them back to the database, but I can't seem to wrap my head around to get them into their respective records.

Should I set the id of the textbox to something that I can parse and match against the corresponding record? The ItemDataBound?

+3


source to share


1 answer


You have to put a hidden field inside the relay item template that takes a value from budget and another hidden field to store the CID value to be read in the feedback request. Of course, you also need a button and a click event handler.

    <asp:Repeater ID="myRep" runat="server">
      <ItemTemplate>
         <div class="form-group">
            <asp:Label ID='thisLbl' runat="server" Text='<%# Eval("CommunityName") %>' />
            <asp:TextBox runat="server" ID="txtBudget" Text='<%# Eval("Budget") %>' CssClass="form-control" />
            <asp:HiddenField runat="server" ID="hdOriginalBudget" Value='<%# Eval("Budget") %>' />
            <asp:HiddenField runat="server" ID="hdCID" Value='<%# Eval("CID") %>' />
         </div>
      </ItemTemplate>
   </asp:Repeater>
    <br />
    <asp:Button ID="btn" runat="server" OnClick="btn_Click" />

      

In your code behind you need to loop inside the repeater to check if the textbox has changed by comparing its value to the hidden field. After saving the budget value in the database, you need to translate the value of the hidden field to the new value entered by the user, otherwise you will always save this value after every post:



    protected void btn_Click(object sender, EventArgs e)
    {
        foreach (RepeaterItem item  in myRep.Items)
        {
            var txtBudget = item.FindControl("txtBudget") as TextBox;
            var hdOriginalBudget = item.FindControl("hdOriginalBudget") as HiddenField;
            var hdCID = item.FindControl("hdCID") as HiddenField;
            if (txtBudget.Text != hdOriginalBudget.Value)
            { 
                //If you enter here means the user changed the value of the text box

                using (SqlConnection conn = new SqlConnection(connString))
                {
                    SqlCommand cmd = new SqlCommand(@"UPDATE Donation SET Budget = @Budget WHERE CID = @CID", conn);
                    cmd.Parameters.Add(new SqlParameter("@Budget", int.Parse(txtBudget.Text)));
                    cmd.Parameters.Add(new SqlParameter("@CID", int.Parse(hdCID.Value)));
                    conn.Open();
                    cmd.ExecuteNonQuery();
                }

                //After you write in the database realign the values
                hdOriginalBudget.Value = txtBudget.Text; 
            }

        }
    }

      

Take care that my code is missing the most basic validation, so if the user writes an invalid value to the textbox (like "yyy") it breaks. So please don't put it in production as it is!

+1


source







All Articles