91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Repeater控件實現編輯、更新、刪除操作的方法

發布時間:2020-10-14 18:23:25 來源:億速云 閱讀:288 作者:小新 欄目:編程語言

小編給大家分享一下Repeater控件實現編輯、更新、刪除操作的方法,希望大家閱讀完這篇文章后大所收獲,下面讓我們一起去探討吧!

后臺.cs代碼

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
         BindGrid();
    }

}

private void BindGrid()
{          
     string strSQL = "SELECT * FROM [User]";
     OleDbConnection objConnection = new OleDbConnection(GetStrConnection());
     objConnection.Open();
     OleDbCommand objCommand = new OleDbCommand(strSQL, objConnection);
     OleDbDataReader reader = objCommand.ExecuteReader(CommandBehavior.CloseConnection);
     rptUser.DataSource = reader;
     rptUser.DataBind();
}

protected void rptUser_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
         System.Data.Common.DbDataRecord record = (System.Data.Common.DbDataRecord)e.Item.DataItem;
         int userId = int.Parse(record["UserId"].ToString());
         if (userId != id)
         {
             ((Panel)e.Item.FindControl("plItem")).Visible = true;
             ((Panel)e.Item.FindControl("plEdit")).Visible = false;
         }
         else
         {
             ((Panel)e.Item.FindControl("plItem")).Visible = false;
             ((Panel)e.Item.FindControl("plEdit")).Visible = true;
         }       
    }
}

protected void rptUser_ItemCommand(object source, RepeaterCommandEventArgs e)
{
     if (e.CommandName == "Edit")
     {
         id = int.Parse(e.CommandArgument.ToString());
     }
     else if (e.CommandName == "Cancel")
     {
         id = -1;
     }
     else if (e.CommandName == "Update")
     {
         string name = ((TextBox)this.rptUser.Items[e.Item.ItemIndex].FindControl("txtName")).Text.Trim();
         string email = ((TextBox)this.rptUser.Items[e.Item.ItemIndex].FindControl("txtEmail")).Text.Trim();
         string qq = ((TextBox)this.rptUser.Items[e.Item.ItemIndex].FindControl("txtQQ")).Text.Trim();
         string strSQL = "UPDATE [User] SET Name=@Name,Email=@Email,QQ=@QQ WHERE UserId=@UserId";
         OleDbConnection objConnection = new OleDbConnection(GetStrConnection());
         OleDbCommand objCommand = new OleDbCommand(strSQL, objConnection);
         objCommand.Parameters.Add("@Name", OleDbType.VarWChar);
         objCommand.Parameters["@Name"].Value = name;
         objCommand.Parameters.Add("@Email", OleDbType.VarWChar);
         objCommand.Parameters["@Email"].Value = email;
         objCommand.Parameters.Add("@QQ", OleDbType.VarWChar);
         objCommand.Parameters["@QQ"].Value = qq;
         objCommand.Parameters.Add("@UserId", OleDbType.Integer);
         objCommand.Parameters["@UserId"].Value = int.Parse(e.CommandArgument.ToString());
         objConnection.Open();
         objCommand.ExecuteNonQuery();
         objConnection.Close();
     }
     else if (e.CommandName == "Delete")
     {
         string strSQL = "DELETE * FROM [User] WHERE UserId=@UserId";
         OleDbConnection objConnection = new OleDbConnection(GetStrConnection());
         OleDbCommand objCommand = new OleDbCommand(strSQL, objConnection);
         objCommand.Parameters.Add("@UserId", OleDbType.Integer);
         objCommand.Parameters["@UserId"].Value = int.Parse(e.CommandArgument.ToString());
         objConnection.Open();
         objCommand.ExecuteNonQuery();
         objConnection.Close();
     }

     BindGrid();
}

private string GetStrConnection()
{
    return "Provider=Microsoft.Jet.OleDb.4.0;data source=" + Server.MapPath("~/Database/test.mdb");
}

前臺.aspx代碼

