【问题标题】:How To Use Update Function In Repeater Control Dynamically?如何在中继控制中动态使用更新功能?
【发布时间】:2012-05-14 03:11:39
【问题描述】:

我有 1 个中继器并从 Assign_Subjects 表中绑定 subjectid 和 subjectname...并放入 1 个文本框 模板以插入在每个中获得的标记主题 @btnInsert_Click 事件,我已经编写了如下插入代码,它工作正常......和 ​​result 表,我想在其中插入标记以及主题......

我想更新标记,即在转发器控件中插入(在文本框 - MarksObtained 列中)...我使用按钮作为 btnUpdate 来编辑文本框...。

.aspx 页面

<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> 

代码隐藏 - 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();
        }

插入结果表格

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

现在的问题是我如何通过中继器进行更新???用更多的代码解释...谢谢

【问题讨论】:

  • 首先,您应该使用参数来构建您的 SqlCommand,否则您将面临 SQL 注入攻击。第二 - 你得到的确切错误是什么?
  • 我对您要完成的工作感到困惑。你有一个中继器,我假设你填充了数据。然后,当您单击“插入”时,它会遍历转发器中的每个项目并将 INSERT 插入到您的数据库中?我认为您可能是想在转发器下方放置文本框以向列表中添加新项目?
  • kevin main - 我更新了我的问题...你可以看到它...
  • 插入后,您将重新绑定转发器,这意味着 Repeater1.Items 集合中的数据发生更改,因此出现错误。删除 SaveData() 方法中的绑定。
  • 只有在没有回发的情况下才绑定 PageLoad 中的中继器才能解决问题

标签: c# asp.net repeater edit insert-update


【解决方案1】:

中继器内的按钮应如下所示.....

 <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>

然后使用Repeater的ItemCommand事件来捕捉repeater控件产生的事件

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..

    }
}

【讨论】:

    猜你喜欢
    • 2016-09-03
    • 2019-11-09
    • 2019-11-29
    • 1970-01-01
    • 1970-01-01
    • 2020-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多