如何用程序动态生成formview并设置其属性

zhjzh_zjz 2005-12-05 09:05:45
我希望在on_load中动态生成一个FormView,将其字段邦定到一个数据库表A,但是其中每个字段的标签让他存储在对应表B中的中文,也就是说显示的 中文 名后面再加上一个输入框,还希望 输入框根据B中队应该字段的类型,分别显示不同的格式,请问该怎么做?给点代码,谢谢。分不够再加

我希望的模版格式是(编辑状态下):

中文字段(表B中取得数据) 邦定数据字段[表A](输入框) 根据B中判断添加的按钮

每次更新的表A,表B只是一个控制显示和输入类型的表
...全文
180 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhjzh_zjz 2005-12-14
  • 打赏
  • 举报
回复
还是我自己搞出来了。这里的人现在怎么没多少热心人了
zhjzh_zjz 2005-12-08
  • 打赏
  • 举报
回复
发了这么久怎么也没高手帮忙一下呢?我就是想把
<asp:textbox id="TitleUpdateTextBox"
text='<%# Bind("Title") %>'
runat="server"/>

改成C#代码生成而已,为什么就不行呢?真的很着急啊,那位打下能帮忙,我多多增分!
ye_zi 2005-12-07
  • 打赏
  • 举报
回复
帮顶
zhjzh_zjz 2005-12-07
  • 打赏
  • 举报
回复

<html>
<body>
<form id="Form1" runat="server">
<h3>
FormView Constructor Example</h3>
<!-- Use a PlaceHolder control as the container for the -->
<!-- dynamically generated FormView control. -->
<asp:PlaceHolder ID="DetailsViewPlaceHolder" runat="server" />
<!-- This example uses Microsoft SQL Server and connects -->
<!-- to the Northwind sample database. Use an ASP.NET -->
<!-- expression to retrieve the connection string value -->
<!-- from the Web.config file. -->
<asp:SqlDataSource ID="EmployeeSource" SelectCommand="SELECT [EmployeeID], [LastName], [FirstName] FROM [Employees]"
ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>" runat="server"
DeleteCommand="DELETE FROM [Employees] WHERE [EmployeeID] = @original_EmployeeID"
InsertCommand="INSERT INTO [Employees] ([LastName], [FirstName]) VALUES (@LastName, @FirstName)"
OldValuesParameterFormatString="original_{0}" UpdateCommand="UPDATE [Employees] SET [LastName] = @LastName, [FirstName] = @FirstName WHERE [EmployeeID] = @original_EmployeeID">
<DeleteParameters>
<asp:Parameter Name="original_EmployeeID" Type="Int32" />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name="LastName" Type="String" />
<asp:Parameter Name="FirstName" Type="String" />
<asp:Parameter Name="original_EmployeeID" Type="Int32" />
</UpdateParameters>
<InsertParameters>
<asp:Parameter Name="LastName" Type="String" />
<asp:Parameter Name="FirstName" Type="String" />
</InsertParameters>
</asp:SqlDataSource>
</form>
</body>
</html>
zhjzh_zjz 2005-12-07
  • 打赏
  • 举报
回复
为什么我用程序生成的FormView的Edit模版在更新的时候不管用,高手帮忙看看是哪里出问题了。我感觉是帮定的地方出问题但不知道在那里修改,

<%@ Page Language="C#" %>

<%@ Import Namespace="System.Data" %>

<script runat="server">
private sealed class EmployeeTemplate : ITemplate
{
void ITemplate.InstantiateIn(Control container)
{ Label firstNameLabel = new Label();
firstNameLabel.ID = "FirstNameLabel";
firstNameLabel.DataBinding += new EventHandler FirstNameLabel_DataBinding);
LiteralControl lineBreak = new LiteralControl("<br/>");
Label lastNameLabel = new Label();
lastNameLabel.ID = "LastNameLabel";
lastNameLabel.DataBinding += new EventHandler(LastNameLabel_DataBinding);

System.Web.UI.WebControls.LinkButton myBtn = new LinkButton();
myBtn.Text = "编辑";
myBtn.ID = "Edit";
myBtn.CommandName = "Edit";
container.Controls.Add(firstNameLabel);
container.Controls.Add(lineBreak);
container.Controls.Add(lastNameLabel);
container.Controls.Add(new LiteralControl("<br/>"));
container.Controls.Add(myBtn);
}
private void FirstNameLabel_DataBinding(Object sender, EventArgs e)
{ Label firstNameLabelControl = (Label)sender;
FormView formViewContainer = (FormView)firstNameLabelControl.NamingContainer;
DataRowView rowView = (DataRowView)formViewContainer.DataItem;
firstNameLabelControl.Text = rowView["FirstName"].ToString();
}
private void LastNameLabel_DataBinding(Object sender, EventArgs e)
{ Label lastNameLabelControl = (Label)sender;
FormView formViewContainer = (FormView)lastNameLabelControl.NamingContainer;
DataRowView rowView = (DataRowView)formViewContainer.DataItem;
lastNameLabelControl.Text = rowView["LastName"].ToString();
}

}


