1、Gridview中,对其进行“编辑”、“更新”操作时,需要设置其中的绑定的文本框的宽度,让用户在“编辑”状态下,在其中的文本框中能显示更多的字符。
错误方法:下面的方法不好使:
protected void grvwVideoDisp_RowEditing(object sender, GridViewEditEventArgs e)
{
this.grvwVideoDisp.EditIndex = e.NewEditIndex;
((TextBox)(grvwVideoDisp.Rows[e.NewEditIndex].Cells[0].Controls[0])).Width = 200; //这条语句运行时出错。
this.grvwVideoBind();
……
}
正确方法:只能在其.aspx页面中设计时,添加:<ControlStyle Width="宽度值" />
<asp:BoundField DataField="viName" HeaderText="新闻标题">
<ItemStyle HorizontalAlign="Left" Width="260px" />
<ControlStyle Width="200px" />
</asp:BoundField>
2、Gridview中,在进行“更新”、“删除”等操作中,需要用到数据库的主健,而在Gridview显示中往往又不希望主健信息让用户看到,这时需要在设计时隐藏主健。
错误方法:
protected void grvwMainItem_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string mid = ((TextBox)(grvwMainItem.Rows[e.RowIndex].Cells[0].Controls[0])).Text.Trim(); //此主健值设置为隐藏的,从而不可用此语句读出!
string name = ((TextBox)(grvwMainItem.Rows[e.RowIndex].Cells[1].Controls[0])).Text.Trim();
string youxj = ((TextBox)(grvwMainItem.Rows[e.RowIndex].Cells[2].Controls[0])).Text.Trim();
……
}
正确方法是:
在其.aspx页面中添加属性DataKeyNames="主健值" 进行设置。
<asp:GridView ID="grvwMainItem" runat="server" AllowPaging="True"
AutoGenerateColumns="False" BackColor="White" BorderColor="#CC9966"
BorderStyle="None" BorderWidth="1px" CellPadding="4" DataKeyNames="mainID"
onpageindexchanging="grvwMainItem_PageIndexChanging"
onrowcancelingedit="grvwMainItem_RowCancelingEdit"
onrowdatabound="grvwMainItem_RowDataBound"
onrowediting="grvwMainItem_RowEditing" onrowupdating="grvwMainItem_RowUpdating"
Width="400px">
……
</asp:GridView>
并在其.aspx.cs代码中按如下的方法读取主健:
protected void grvwMainItem_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string mid = this.grvwMainItem.DataKeys[e.RowIndex][0].ToString(); //在其aspx页面中加入了:DataKeyNames="mainID",所以可以隐藏的主健值。
……
}
3、在Gridview中使用多个主健。
有时,需要使用多个主健来对数据库中表进行操作。这时可以使用DataKeyNames="主健值1,主健值2",来设置多主健情况。如:
<asp:GridView ID="grvwSubItem" runat="server" AutoGenerateColumns="False" DataKeyNames="secondID,mainID"
Width="500px" AllowPaging="True" BackColor="White" BorderColor="#CCCCCC"
……
</asp:GridView>
正确读取多个主健的方法是:
protected void grvwSubItem_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string subid = this.grvwSubItem.DataKeys[e.RowIndex][0].ToString(); //读取DataKeyNames="secondID,mainID”中的secondID主健。
string mainid=this.grvwSubItem.DataKeys[e.RowIndex][1].ToString(); //读取DataKeyNames="secondID,mainID”中的mainID主健。
……
}
4、Gridview中,CheckBox控件的数据绑定方法。
下面为Gridview控件的前台部分代码:
<Columns>
<asp:BoundField DataField="viName" HeaderText="新闻标题">
<ItemStyle HorizontalAlign="Left" Width="260px" />
<ControlStyle Width="260px" />
</asp:BoundField>
<asp:TemplateField HeaderText="新闻发布">
<EditItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" Checked='<%# Convert.ToBoolean(Eval("isnotVisial")) %>' Text="发布" />
</EditItemTemplate>
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" Checked='<%# Convert.ToBoolean(Eval("isnotVisial")) %>' Text="发布" />
</ItemTemplate>
<ItemStyle Width="80px" />
</asp:TemplateField>
<asp:CommandField HeaderText="操作命令" ShowEditButton="True">
<ItemStyle Width="100px" />
</asp:CommandField>
</Columns>
在上面代码的模板部分对CheckBox.Checked控件的数据绑定是:Checked='<%# Convert.ToBoolean(Eval("isnotVisial")) %>' 。因为在其相关的数据库中的“innottVisial”字段内存储的是"1”或者"0”,所以转换函数Convert.ToBoolean( )是成立的。
其对应的.aspx.cs后台代码为:
protected void grvwVideoDisp_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string viid = this.grvwVideoDisp.DataKeys[e.RowIndex][0].ToString(); //获取DataKeyNames="viID"数据库主健。
//取得Girdview第一个绑定域数据。即数据库中”新闻标题“。
string viname = ((TextBox)(this.grvwVideoDisp.Rows[e.RowIndex].Cells[0].Controls[0])).Text.Trim();
// 下面代码获取Gridview中模板绑定数据。需要用到:FindControl(“控件ID”),面不能用上面的Control[索引号]来获取。
string isnotvisual = (((CheckBox)(this.grvwVideoDisp.Rows[e.RowIndex].Cells[1].FindControl("CheckBox1"))).Checked == true) ? "1" : "0";
VideoFile vfc=new VideoFile(viid,viname,isnotvisual);
bool b = vfdao.UpdateVideNews(vfc);
……
}
5、让frameset创建框架网页时,在不同的屏幕上、在不同的浏览器中都能居中显示。
由于各种浏览器的差异,让常见的IE、FireFox、Chrome或者以它们为核心的浏览器都能做到兼容居中显示。只好在然来的框架外再有套一框架。见下面代码:
<frameset cols="*,960,*" frameborder="no" border="0" framespacing="0">
<frame src="about:blank"></frame>
<frameset id="maimSet" rows="120,*,30" cols="*" frameborder="0" framespacing="0" style="960px; vertical-align:top;">
<frame src="mtop.aspx" scrolling="no" noresize="noresize" name="top" id="top">
</frame>
<frameset id="secondSet" cols="230,730" frameborder="0" framespacing="0" style=" 960px;hvertical-align:top;">
<frame src="mleft.aspx" frameborder="1" framespacing="0" name="left" scrolling="yes" noresize="noresize" id="left">
</frame>
<frame src="SystemMain.aspx" frameborder="1" framespacing="0" scrolling="yes" noresize="noresize" name="main" id="main">
</frame>
</frameset>
<frame src="mfooter.aspx" scrolling="no" noresize="noresize" name="footer" id="footer">
</frame>
</frameset>
<frame src="about:blank"></frame>
</frameset>
6、当Treeview控件与.xml文件数据绑定后,为了让用户能通过点击Treeview中的父节点"名称”来展开、收缩其下的子节点,而不是只能通过点击父节点名称前面的"+”号来展开、收缩节点。需要为Treeview控件添加TreeNodeDataBound( )事件。并在其中添加如下语句。如:
protected void TreeView1_TreeNodeDataBound(object sender, TreeNodeEventArgs e)
{
e.Node.SelectAction = TreeNodeSelectAction.Expand;
}