【问题标题】:ASP.NET Dynamically add user controls and save to the databaseASP.NET 动态添加用户控件并保存到数据库
【发布时间】:2013-07-27 06:24:33
【问题描述】:

我正在编写一个 asp.net 站点,在数据库中,我有一个 questions 表和 answersOptions 表。我想根据数据库条目动态生成单选按钮或复选框服务器控件。

并且还允许用户点击提交按钮将用户选择的答案保存到数据库中。

关于如何实现这一点的任何建议?

提前致谢。

【问题讨论】:

  • 阅读这篇文章你会知道如何在ASP.NET中添加动态控件weblogs.asp.net/infinitiesloop/archive/2006/08/25/…2800_Part-1_2900.aspx
  • 能否再次指定网址,谢谢
  • 只要谷歌这个,你就会得到 ASP.NET 博客“真正理解动态控制(第 1 部分)”

标签: asp.net webusercontrol


【解决方案1】:
void getAnswers(){  SqlDataReader dr;
        SqlConnection con = new SqlConnection();
        con.ConnectionString = constring;
        SqlCommand Answers = new SqlCommand();
        con.ConnectionString = constring;
        Answers = new SqlCommand("YourStoredProcedure'" + questionID + "'", con);

        try
        {

            con.Open();
            dr = Answers.ExecuteReader();
            while (dr.Read())
            {

            CheckBox answer = new CheckBox();
            answer.Text = dr[0].ToString(); // your datarow cosist of your stored procedures return state

            }

             finally
        {
            if (dr != null)
            {
                dr.Close();
            }
            if (con != null)
            {
                con.Close();
            }
         }
        }

}

然后在 Page_Load 调用你的函数 getAnswers();我认为这是主要思想

【讨论】:

    【解决方案2】:
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-23
    • 2011-06-09
    • 1970-01-01
    • 1970-01-01
    • 2017-08-31
    • 1970-01-01
    相关资源
    最近更新 更多