private sealed class EmployeeEditTemplate : ITemplate
{
void ITemplate.InstantiateIn(Control container)
{
Label firstNameLabel = new Label();
firstNameLabel.ID = "FirstNameLabel";
firstNameLabel.Text = "名:";
TextBox tbox1 = new TextBox();
tbox1.ID = "tbox1";
tbox1.DataBinding += new EventHandler(tbox1_DataBinding);
LiteralControl lineBreak = new LiteralControl("<br/>");

Label lastNameLabel = new Label();
lastNameLabel.ID = "LastNameLabel";
lastNameLabel.Text = "姓:";

TextBox tbox2 = new TextBox();
tbox2.ID = "tbox2";
tbox2.DataBinding += new EventHandler(tbox2_DataBinding);

System.Web.UI.WebControls.LinkButton myUpdateBtn = new LinkButton();
myUpdateBtn.Text = "更新";
myUpdateBtn.CausesValidation = false;
myUpdateBtn.ID = "UpdateButton";
myUpdateBtn.CommandName = "Update";
System.Web.UI.WebControls.LinkButton myCancelBtn = new LinkButton();
myCancelBtn.Text = "取消";
myCancelBtn.ID = "CancelButton";
myCancelBtn.CommandName = "Cancel";
container.Controls.Add(firstNameLabel);
container.Controls.Add(tbox1);
container.Controls.Add(lineBreak);
container.Controls.Add(lastNameLabel);
container.Controls.Add(tbox2);
container.Controls.Add(new LiteralControl("<br/>"));
container.Controls.Add(myUpdateBtn);
container.Controls.Add(myCancelBtn);
}
private void tbox1_DataBinding(Object sender, EventArgs e)
{
TextBox tbox1 = (TextBox)sender;
FormView formViewContainer = (FormView)tbox1.NamingContainer;
DataRowView rowView = (DataRowView)formViewContainer.DataItem;
tbox1.Text = rowView["FirstName"].ToString();
//问题是否出在此,该如何修改呢?下面的方法也不行
//DataBinder.Eval(formViewContainer.DataItem, "FirstName").ToString();
}
private void tbox2_DataBinding(Object sender, EventArgs e)
{
TextBox tbox2 = (TextBox)sender;
FormView formViewContainer = (FormView)tbox2.NamingContainer;
DataRowView rowView = (DataRowView)formViewContainer.DataItem;
//tbox2.Text = rowView["LastName"].ToString();
//问题是否出在此,该如何修改呢?上面的方法也不行

tbox2.Text = DataBinder.Eval(formViewContainer.DataItem, "LastName").ToString();
}
}
void Page_Load(Object sender, EventArgs e)
{

// Create a new FormView object.
FormView employeesFormView = new FormView();

// Set the FormView object's properties.
employeesFormView.ID = "EmployeesFormView";
employeesFormView.DataSourceID = "EmployeeSource";
employeesFormView.AllowPaging = true;
employeesFormView.PagerSettings.Mode = PagerButtons.NextPrevious;
employeesFormView.HeaderText = "Employee Name";
employeesFormView.RowStyle.BackColor = System.Drawing.Color.LightBlue;
employeesFormView.HeaderStyle.BackColor = System.Drawing.Color.Silver;
employeesFormView.PagerStyle.BackColor = System.Drawing.Color.Silver;
employeesFormView.AllowPaging = Boolean.Parse("true");
employeesFormView.GridLines = GridLines.Both;

// employeesFormView.ItemCreated += new EventHandler(EmployeeFormView_ItemCreated);

// Create the dynamic template using the custom template class.
employeesFormView.ItemTemplate = new EmployeeTemplate();
employeesFormView.EditItemTemplate = new EmployeeEditTemplate();

// Add the FormView object to the Controls collection
// of the PlaceHolder control.
DetailsViewPlaceHolder.Controls.Add(employeesFormView);

}

</script>

62,074

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