动态添加Controles,如何检索数据。 [英] dynamically added Controles, how to retrieve data.

查看:63
本文介绍了动态添加Controles,如何检索数据。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我坚持从ASP.NET中的动态添加文本框中检索数据。



我是主站点和内容站点。我已经在内容网站上添加了一些按钮,在用户需要之后添加或删除文本框。



我的问题是,我不确定如何正确检索数据。希望有些人可以帮助我。



我的内容网站:

Hi
I'm stuck at retrieving data from my Dynamically added Text boxes in ASP.NET.

I Master Site and a Content site. I have added some buttons to the content site there are addíng or removing the textboxes, after what's needed by the user.

My problem is, that i'm not sure how to retrieve the data correct. hope some body can help me on the way.

My Content site:

<%@ Page Title="" Language="C#" MasterPageFile="~/main.master" AutoEventWireup="true" CodeFile="CreateRMA.aspx.cs" Inherits="CreateRMA" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="mainsite" Runat="Server">

    <div id="div_fortext" class="div_fortext">

        <p class="header2">
            Opret RMA Sag
        </p>

        <p class="text1">
            Her Kan de oprette alt det udstyr der skal sendes til reperation hos zenitel.
        </p>

    </div>

    <div id="div_insert_devices"  runat="server">

        

    </div>
          // 3 buttons one who add, one who remove textboxes and a submit button
    <asp:Button ID="btnAddRow" runat="server" Text="Add Row" CssClass="butten1" OnClick="btnAddRow_Click" />
    <asp:Button ID="btnRemoveRow" runat="server" Text="Remove Row" CssClass="butten1" OnClick="btnRemoveRow_Click" />
    <asp:Button ID="btnSubmit" runat="server" Text="Submit" CssClass="butten1" OnClick="btnSubmit_Click" />

</asp:Content>





我的C#代码背后:



My C# code behind:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class CreateRMA : System.Web.UI.Page
{


    protected void Page_Load(object sender, EventArgs e)
    {

        if (!Page.IsPostBack)
        {
            ViewState["DeviceCount"] = ViewState["DeviceCount"] == null ? 1 : ViewState["DeviceCount"];
            InsertLine();
        }
        
    }


    private void InsertLine()
    {

        int DeviceCount = int.Parse(ViewState["DeviceCount"].ToString());

        

        for (int i = 0; i < DeviceCount; i++)
        {
            LiteralControl text = new LiteralControl("<div class=\"divPerDevice\">");
            div_insert_devices.Controls.Add(text);

            TextBox txtbox = new TextBox();
            txtbox.ID = "serial" + i;
            txtbox.CssClass = "textbox1";
            txtbox.Attributes.Add("runat", "Server");
            div_insert_devices.Controls.Add(txtbox);

            text = new LiteralControl("</div>");
            div_insert_devices.Controls.Add(text);
        }


    }


    protected void btnAddRow_Click(object sender, EventArgs e)
    {
        int count = int.Parse(ViewState["DeviceCount"].ToString());

        count++;


        ViewState["DeviceCount"] = count;
        InsertLine();
    }


    protected void btnRemoveRow_Click(object sender, EventArgs e)
    {
        int count = int.Parse(ViewState["DeviceCount"].ToString());

        count--;


        ViewState["DeviceCount"] = count;
        InsertLine();
    }


    protected void btnSubmit_Click(object sender, EventArgs e)
    {

        // Submit - save the textboxes to Strings ??? Can any body help

    }
}

推荐答案

HI,



请查看以下样本。



]]>







< title> Untitled Page







< asp:scriptmanager id =ScriptManager1runat =serverxmlns:asp =#unknown>





Please check the below sample.

]]>



<title>Untitled Page



<asp:scriptmanager id="ScriptManager1" runat="server" xmlns:asp="#unknown">









< asp:updatepanel id =UpdatePanel1runat =serverxmlns:asp =#unknown>

< contenttemplate>

< asp:占位符runat =serverid =myPlaceHolder>



< triggers> < asp:asyncpostbacktrigger controlid =btnAddTextBoxeventname =点击>









<asp:updatepanel id="UpdatePanel1" runat="server" xmlns:asp="#unknown">
<contenttemplate>
<asp:placeholder runat="server" id="myPlaceHolder">

<triggers> <asp:asyncpostbacktrigger controlid="btnAddTextBox" eventname="Click">








