网页的<title> 部分与后台数据库绑定

前台页面:

<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

<title><%=newtitle%></title>


后台页面:

 

   
//定义字段
 public string newtitle;
      protected void Page_Load(object sender, EventArgs e)
    {
      

        SqlConnection con = new SqlConnection();
        string sql2 = "select TOP 20* from  table order by newdate DESC";

        SqlDataAdapter da2 = new SqlDataAdapter(sql2, con);

        DataSet ds2 = new DataSet();

        da2.Fill(ds2, "webinfo2");

        DataList2.DataSource = ds2;
        DataList2.DataKeyField = "id";
        DataBind();
        newtitle = "动态title";

    }


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ViewModel代码如下: ```csharp public class MainViewModel : INotifyPropertyChanged { private ObservableCollection<string> _source; private string _selectedSource; public ObservableCollection<string> Source { get { return _source; } set { if (_source != value) { _source = value; OnPropertyChanged("Source"); } } } public string SelectedSource { get { return _selectedSource; } set { if (_selectedSource != value) { if (!string.IsNullOrEmpty(_selectedSource)) { MessageBox.Show($"您选择了{_selectedSource},下一步选择{_selectedSource}"); } _selectedSource = value; OnPropertyChanged("SelectedSource"); } } } public event PropertyChangedEventHandler PropertyChanged; protected void OnPropertyChanged(string propertyName) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } } ``` XAML代码如下: ```xml <Window x:Class="ComboBoxDemo.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525"> <Grid> <ComboBox x:Name="comboBox" IsEditable="True" Text="{Binding SelectedSource, Mode=TwoWay}"> <ComboBox.ItemsSource> <Binding Path="Source"/> </ComboBox.ItemsSource> <ComboBox.Style> <Style TargetType="ComboBox"> <Setter Property="IsDropDownOpen" Value="True" /> <Style.Triggers> <Trigger Property="Text" Value="{x:Null}"> <Setter Property="IsDropDownOpen" Value="False" /> </Trigger> </Style.Triggers> </Style> </ComboBox.Style> <ComboBox.InputBindings> <KeyBinding Key="Enter" Command="{Binding AddSourceCommand}"/> </ComboBox.InputBindings> </ComboBox> </Grid> </Window> ``` MainWindow.xaml.cs代码如下: ```csharp public partial class MainWindow : Window { private MainViewModel _viewModel; public MainWindow() { InitializeComponent(); _viewModel = new MainViewModel(); DataContext = _viewModel; LoadData(); } private void LoadData() { string connectionString = "Data Source=MSJ.db;Version=3;"; using (SQLiteConnection connection = new SQLiteConnection(connectionString)) { connection.Open(); string sql = "SELECT * FROM Source;"; SQLiteCommand command = new SQLiteCommand(sql, connection); SQLiteDataReader reader = command.ExecuteReader(); ObservableCollection<string> source = new ObservableCollection<string>(); while (reader.Read()) { source.Add(reader[0].ToString()); } _viewModel.Source = source; reader.Close(); connection.Close(); } } } ``` 在MainWindow.cs中用SQLiteConnection从数据库中读取数据,并将数据绑定到ComboBox上,同时添加了一个按Enter键的InputBinding,用于触发添加自定义输入的Command。AddSourceCommand在ViewModel中实现,代码如下: ```csharp public class MainViewModel : INotifyPropertyChanged { // ... public ICommand AddSourceCommand { get; set; } public MainViewModel() { AddSourceCommand = new RelayCommand(AddSource); } private void AddSource() { if (!string.IsNullOrEmpty(SelectedSource) && !Source.Contains(SelectedSource)) { Source.Add(SelectedSource); SaveData(); } } private void SaveData() { string connectionString = "Data Source=MSJ.db;Version=3;"; using (SQLiteConnection connection = new SQLiteConnection(connectionString)) { connection.Open(); string sql = "DELETE FROM Source;"; SQLiteCommand command = new SQLiteCommand(sql, connection); command.ExecuteNonQuery(); foreach (string source in Source) { sql = $"INSERT INTO Source(Source) VALUES('{source}');"; command = new SQLiteCommand(sql, connection); command.ExecuteNonQuery(); } connection.Close(); } } } ``` AddSourceCommand的实现简单地判断用户输入是否为空,是否已经存在于ComboBox中,如果不是则添加到Source中,并保存到数据库中。SaveData方法用SQLiteCommand执行SQL语句,删除原有数据并插入新数据。 其中,RelayCommand是一个常用的ICommand实现,请自行搜索相关资料获取代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值