有一个下拉菜单显示1-10,用户从中选择他想要的文本框的数量 - 如果他选择无限制,则显示一个文本框,其中他输入文本框的确切数量他想。如果他输入40,则显示他40个在运行时创建的文本框。从'unlimited'动态控件输入数据到数据库
我的问题是,我如何从40-或“他输入的任何数字”文本框输入MS SQL数据库中的数据。我无法动态创建列名,这太乏味乏味和麻烦。有什么办法可以做到这一点?
有一个下拉菜单显示1-10,用户从中选择他想要的文本框的数量 - 如果他选择无限制,则显示一个文本框,其中他输入文本框的确切数量他想。如果他输入40,则显示他40个在运行时创建的文本框。从'unlimited'动态控件输入数据到数据库
我的问题是,我如何从40-或“他输入的任何数字”文本框输入MS SQL数据库中的数据。我无法动态创建列名,这太乏味乏味和麻烦。有什么办法可以做到这一点?
你之间是什么什么,那就是是页面和评论或说明或任何一个1对多的关系。你不应该有这样的模型在你的数据库text_box_1,text_box_2等相反,它应该是:
CREATE TABLE Some_Entity
(
some_entity_id INT NOT NULL,
CONSTRAINT PK_Some_Entity PRIMARY KEY CLUSTERED (some_entity_id)
)
GO
CREATE TABLE Some_Entity_Comments
(
some_entity_id INT NOT NULL,
comment_number INT NOT NULL,
comments VARCHAR(1000) NOT NULL,
CONSTRAINT PK_Some_Entity_Comments
PRIMARY KEY CLUSTERED (some_entity_id, comment_number),
CONSTRAINT FK_Some_Entity_Comments_Some_Entity
FOREIGN KEY (some_entity_id) REFERENCES Some_Entity (some_entity_id)
)
GO
一旦这样做了,你可以使用类似于门写来处理前端的东西代码。您只需将文本框索引作为comment_number。
谢谢你和汤姆H.赞赏。 – input 2009-05-06 10:40:59
当您创建文本框时,您可以为它们提供顺序标识,然后在回发中遍历这些文本框,将值写入数据库。
例如:
/// <Summary>
/// This would be fired when the user enters the number of textboxes
/// the want and click the 'Create Textboxes' button.
/// </Summary>
protected void CreateTextBoxes_Click(object sender, EventArgs e)
{
// Check how many textboxes the user wants
int count = Convert.ToInt32(CountTextBox.Text);
// Generate the number of textboxes requested
// and add them to the page
for (int i=0; i < count; i++)
{
// Create the textbox
TextBox textbox = new Textbox();
// Set the ID so we can access it later
textbox.ID = "TextBox_" + i;
// Add the textbox to the panel
TextBoxPanel.Controls.Add(textbox);
}
}
/// <Summary>
/// This would be fired when the user has entered values in the textboxes
/// and clicked the Save button to save the values to the database.
/// </Summary>
protected void SaveButton_Click(object sender, EventArgs e)
{
// Check how many textboxes the user created
int count = Convert.ToInt32(CountTextBox.Text);
// Loop through them
for (int i=0; i < count; i++)
{
// Get the TextBox
TextBox textbox = (TextBox) FindControl("TextBox_" + i);
// Get the value
string val = textbox.Text;
// Insert into DB
SaveValueToDatabase(val);
}
]
您是否考虑过将数据存储为数据库中的XML字段?
如果你有40个文本框,你可以只是一个简单的模式来存储他们喜欢:
<YourData>
<TextBox ID="TextBox1">Value1</TextBox>
<TextBox ID="TextBox2">Value2</TextBox>
<TextBox ID="TextBox3">Value3</TextBox>
... etc ...
</YourData>
无需在你的数据库的动态领域。您还应该在某处存储架构。这是存储可以从记录更改为记录的动态数据的好方法。
门,它给我的对象引用的错误没有设置这一行:
// Get the value
string val = textbox.Text;
似乎无法找到文本框的控制。
你问,因为你的老板正在要求一个下拉菜单 - 你为什么要问如何把某些东西放入分贝? – 2009-05-06 03:17:59
我不认为你有我的问题 – input 2009-05-06 08:39:36