简体   繁体   中英

How To Use Update Function In Repeater Control Dynamically?

i have 1 repeater and bind subjectid and subjectname from Assign_Subjects table...and put 1 textbox template to insert marks obtained in each subjects @btnInsert_Click event and i already wrote my insertion code as below and it works fine...and result table in which i want to insert marks along with subjects...

I want to update the marks ie inserted (in textbox - MarksObtained column) within repeater control...i used button as btnUpdate for edit textboxes....

.aspx page

<div>
 <table border="0" width="600px" cellpadding="2" cellspacing="1" style="border: 1px solid maroon;">
            <asp:Repeater ID="Repeater1" runat="server">
                <HeaderTemplate>
                    <tr bgcolor="maroon">
                        <th>
                            Subject_Id
                        </th>
                        <th>
                            Subject_Name
                        </th>
                        <th>
                            Fill_Marks
                        </th>
                    </tr>
                </HeaderTemplate>
                <ItemTemplate>
                    <tr>
                        <td width="100">
                            <asp:TextBox ID="txtSubjectId" runat="Server" Text='<%#Eval("Subject_Id")%>'></asp:TextBox>
                        </td>
                        <td>
                            <asp:TextBox ID="txtSubjectName" runat="Server" Text='<%#Eval("Subject_Name")%>'></asp:TextBox>
                        </td>
                        <td>
                            <asp:TextBox ID="txtMarks" runat="server" ></asp:TextBox>
                        </td>
                    </tr>
                </ItemTemplate>
            </asp:Repeater>
        </table>
        <asp:Button ID="btnInsert" runat="server" OnClick="btnInsert_Click" Text="Insert" />
        **<asp:Button ID="btnUpdate" runat="server" OnClick="btnUpdate_Click" Text="Insert" />**
    </div> 

code behind - c#

   protected void btnInsert_Click(object sender, EventArgs e)
  {
      foreach (RepeaterItem repeaterItem in Repeater1.Items)
      {
          string subjectId = (repeaterItem.FindControl("txtSubjectId") as TextBox).Text.Trim();
          string subjectName = (repeaterItem.FindControl("txtSubjectName") as TextBox).Text.Trim();
          string marks = (repeaterItem.FindControl("txtMarks") as TextBox).Text.Trim();

          this.SaveData(subjectId, subjectName, marks);
      }
  }

 private void SaveData(string subjectId, string subjectName, string marks)
        {
            cn = new SqlConnection(ConfigurationManager.ConnectionStrings["DbConnect"].ConnectionString);
            cn.Open();

            SqlCommand cmd = new SqlCommand("Insert into result(id,name,marks) values('"+subjectId+"','"+subjectName+"','"+marks+"')", cn);

            Repeater1.DataSource = cmd.ExecuteReader();

            //Display a popup which indicates that the record was successfully inserted

            Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "Script", "alert('Successfuly Inserted. !!');", true);

            cn.Close();
            cmd.Connection.Close();
            cmd.Connection.Dispose();
        }

Insert into result table

id     varchar(20)
name   varchar(20)
marks  varchar(20)

Now question is how can i update through repeaters ??? explained with code more appriciated... thanks

your buttons inside repeater should look like this.....

 <td>
    <asp:ImageButton ID="btnSave" runat="server"  
         ImageUrl="~/images/save.png" ToolTip="click to save record"  
         CommandName="SaveData" 
         OnClientClick="return confirm('Conform message before  saving')" />
</td>

then to capture the events generated by repeater control use Repeater's ItemCommand event

protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
{
   // note that this is the command we assign to save button. 
   // we use it here to capture which button was clicked.
    if (e.CommandName == "SaveData")
    {
        TextBox txtFirstName = e.Item.FindControl("txtFirstName") as TextBox;
        string fname= txtFirstName.Text;

       // do somethig with your date here..

    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM