Datalist或Repeater里点击某列内容将放到文本框中以便编辑,文本框失去焦点后信息即可修改成功...

       如同GridView的编辑功能,在Datalist或Repeater中也可以实现。本文实现的功能就是当点击Datalist或Repeater中的某列,自动出现一文本框,然后在文本框中输入要改成的内容,当光标离开该框后即提交到数据库啦!功能虽实现了,不过朋友给我一建议“若无刷新就更好了”,呵呵......

       下面是我的代码,还希望能帮到有需要的朋友。注意:我用到了文本框的TextChanged事件,需要将该文本框的AutoPostBack属性设置为true。

ExpandedBlockStart.gif 前台代码
 1  <% @ Page Language = " C# "  AutoEventWireup = " true "  CodeFile = " UpdateInDatalist.aspx.cs "  Inherits = " UpdateInDatalist "   %>
 2 
 3  <! DOCTYPE html PUBLIC  " -//W3C//DTD XHTML 1.0 Transitional//EN "   " http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd " >
 4 
 5  < html xmlns = " http://www.w3.org/1999/xhtml " >
 6  < head runat = " server " >
 7       < title > 无标题页 </ title >
 8       < script language = " javascript "  type = " text/javascript " >
 9         function pageOnLoad()
10         {
11              var arrAllTextboxes = document.getElementsByTagName( " input " );
12               for (i = 0 ;i < arrAllTextboxes.length;i ++ )
13              {
14                   if (arrAllTextboxes[i].id.indexOf( " TxtProName " ) !=- 1 || arrAllTextboxes[i].id == " TextBox1 " || arrAllTextboxes[i].id == " TxtProductID " )
15                  {
16                      document.getElementById(arrAllTextboxes[i].id).style.display = " none " ;
17                  }
18              }
19         }
20          function labelOnClick(Label1,TxtProName,productID,TxtProductID)
21          {
22                document.getElementById(TxtProductID).value = productID; // the nonce productID which needs to update the productName
23                document.getElementById(Label1).style.display = " none " ; // the label disappear
24                document.getElementById(TxtProName).style.display = " inline " ; // the txt appear
25                document.getElementById(TxtProName).focus();
26          }
27          function TxtboxOnblur(TextBox1,controlTxtID,LblProName)
28          {
29              document.getElementById(LblProName).style.display = " inline " ; // the label appear
30              document.getElementById(controlTxtID).style.display = " none " ; // the txt disappear
31                document.getElementById(TextBox1).value  =  document.getElementById(controlTxtID).value;
32          }
33       </ script >
34  </ head >
35  < body onload = " pageOnLoad() " >
36       < form id = " form1 "  runat = " server " >
37       < asp:TextBox ID = " TextBox1 "  runat = " server " ></ asp:TextBox >
38       < asp:TextBox ID = " TxtProductID "  runat = " server " ></ asp:TextBox >
39      
40       < asp:DataList ID = " DataList1 "  runat = " server "  HorizontalAlign = " Justify "  
41          RepeatColumns = " 3 "  RepeatDirection = " Horizontal "  
42          onitemdatabound = " DataList1_ItemDataBound " >
43       < ItemTemplate >
44      
45           < table >
46               < tr >
47                   < td >
48                       < asp:Image ID = " Image1 "  Width = " 150 "  Height = " 120 "  ImageUrl = ' <%# "UploadImages/"+Eval("productPic") %> '  runat = " server "   />
49                   </ td >
50               </ tr >
51               < tr >
52                   < td >
53                       < asp:Label ID = " Label1 "  runat = " server "  Text = ' <%#Eval("productName")%> ' ></ asp:Label >
54                       < asp:Label ID = " LblProductID "  Visible = " false "  runat = " server "  Text = ' <%#Eval("productID")%> ' ></ asp:Label >
55                       < asp:TextBox ID = " TxtProName "  AutoPostBack = " True "  runat = " server "  Text = ' <%#Eval("productName") %> '  ontextchanged = " TxtProName_TextChanged " ></ asp:TextBox >
56                   </ td >  
57               </ tr >
58           </ table >
59           </ ItemTemplate >
60       </ asp:DataList >
61       </ form >
62  </ body >
63  </ html >
64 
ExpandedBlockStart.gif 后台代码
using  System;
using  System.Collections;
using  System.Configuration;
using  System.Data;
using  System.Linq;
using  System.Web;
using  System.Web.Security;
using  System.Web.UI;
using  System.Web.UI.HtmlControls;
using  System.Web.UI.WebControls;
using  System.Web.UI.WebControls.WebParts;
using  System.Xml.Linq;

public   partial   class  UpdateInDatalist : System.Web.UI.Page
{
    
string  sql  =   null ;
    
protected   void  Page_Load( object  sender, EventArgs e)
    {
        
if  ( ! IsPostBack)
        {
            BindDataList1();
        }
    }
    
void  BindDataList1()
    {
        sql 
=   string .Format( " select * from productTable order by productID desc " );
        DataTable dt 
=  DataBase.ExecuteDataSet(sql).Tables[ 0 ];
        DataList1.DataSource 
=  dt.DefaultView;
        DataList1.DataBind();
    }
    
protected   void  DataList1_ItemDataBound( object  sender, DataListItemEventArgs e)
    {
        
if  (e.Item.ItemType  ==  ListItemType.Item  ||  e.Item.ItemType  ==  ListItemType.AlternatingItem)
        {
            DataRowView drv 
=  (DataRowView)e.Item.DataItem;
            
string  productID  =  ((Label)e.Item.FindControl( " LblProductID " )).Text.ToString();
            ((Label)e.Item.FindControl(
" Label1 " )).Attributes.Add( " onClick " " labelOnClick(' "   +  ((Label)e.Item.FindControl( " Label1 " )).ClientID  +   " ',' "   +  ((TextBox)e.Item.FindControl( " TxtProName " )).ClientID  +   " ',' "   +  productID  +   " ',' "   +  TxtProductID.ClientID  +   " ') " );

            ((TextBox)e.Item.FindControl(
" TxtProName " )).Attributes.Add( " onBlur " " TxtboxOnblur(' "   +  TextBox1.ClientID  +   " ',' "   +  ((TextBox)e.Item.FindControl( " TxtProName " )).ClientID  +   " ',' "   +  ((Label)e.Item.FindControl( " Label1 " )).ClientID  +   " ') " );
        }
    }
    
protected   void  TxtProName_TextChanged( object  sender, EventArgs e)
    {
        
string  productName  =  TextBox1.Text.ToString();
        
string  productID  =  TxtProductID.Text.Trim().ToString();
        
// Response.Write(productID);
        sql  =   string .Format( " update productTable set productName='{0}' where productID={1} " , productName, productID);
        
int  i  =  DataBase.ExecuteNonQuery(sql);
        
if  (i  >   0 )
        {
            BindDataList1();
        }
    }
}


转载于:https://www.cnblogs.com/lotuslove/archive/2010/02/01/1660913.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的示例,演示如何使用Qt多线程读取文件,然后将文件内容分页并显示在TableView。 首先,我们需要创建一个线程类来处理文件读取和分页操作。在这个示例,我们将使用QThread类作为基类,然后覆盖run()函数来执行线程操作。我们还将添加一个信号(sendData)来将读取的数据发送回主线程。 FileReaderThread.h文件: ```cpp #ifndef FILEREADERTHREAD_H #define FILEREADERTHREAD_H #include <QThread> #include <QStringList> class FileReaderThread : public QThread { Q_OBJECT public: explicit FileReaderThread(QObject *parent = nullptr); void setFileName(const QString &fileName); void setPageSize(int pageSize); signals: void sendData(const QStringList &data); protected: void run() override; private: QString m_fileName; int m_pageSize; }; #endif // FILEREADERTHREAD_H ``` FileReaderThread.cpp文件: ```cpp #include "FileReaderThread.h" #include <QFile> #include <QTextStream> FileReaderThread::FileReaderThread(QObject *parent) : QThread(parent) , m_pageSize(10) { } void FileReaderThread::setFileName(const QString &fileName) { m_fileName = fileName; } void FileReaderThread::setPageSize(int pageSize) { m_pageSize = pageSize; } void FileReaderThread::run() { QFile file(m_fileName); if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) return; QTextStream in(&file); QStringList dataList; int lineCount = 0; while (!in.atEnd()) { QString line = in.readLine(); if (line.isEmpty()) continue; dataList.append(line); ++lineCount; if (lineCount >= m_pageSize) { emit sendData(dataList); dataList.clear(); lineCount = 0; } } if (!dataList.isEmpty()) emit sendData(dataList); } ``` 接下来,我们需要在主线程创建一个TableView,并将其与一个QStandardItemModel关联。然后,我们将创建一个QLineEdit和一个QPushButton,允许用户输入文件名和页大小。当用户单击按钮时,我们将启动一个新线程来处理文件读取和分页操作。 MainWindow.h文件: ```cpp #ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <QStringList> namespace Ui { class MainWindow; } class QStandardItemModel; class FileReaderThread; class MainWindow : public QMainWindow { Q_OBJECT public: explicit MainWindow(QWidget *parent = nullptr); ~MainWindow(); private slots: void on_pushButton_clicked(); void onThreadData(const QStringList &data); private: Ui::MainWindow *ui; QStandardItemModel *m_model; FileReaderThread *m_thread; }; #endif // MAINWINDOW_H ``` MainWindow.cpp文件: ```cpp #include "MainWindow.h" #include "ui_MainWindow.h" #include "FileReaderThread.h" #include <QStandardItemModel> #include <QMessageBox> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow), m_model(new QStandardItemModel(this)), m_thread(new FileReaderThread(this)) { ui->setupUi(this); m_model->setHorizontalHeaderLabels(QStringList() << "Line"); ui->tableView->setModel(m_model); ui->tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch); connect(m_thread, &FileReaderThread::sendData, this, &MainWindow::onThreadData); } MainWindow::~MainWindow() { delete ui; } void MainWindow::on_pushButton_clicked() { QString fileName = ui->lineEdit->text().trimmed(); if (fileName.isEmpty()) { QMessageBox::warning(this, tr("Warning"), tr("Please input file name!")); return; } bool ok; int pageSize = ui->lineEdit_2->text().toInt(&ok); if (!ok || pageSize < 1) { QMessageBox::warning(this, tr("Warning"), tr("Please input correct page size!")); return; } m_thread->setFileName(fileName); m_thread->setPageSize(pageSize); m_thread->start(); } void MainWindow::onThreadData(const QStringList &data) { for (const auto &line : data) { QList<QStandardItem *> items; items.append(new QStandardItem(line)); m_model->appendRow(items); } } ``` 现在我们已经完成了示例应用程序的所有代码。当用户单击“PushButton”按钮时,我们将启动一个新线程来处理文件读取和分页操作。每当线程读取一页数据时,它将使用sendData信号将数据发送回主线程。主线程将收到数据并将其添加到TableView。 注意!在实际应用程序,您需要添加一些错误处理代码来处理可能出现的错误,例如文件打开失败等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值