Click here to Skip to main content
15,890,282 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hello , my problem is simple .

it's Questions and answers page about 17 question with it's answer ,
with two text boxes one for question and on for answer.
i bind the questions at the PageLoad from List<> method .
i have a repeater control with one itemtemplate containing the two textboxes.

i loop through the repeater and insert the data in my database , but the problem is the first textbox is the only one inserted !
when i press save button it seems that it takes the binded data for question textbox because when i write into the text without binding it , it behave like it's an empty just like what happening with the Answer text box .




this my code :

ASP.NET
 <asp:Repeater ID="questionRepeater" ViewStateMode="Enabled" runat="server">
    <ItemTemplate>
        <tr class="">
            <td>
                <div class="control-group">
                    <label class="control-label">Queston  : </label>

                    <div class="controls">
                        <asp:TextBox runat="server" ID="txtQ" Text='<%#Eval("Question") %>' ReadOnly="true" CssClass="span8">
                        </asp:TextBox>
                    </div>
                </div>
            </td>
        </tr>
        <tr class="info">
            <td>
                <div class="control-group">
                    <label class="control-label">Answer : </label>
                    <div class="controls">
                        <asp:TextBox runat="server" ID="txtAns"
                            Height="150" TextMode="MultiLine" CssClass="span8"></asp:TextBox>

                    </div>
                </div>
            </td>
        </tr>
    </ItemTemplate>
</asp:Repeater>

C#
private void BindRepeater()
   {
       List<SessionQuestion> questions = new List<SessionQuestion>();
       questions.Add(new SessionQuestion { Question = " Q1" });
       questions.Add(new SessionQuestion { Question = " Q2" });
       .
       .
       .
       questionRepeater.DataSource = questions;
       questionRepeater.DataBind();
   }

   protected void btnSave_Click(object sender, EventArgs e)
   {
       Sessions session = new Sessions();
       SessionQuestion sessionQuestions = new SessionQuestion();

       session.ClientId = id;
       session.DateTime = DateTime.Now;
       session.Report = txtReport.Text;
       session.Notes = string.Empty;
       session.IsActive = IsActive.Active;

       int sessionId = SessionBLL.Insert(session);

       foreach (Control item in questionRepeater.Items)
       {
           sessionQuestions.SessionId = sessionId;


           TextBox txtQ = (TextBox)item.FindControl("txtQ");
           sessionQuestions.Answer = "";
           sessionQuestions.Question = txtQ.Text;

           var txtAns = (TextBox)item.FindControl("txtAns") as TextBox;
           if (txtAns != null)
           {
               sessionQuestions.Answer = "";
               sessionQuestions.Answer = txtAns.Text;
           }


           Thread.Sleep(150);
           if (txtAns != null && txtQ.Text != null)
           {
               SessionQuestionBLL.Insert(sessionQuestions);
           }
       }

       string message = "";

       Response.Redirect("/Sessions/Sessions.aspx?message=" + message);
   }
Posted
Updated 25-Aug-13 3:54am
v3
Comments
Mahesh Bailwal 25-Aug-13 9:45am    
did you debug the code whats going wrong, where do you think problem is?
Ahmad Abd-Elghany 25-Aug-13 9:49am    
yah i debug it ,
it's Questions and answers page about 17 question with it's answer , i bind the question at the PageLoad from List<> method .
so when i press save button it seems that it takes the binded data for question textbox
because when i write into the text without binding it , it behave like it's an empty just like what happening with the other text box
i will update my question .

1 solution

and this the end of the developer who forget to put the
C#
!IsPostBack

in the pageload :D
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900