Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 通过按钮onclick事件在ASP.NET中创建动态表_C#_Asp.net - Fatal编程技术网

C# 通过按钮onclick事件在ASP.NET中创建动态表

C# 通过按钮onclick事件在ASP.NET中创建动态表,c#,asp.net,C#,Asp.net,以下是我的要求: 我有一个下拉列表和文本框(appName和profile) 我想从下拉列表和文本框中获取这些值,并将它们添加到一个表(或一个类似于gridview的控件,该控件呈现为 表1) 在某个时候,我希望能够在表中循环并将值提交给db 我的问题是: [Serializable] public class securityApps { public string secAppID { get; set; } public stri

以下是我的要求:

  • 我有一个下拉列表和文本框(appName和profile)
  • 我想从下拉列表和文本框中获取这些值,并将它们添加到一个表(或一个类似于gridview的控件,该控件呈现为 表1)
  • 在某个时候,我希望能够在表中循环并将值提交给db
我的问题是:

    [Serializable]
    public class securityApps
    {
        public string secAppID { get; set; }
        public string secAppName { get; set; }
        public string secProfile { get; set; }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        BindApps();
    }

    protected void addAppButton_Click(object sender, EventArgs e)
    {
        DropDownList appList = (DropDownList)newEntryFV.FindControl("appList");
        TextBox profileTextBox = (TextBox)newEntryFV.FindControl("profileTextBox");
        addAppsToListVS(appList.SelectedValue.ToString(), appList.SelectedItem.Text.ToString(), profileTextBox.Text.ToString());
        BindApps();

    }

    private void addAppsToListVS(string appID, string appName, string profile)
    {
        securityApps secApp = new securityApps();
        secApp.secAppID = appID;
        secApp.secAppName = appName;
        secApp.secProfile = profile;
        ((List<securityApps>)ViewState["appsListVS"]).Add(secApp);
    }
    // Binds apps to Grid View
    private void BindApps()
    {
        GridView appsListGV = (GridView)newEntryFV.FindControl("appsListGV");
        if (ViewState["appsListVS"] != null)
        {
            appsListGV.DataSource = (List<securityApps>)ViewState["appsListVS"];
            appsListGV.DataBind();
        }
        else
        {
            List<securityApps> appsListVS = new List<securityApps>();
            ViewState["appsListVS"] = appsListVS;
        }
    }
  • onClick导致的回发甚至会将表的大小写为只显示最后输入的值,而不保留以前输入的任何值 价值观
注:

  • 我尝试使用绑定到datagrid的datalist来解决这个问题,但没有成功
代码:

解决我问题的代码:

    [Serializable]
    public class securityApps
    {
        public string secAppID { get; set; }
        public string secAppName { get; set; }
        public string secProfile { get; set; }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        BindApps();
    }

    protected void addAppButton_Click(object sender, EventArgs e)
    {
        DropDownList appList = (DropDownList)newEntryFV.FindControl("appList");
        TextBox profileTextBox = (TextBox)newEntryFV.FindControl("profileTextBox");
        addAppsToListVS(appList.SelectedValue.ToString(), appList.SelectedItem.Text.ToString(), profileTextBox.Text.ToString());
        BindApps();

    }

    private void addAppsToListVS(string appID, string appName, string profile)
    {
        securityApps secApp = new securityApps();
        secApp.secAppID = appID;
        secApp.secAppName = appName;
        secApp.secProfile = profile;
        ((List<securityApps>)ViewState["appsListVS"]).Add(secApp);
    }
    // Binds apps to Grid View
    private void BindApps()
    {
        GridView appsListGV = (GridView)newEntryFV.FindControl("appsListGV");
        if (ViewState["appsListVS"] != null)
        {
            appsListGV.DataSource = (List<securityApps>)ViewState["appsListVS"];
            appsListGV.DataBind();
        }
        else
        {
            List<securityApps> appsListVS = new List<securityApps>();
            ViewState["appsListVS"] = appsListVS;
        }
    }
[可序列化]
公共类安全应用程序
{
公共字符串secAppID{get;set;}
公共字符串secAppName{get;set;}
公共字符串secProfile{get;set;}
}
受保护的无效页面加载(对象发送方、事件参数e)
{
BindApps();
}
受保护的void addAppButton\u单击(对象发送者,事件参数e)
{
DropDownList appList=(DropDownList)newEntryFV.FindControl(“appList”);
TextBox profileTextBox=(TextBox)newEntryFV.FindControl(“profileTextBox”);
addAppsToListVS(appList.SelectedValue.ToString()、appList.SelectedItem.Text.ToString()、profileTextBox.Text.ToString());
BindApps();
}
私有void addAppsToListVS(字符串appID、字符串appName、字符串配置文件)
{
securityApps secApp=新的securityApps();
secApp.secAppID=appID;
secApp.secAppName=appName;
secApp.secProfile=profile;
((列表)ViewState[“appsListVS”])。添加(secApp);
}
//将应用程序绑定到网格视图
私有void BindApps()
{
GridView appsListGV=(GridView)newEntryFV.FindControl(“appsListGV”);
如果(ViewState[“appsListVS”]!=null)
{
appsListGV.DataSource=(列表)视图状态[“appsListVS”];
appsListGV.DataBind();
}
其他的
{
List appsListVS=新列表();
ViewState[“appsListVS”]=appsListVS;
}
}

在ViewState中存储对象列表(它们甚至可以是简单的键值对)怎么样。您可以将该数据用作GridView的数据源。我认为这是最简单的方法。如果你需要更多的细节,请告诉我

编辑--上面的解决方案看起来不错--我可以通过为ViewState值设置一个属性来简化这一过程

List<securityApps> AppsListVS{
 get
 { 
    if(ViewState["AppListVS"] == null
       this.AppListVS = new List(securityApps)();
     return (List<securityApps>)ViewState["AppListVS"];
 }
 set
 {
      ViewState["AppListVS"] = value;
 }
}
列出appslistv{
得到
{ 
如果(ViewState[“AppListVS”]==null
this.AppListVS=新列表(securityApps)();
返回(列表)视图状态[“AppListVS”];
}
设置
{
ViewState[“AppListVS”]=值;
}
}

我确实尝试过这一点,但我认为我的错误在于将列表存储在本地而不是视图状态。我现在将对此进行研究,但任何示例都会很好。这可能需要一些错误处理,但它演示了我正在尝试做的事情。