<form id="form1" runat="server">
    <asp:Repeater ID="rptUser" runat="server" onitemcommand="rptUser_ItemCommand" 
        onitemdatabound="rptUser_ItemDataBound">
        <HeaderTemplate>
            <table width="960" align="center" cellpadding="3" cellspacing="1" style="background-color: #ccc;">
                <thead style="background-color: #eee;">
                    <tr>
                        <th width="10%">
                            用戶ID
                        </th>
                        <th>
                            用戶名
                        </th>
                        <th width="22%">
                            郵件
                        </th>
                        <th width="20%">
                            QQ
                        </th>
                        <th width="15%">
                            注冊時間
                        </th>
                        <th width="12%">
                            操作
                        </th>
                    </tr>
                </thead>
                <tbody style="background-color: #fff;">
        </HeaderTemplate>
        <ItemTemplate>
            <asp:Panel ID="plItem" runat="server">
                <tr style="text-align: center;">
                    <td>
                        <%# DataBinder.Eval(Container.DataItem, "UserId")%>
                    </td>
                    <td>
                        <%# DataBinder.Eval(Container.DataItem, "Name")%>
                    </td>
                    <td>
                        <%# DataBinder.Eval(Container.DataItem, "Email")%>
                    </td>
                    <td>
                        <%# DataBinder.Eval(Container.DataItem, "QQ")%>
                    </td>
                    <td>
                        <%# DataBinder.Eval(Container.DataItem, "AddTime","{0:yyyy-MM-dd}")%>
                    </td>
                    <td> <asp:LinkButton runat="server" ID="lbtEdit" CommandArgument='<%# DataBinder.Eval(Container.DataItem, "UserId")%>'
                     CommandName="Edit" Text="編輯"></asp:LinkButton>&nbsp;&nbsp;
                    <asp:LinkButton runat="server" ID="lbtDelete" CommandArgument='<%# DataBinder.Eval(Container.DataItem, "UserId")%>'
                     CommandName="Delete" Text="刪除" OnClientClick="return confirm('確定要刪除?')"></asp:LinkButton>
                    </td>
                </tr>
            </asp:Panel>
            <asp:Panel ID="plEdit" runat="server">
                <tr style="text-align: center;">
                    <td>
                        <%# DataBinder.Eval(Container.DataItem, "UserId")%>
                    </td>
                    <td>
                        <asp:TextBox ID="txtName" Text='<%# DataBinder.Eval(Container.DataItem,"Name") %>'
                            runat="server"></asp:TextBox>
                    </td>
                    <td>
                        <asp:TextBox ID="txtEmail" Text='<%# DataBinder.Eval(Container.DataItem,"Email") %>'
                            runat="server"></asp:TextBox>
                    </td>
                    <td>
                        <asp:TextBox ID="txtQQ" Text='<%# DataBinder.Eval(Container.DataItem,"QQ") %>' runat="server"></asp:TextBox>
                    </td>
                    <td>
                        <%# DataBinder.Eval(Container.DataItem, "AddTime","{0:yyyy-MM-dd}")%>
                    </td>
                    <td>
                      <asp:LinkButton runat="server" ID="lbtUpdate" CommandArgument='<%# DataBinder.Eval(Container.DataItem, "UserId")%>'
                     CommandName="Update" Text="更新"></asp:LinkButton>&nbsp;&nbsp;
                    <asp:LinkButton runat="server" ID="lbtCancel" CommandArgument='<%# DataBinder.Eval(Container.DataItem, "UserId")%>'
                     CommandName="Cancel" Text="取消"></asp:LinkButton>
                    </td>
                </tr>
            </asp:Panel>
        </ItemTemplate>
        <FooterTemplate>
            </tbody></table>
        </FooterTemplate>
    </asp:Repeater>
</form>

看完了這篇文章,相信你對Repeater控件實現編輯、更新、刪除操作的方法有了一定的了解,想了解更多相關知識,歡迎關注億速云行業資訊頻道,感謝各位的閱讀!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

龙州县| 吕梁市| 伊春市| 宜丰县| 沈丘县| 闵行区| 彭州市| 仪征市| 德化县| 河源市| 景泰县| 芜湖市| 潜江市| 博罗县| 泸溪县| 新建县| 逊克县| 湾仔区| 同心县| 息烽县| 四川省| 佛山市| 顺昌县| 朝阳区| 三台县| 宁德市| 子洲县| 嘉兴市| 太白县| 阜新市| 镇巴县| 友谊县| 郁南县| 永寿县| 吴桥县| 南昌市| 汝阳县| 汉中市| 新竹市| 连城县| 浦江县|