【问题标题】:Enter data in html textbox and show it in database在html文本框中输入数据并在数据库中显示
【发布时间】:2015-05-07 11:50:56
【问题描述】:

我创建了两个表:Artist 和 Song。 Artist 表有字段 Artistid 和 ArtistName,Song 表有 SongID、Lyrics、Description 和 Artistid 作为辅助键。我有一个 Web 表单来添加包含文本框和提交按钮的新艺术家。

代码如下:

<head id="Head1" runat="server">
    <title></title>


</head>
<body>
    <form id="form1" method ="post"> 
        <div style="width: 960px; color: black; border: 2px solid black;                 text-align:"center">
            <table>
                <tr> 
                    <td> <label>Artist: </label>
                        <input type="text" name ="textArtist" id ="txtartist"  value = "" /> 
                    </td>
                </tr>
                <tr>
                    <td> 
                        <p align="center" > 
                            <input type = "button" id= "btnArtist" value ="submit" /> 
                        </p> 
                    </td> 
                </tr>

            </table>
        </div>
    </form>
</body>

我的要求是在文本框中输入一些名字并点击提交,相同的名字应该显示在数据库的艺术家表中。 我还使用 Mygenerations 与艺术家和歌曲表创建了一个名称空间。我已经编写了以下代码,但它没有显示数据库中的数据。它无法正常工作。请帮忙。 文件后面的代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using NCI.EasyObjects ;
using TestSong ;

public partial class _Default : System.Web.UI.Page
{
    string strcon = "Company";

    protected void Page_Load(object sender, EventArgs e)
    {

    }

    void btnartist_onclick(object sender, EventArgs e)
    {
        string s_artistname = textArtist.Value; 
        Artist oArtist = new Artist(strcon);
        oArtist.Where.ArtistName.Value = s_artistname;
        if (!oArtist.Query.Load())
        {
            oArtist.AddNew();
            oArtist.ArtistName = textArtist.Value;
            oArtist.Save();
        }
    }
}

我也收到了包含textArtist.Value 的行的错误。错误是 textArtist 在当前上下文中不存在。请告诉我哪里出错了。

【问题讨论】:

    标签: c# asp.net


    【解决方案1】:

    可能有更多原因,但现在您使用 txtArtist 作为 Id。所以用这个

      string s_artistname = txtArtist.Value;
    

      oArtist.ArtistName = txtArtist.Value;
    

    【讨论】:

      【解决方案2】:

      如果您想在代码隐藏中访问它,请使用 runat=server

      <input type="text" name ="textArtist" id ="txtartist"  runat="server" /> 
      

      【讨论】:

      • 我已经尝试了上面的..错误消失了但是我没有得到数据库中显示的数据。
      • 嗨,我已经完成了上述工作,现在我创建了一个 Web 表单,其中包含一个用于歌曲描述的文本框、一个用于艺术家姓名的下拉列表、一个用于歌词的文本区域和一个提交按钮。我需要的是输入歌曲名称,为歌曲选择特定艺术家并输入歌词,所有这些更改都应反映在数据库中。
      • protected void btnSubmitSong_Click(object sender, EventArgs e) { Songs oSong = new Songs(strcon); oSong.Where.Description.Value = txtDescription.Value; if (!oSong.Query.Load()) { oSong.AddNew(); oSong.s_Description = txtDescription.Value; oSong.Save();} AddLyrics(); } private void BindArtistData() { 艺术家 oArtist = new Artist(strcon); if(oArtist.Query.Load()) { ddlArtist.DataTextField = "ArtistName"; ddlArtist.DataValueField = "ArtistID"; ddlArtist.DataSource = oArtist.DefaultView.Table; ddlArtist.DataBind(); ddlArtist.SelectedIndex = 0; } }
      • 我可以同时获取数据库中的歌曲名称和歌词,但不能同时获取。假设如果我输入歌曲名称及其歌词,它将只显示歌词而不是数据库中的歌曲名称。有人请帮忙。我需要在今天之前解决它。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-03-23
      • 1970-01-01
      • 1970-01-01
      • 2019-05-14
      • 2015-04-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多