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

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

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

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

每次更新的表A,表B只是一个控制显示和输入类型的表
...全文
195 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>
摘要:本文通过一个程序实例描述了在VC++6.0下如何在单文档程序中通过菜单动态控制多 个窗体的切换。    一、 引言    我们在编制程序中根据需求的不同会在程序风格上选择多文档、单文档或是对话框模式 ,对于单文档模式可能是我们使用比较多的,但有时我们想采用单文档的形式显示多个不同 的窗体,如作为数据库前台应用程序就会遇到此类问题,数据库由大量的表单组成,而同常 一个窗体内只用来显示一个表单,所以要显示其他的表单时就要用到切换窗体的技术了,下 面就通过一个程序说明该技术的实现方法。    二、 实现技术    新建一个基于CFormView的单文档应用程序,再添加一个窗体和与之对应的基于 CFormView的新视类,然后通过在主框架类里添加控制代码和菜单控制实现这两个窗体的动态 切换,下面就是具体的实现过程:    (一) 用"MFC AppWizard(exe)"建立一个新项目"SwitchForm",并在第二步的创建类型上选 择为"Single documnet"单文档模式,第三、四、五、六步均取确省状态,最后一步选择 "CFormView"作为视类的基类。点按"完成"按钮,生成了初始工程"SwitchForm"。    (二) 点选菜单"Insert"、"Resource…",在弹出的"Insert Resource"对话框中"Dialog"树 里的"IDD_FORMVIEW",点击"New"按钮,生成了一个新的窗体,将其ID号改为"IDD_NEXTFORM"。 在原有的窗体上加一个静态框"这是第一个窗体";在新建的窗体上也添加一个静态框"这是第二 个窗体"。    (三) 在菜单资源的"IDR_MAINFRAME"上添加一级菜单"窗体切换",及其二级菜单"第一个窗 体"、"第二个窗体",其标识号分别为"ID_FIRSTFORM"和"ID_SECONDFORM"。修该"第一个窗体" 的属性为"Checked",表明程序初始时显示的是第一个窗体。    (四) 在"ClassView"属性页里的"SwitchForm classes"上右键,在弹出菜单上选择 "New Class…",弹出"New Class"对话框,选择"Dialog ID:"为我们刚添加的新窗体 "IDD_NEXTFORM",选择"Base class:"为"CFormView",类名取为"CNextFormView",这样就把第 二个窗体对应的视图类添加到了工程。 (五) 在框架类里添加函数SwitchToForm(): void CMainFrame::SwitchToForm(int nForm) { file://获取原来的活动窗体的视图句柄 CView* pOldActiveView = GetActiveView(); file://获取由"nForm"标识的窗体所对应的视图句柄 CView* pNewActiveView = (CView*) GetDlgItem(nForm); file://若视图句柄为空,则创建一新的。 if (pNewActiveView == NULL) { if (nForm == IDD_SW99vCHFORM_FORM) pNewActiveView = (CView*)new CSwitchFormView; if (nForm == IDD_NEXTFORM) pNewActiveView = (CView*)new CNextFormView; CCreateContext context; context.m_pCurrentDoc = pOldActiveView->GetDocument(); pNewActiveView->Create(NULL,NULL,0L, CFrameWnd::rectDefault, this,nForm,&context); pNewActiveView->OnInitialUpdate(); } file://选择pNewActiveView为活动窗体 SetActiveView(pNewActiveView); file://显示活动窗体,隐藏非活动窗体 pNewActiveView->ShowWindow(SW_SHOW); pOldActiveView->ShowWindow(SW_HIDE); int ID; if(pOldActiveView->GetRuntimeClass() == RUNTIME_CLASS(CSwitchFormView)) ID=IDD_SW99vCHFORM_FORM; if(pOldActiveView->GetRuntimeClass() == RUNTIME_CLASS(CNextFormView)) ID=IDD_NEXTFORM; file://设置窗体的ID号 pOldActiveView->SetDlgCtrlID(ID); pNewActiveView->SetDlgCtrlID(AFX_IDW_PANE_FIRST); RecalcLayout(); }    (六)添加两个菜单相对应的命令响应函数和更新函数如下: void CMainFrame::OnFirstform() { file://通过IsKindOf函数确定当前活动窗口是否是第一个窗口,如是,则无须切换, file://否则将通过SwitchToForm函数将当前活动窗口切换到"IDD_SW99vCHFORM_FORM" file://标识的第二个窗体。 if (GetActiveView()->IsKindOf(RUNTIME_CLASS(CSwitchFormView))) return; SwitchToForm(IDD_SW99vCHFORM_FORM); } void CMainFrame::OnUpdateFirstform(CCmdUI* pCmdUI) { file://通过IsKindOf函数判断当前活动窗口是否是第一个窗体,如是则将其选中。 pCmdUI->SetCheck(GetActiveView()->IsKindOf(RUNTIME_CLASS(CSwitchFormView))); } void CMainFrame::OnSecondform() { if (GetActiveView()->IsKindOf(RUNTIME_CLASS(CNextFormView))) return; SwitchToForm(IDD_NEXTFORM); } void CMainFrame::OnUpdateSecondform(CCmdUI* pCmdUI) { pCmdUI->SetCheck(GetActiveView()->IsKindOf(RUNTIME_CLASS(CNextFormView))); }    然后再在该文件开始处添加对两个视图类的引用: #include "SwitchFormDoc.h" #include "SwitchFormView.h" #include "NextFormView.h"    在此须注意:应在两个视类的引用之前添加对文档类的引用,否则会引起编译错误。另外,由于视 类的构造函数在声明时都确省的声明为保护型的,在框架类中无法引用,所以还要将两个视类的类 声明改动如下: class CNextFormView : public CFormView { public: file://将protected 改为public. CNextFormView(); …… }; class CSwitchFormView : public CFormView { public: file://将protected 改为public. CSwitchFormView(); …… };    三、 编译运行    编译运行程序,开始时的窗体上有"这是第一个窗体的字样",菜单也只有"第一个窗体"是被选中的, 当前的活动窗体是第一个窗体;点击菜单"第二个窗体",视图中的窗体上的字样变成了"这是第二 个 窗体",同时选中的菜单也由"第一个窗体"变成了"第二个窗体",实现了通过菜单将窗体进行动态切换。    总结:此程序中关键的是SwitchToView函数,在此函数中,程序搜索所有当前文档的显示窗口来查找与CruntimeClass变量匹配的视图类。如果找到,该窗口被激活。通过与之类似的方法,还可以实现在多文档模式下的单档(文档)多视(视图),通过不同的视图以不同的方式显示来自同一份文档的数据,以更好的满足程序的需要。

62,252

社区成员

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

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

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

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