< asp:button id =btnAddTextBoxrunat =servertext =Add TextBoxonclick =btnAddTextBox_Clickxmlns:asp =#unknown >





< asp:updatepanel id =UpdatePanel2runat =serverxmlns:asp =#unknown>

< contenttemplate>

< asp:button runat =serverid =MyButtontext =Get Values。 onclick =MyButton_Click>





< asp:label runat =serverid =MyLabel>








<asp:button id="btnAddTextBox" runat="server" text="Add TextBox" onclick="btnAddTextBox_Click" xmlns:asp="#unknown">



<asp:updatepanel id="UpdatePanel2" runat="server" xmlns:asp="#unknown">
<contenttemplate>
<asp:button runat="server" id="MyButton" text="Get Values." onclick="MyButton_Click">



<asp:label runat="server" id="MyLabel">
















使用System;

使用System.Collections.Generic;

使用System.Linq;

使用System.Web;

使用System.Web.UI;

使用System。 Web.UI.WebControls;



public partial class _Default:System.Web.UI.Page

{

static int myCount = 5;

private TextBox [] dynamicTextBoxes;



protected void Page_PreInit(object sender,EventArgs e)

{

控制myControl = GetPostBackControl(this.Page);

if(myControl!= null)

{

if(myControl.ClientID.ToString()==btnAddTextBox)

{

myCount = myCount + 1;

}

}



}

protected覆盖void OnInit(EventArgs e)

{

base.OnInit(e);

dynamicTextBoxes = new TextBox [myCount];

int i;

for(i = 0;我< mycount的; i + = 1)

{

TextBox textBox = new TextBox();

textBox.ID =myTextBox+ i.ToString( );

myPlaceHolder.Controls.Add(textBox);

dynamicTextBoxes [i] = textBox;

LiteralControl literalBreak = new LiteralControl(< br>);

myPlaceHolder.Controls.Add(literalBreak);

}

}

public static控制GetPostBackControl(页面)页面

{

控制mycontrol = null;

string ctrlname = thePage.Request.Params.Get(_ EVENTTARGET );

if(((ctrlname!= null)&(ctrlname!= string.Empty)))

{

mycontrol = thePage.FindControl(ctrlname);

}

else

{

foreach(Page.Request中的字符串项) .Form)

{

控制c = thePage.FindControl(item);

if(((c)是System.Web.UI.WebControls.Button))

{

mycontrol = c;

}

}

}

返回mycontrol;

}





protected void btnAddTextBox_Click(object sender,EventArgs e)

{

//由于事件排序而在preInit中处理。

}

protected void MyButton_Click(object sender,EventArgs e)

{

MyLabel.Text =;

foreach(textBox tb in dynamicTextBoxes)

{

MyLabel。文字+ = tb.Text +::;

}

}

}








using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
static int myCount = 5;
private TextBox[] dynamicTextBoxes;

protected void Page_PreInit(object sender, EventArgs e)
{
Control myControl = GetPostBackControl(this.Page);
if (myControl != null)
{
if (myControl.ClientID.ToString() == "btnAddTextBox")
{
myCount = myCount + 1;
}
}

}
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
dynamicTextBoxes = new TextBox[myCount];
int i;
for (i = 0; i < myCount; i += 1)
{
TextBox textBox = new TextBox();
textBox.ID = "myTextBox" + i.ToString();
myPlaceHolder.Controls.Add(textBox);
dynamicTextBoxes[i] = textBox;
LiteralControl literalBreak = new LiteralControl("
");
myPlaceHolder.Controls.Add(literalBreak);
}
}
public static Control GetPostBackControl(Page thePage)
{
Control mycontrol = null;
string ctrlname = thePage.Request.Params.Get("_EVENTTARGET");
if (((ctrlname != null) & (ctrlname != string.Empty)))
{
mycontrol = thePage.FindControl(ctrlname);
}
else
{
foreach (string item in thePage.Request.Form)
{
Control c = thePage.FindControl(item);
if (((c) is System.Web.UI.WebControls.Button))
{
mycontrol = c;
}
}
}
return mycontrol;
}


protected void btnAddTextBox_Click(object sender, EventArgs e)
{
// Handled in preInit due to event sequencing.
}
protected void MyButton_Click(object sender, EventArgs e)
{
MyLabel.Text = "";
foreach (TextBox tb in dynamicTextBoxes)
{
MyLabel.Text += tb.Text + " :: ";
}
}
}


这篇关于动态添加Controles,如何检索数据。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