双击子窗口中的行,自增到父窗口中新增一个行(c#)

本文介绍了一个ASP.NET网页应用示例,展示了如何通过HTML控件与Repeater控件进行数据交互,包括增行功能的实现及数据绑定过程。同时,文章详细解释了父窗口与子窗口间的通信机制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1, 在父窗口中增加两个Html控件,一个Repeater控件

<input type=”submit” onclick=”SelInv()” value=”增行”>

<input type=”hidden” id=”TxInv” name=”TxInv” runat=”server”> 这个是运行于服务端的Html控件,且它的类型为“隐藏”,使用它的目的就是用它做一个中转

其中的脚本如下:(加入到父窗口中的html视图中)

<scriptlanguage="javascript">

varvDialog=null;

functionSelInv()

...{

vDialog
=showModalDialog("cSmallInv.aspx?invname=",window,"status:no;resizable:yes;dialogHeight:500px;dialogWidth:500px;unadorne:yes");

if(vDialog!=null)vDialog.CusName2.value="";

}


</script>

页面如下:

当点击"增行"时,将弹出新的窗口 效果如下:


其html内容如下:

<%...@Pagelanguage="c#"Codebehind="index.aspx.cs"AutoEventWireup="false"Inherits="test.index"%>
<!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.0Transitional//EN">
<HTML>
<HEAD>
<title>index</title>
<metaname="GENERATOR"Content="MicrosoftVisualStudio.NET7.1">
<metaname="CODE_LANGUAGE"Content="C#">
<metaname="vs_defaultClientScript"content="JavaScript">
<metaname="vs_targetSchema"content="http://schemas.microsoft.com/intellisense/ie5">
<script>...
varvDialog=null;
functionSelInv()
...{
vDialog
=showModalDialog("cSmallInv.aspx?invname=",window,"status:no;resizable:yes;dialogHeight:500px;dialogWidth:500px;unadorne:yes");

if(vDialog!=null)vDialog.CusName2.value="";
}

</script>
</HEAD>
<bodyMS_POSITIONING="GridLayout">
<formid="Form1"method="post"runat="server">
<FONTface="宋体">
<TABLEid="Table1"style="WIDTH:816px;HEIGHT:214px"cellSpacing="0"cellPadding="0"
width
="816"border="0">
<TBODY>
<TR>
<TD><INPUTtype="submit"value="增行"onclick="SelInv()"><inputtype="hidden"id="TxInv"name="TxInv"runat="server">
</TD>
</TR>
<TR>
<TDvAlign="top">
<asp:Repeaterid="Repeater1"runat="server">
<HeaderTemplate>
<tablewidth="800"border="0"cellpadding="0"cellspacing="1"style="font-size:9pt"bgcolor="#333333">
<trheight="30">
<tdalign="center"valign="middle"bgcolor="#FFFF00">编号</td>
<tdalign="center"valign="middle"bgcolor="#FFFF00">定单号</td>
<tdalign="center"valign="middle"bgcolor="#FFFF00">定单日期</td>
<tdalign="center"valign="middle"bgcolor="#FFFF00">定单数量</td>
<tdalign="center"valign="middle"bgcolor="#FFFF00">派送队</td>
<tdalign="center"valign="middle"bgcolor="#FFFF00">派送标题</td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<tdheight="20"bgcolor="#ffffff"><asp:TextBoxID="stor_id"Runat="server"></asp:TextBox></td>
<tdheight="20"bgcolor="#ffffff"><asp:TextBoxID="ord_num"Runat="server"></asp:TextBox></td>
<tdheight="20"bgcolor="#ffffff"><asp:LiteralID="ord_date"Runat="server"></asp:Literal></td>
<tdheight="20"bgcolor="#ffffff"><asp:TextBoxID="qty"Runat="server"></asp:TextBox></td>
<tdheight="20"bgcolor="#ffffff"><asp:LiteralID="payterms"Runat="server"></asp:Literal></td>
<tdheight="20"bgcolor="#ffffff"><asp:TextBoxID="title_id"Runat="server"></asp:TextBox></td>
</tr>
</ItemTemplate>
<FooterTemplate>
</TABLE>
</FooterTemplate></asp:Repeater></TD></TR>
<TR>
<TD></TD>
</TR>
</TBODY></TABLE></FONT>
</form>
</body>
</HTML>


Cs代码如下:
usingSystem;
usingSystem.Collections;
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Drawing;
usingSystem.Web;
usingSystem.Web.SessionState;
usingSystem.Web.UI;
usingSystem.Web.UI.WebControls;
usingSystem.Web.UI.HtmlControls;
usingSystem.Configuration;
usingSystem.Data.SqlClient;
usingcls_common;//引用自定义的命名空间;
namespacetest
...{
/**////<summary>
///index的摘要说明。
///</summary>

publicclassindex:System.Web.UI.Page
...{
protectedSystem.Web.UI.HtmlControls.HtmlInputHiddenTxInv;
protectedSystem.Web.UI.WebControls.RepeaterRepeater1;

privatevoidPage_Load(objectsender,System.EventArgse)
...{
if(!Page.IsPostBack)
...{
this.Repeater1.DataSource=createTable();
this.Repeater1.DataBind();
}

}


privateDataTablecreateTable()
...{
DataTablectbl
=newDataTable();
ctbl.Columns.Add(
"stor_id",typeof(string));
ctbl.Columns.Add(
"ord_num",typeof(string));
ctbl.Columns.Add(
"ord_date",typeof(string));
ctbl.Columns.Add(
"qty",typeof(string));
ctbl.Columns.Add(
"payterms",typeof(string));
ctbl.Columns.Add(
"title_id",typeof(string));
ViewState[
"table"]=ctbl;//把表结构存放到viewstate中
returnctbl;
}

Web窗体设计器生成的代码#regionWeb窗体设计器生成的代码
overrideprotectedvoidOnInit(EventArgse)
...{
//
//CODEGEN:该调用是ASP.NETWeb窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}


/**////<summary>
///设计器支持所需的方法-不要使用代码编辑器修改
///此方法的内容。
///</summary>

privatevoidInitializeComponent()
...{
this.Repeater1.ItemDataBound+=newSystem.Web.UI.WebControls.RepeaterItemEventHandler(this.Repeater1_ItemDataBound);
this.TxInv.ServerChange+=newSystem.EventHandler(this.TxInv_ServerChange);
this.Load+=newSystem.EventHandler(this.Page_Load);//事件处理及事件委托

}

#endregion


privatevoidTxInv_ServerChange(objectsender,System.EventArgse)   //触发的serverchange事件
...{
DataTabledt
=(DataTable)ViewState["table"];
dt.Rows.Clear();
//清空所有行
inti=0;
foreach(RepeaterItemiteminthis.Repeater1.Items)
...{
i
++;
DataRowtherow
=dt.NewRow();//新增一行
TextBoxstor_id=(TextBox)item.FindControl("stor_id");
therow[
"stor_id"]=stor_id.Text;//使用循环读出子控件中的内容,在第一次加载时会自动跳出此循环,在已有添加的行时,增加到new的表中

TextBoxord_num
=(TextBox)item.FindControl("ord_num");
therow[
"ord_num"]=ord_num.Text;

Literalord_date
=(Literal)item.FindControl("ord_date");
therow[
"ord_date"]=ord_date.Text;

TextBoxqty
=(TextBox)item.FindControl("qty");
therow[
"qty"]=qty.Text;

Literalpayterms
=(Literal)item.FindControl("payterms");
therow[
"payterms"]=payterms.Text;

TextBoxtitle_id
=(TextBox)item.FindControl("title_id");
therow[
"title_id"]=title_id.Text;

dt.Rows.Add(therow);

}

//stringx=TxInv.Value.ToString();
string[]cinvlist=TxInv.Value.Split(',');//接收隐藏的html控件中内容
DataRowrow=dt.NewRow();
row[
"stor_id"]=cinvlist[0].ToString();
row[
"ord_num"]=cinvlist[1].ToString();
row[
"title_id"]=cinvlist[2].ToString();
row[
"qty"]=cinvlist[3].ToString();
row[
"ord_date"]=DateTime.Now;
row[
"payterms"]="job17";
dt.Rows.Add(row);
this.Repeater1.DataSource=dt;
this.Repeater1.DataBind();
ViewState[
"table"]=dt;
}


privatevoidRepeater1_ItemDataBound(objectsender,System.Web.UI.WebControls.RepeaterItemEventArgse)
//绑定文本框中的数据全在该ItemDataBound事件中
...{
if(e.Item.ItemType==ListItemType.Item||e.Item.ItemType==ListItemType.AlternatingItem)
...{
DataRowViewview
=e.Item.DataItemasDataRowView;
TextBoxstor_id
=(TextBox)e.Item.FindControl("stor_id");
stor_id.Text
=view["stor_id"].ToString();
TextBoxord_num
=(TextBox)e.Item.FindControl("ord_num");
ord_num.Text
=view["ord_num"].ToString();
Literalord_date
=(Literal)e.Item.FindControl("ord_date");
ord_date.Text
=view["ord_date"].ToString();
TextBoxqty
=(TextBox)e.Item.FindControl("qty");
qty.Text
=view["qty"].ToString();
Literalpayterms
=(Literal)e.Item.FindControl("payterms");
payterms.Text
=view["payterms"].ToString();
TextBoxtitle_id
=(TextBox)e.Item.FindControl("title_id");
title_id.Text
=view["title_id"].ToString();

}


}

}

}



运行的效果如下:

3.下面就是子窗口cSmallInv.aspx的设计,在子窗口中放置一个DataGrid控件,绑定好数据,并在ItemDataBound事件中加上提示信息以及传值的代码,如下:


privatevoidDataGrid1_ItemDataBound(objectsender,System.Web.UI.WebControls.DataGridItemEventArgse)
...{
if(e.Item.ItemType==ListItemType.Item||e.Item.ItemType==ListItemType.AlternatingItem)
...{
e.Item.Attributes.Add(
"title","双击选择");
e.Item.Attributes.Add(
"ondblclick","javascript:selTopic('"+e.Item.Cells[0].Text+"','"+e.Item.Cells[1].Text+"','"+e.Item.Cells[2].Text+"','"+e.Item.Cells[3].Text+"')");
}


}


脚本如下:(加入到cSmallInv.aspxhtml视图中)

<scriptlanguage="javascript"type="text/javascript">

functionselTopic(id,name,dfeevalue,csupinv)

...{

dialogArguments.Form1.TxInv.value
=id+','+name+','+dfeevalue+','+csupinv;//这时会触发上面的TxInv的ServerChanged事件

window.close();

}


</script>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值