动态控件LinkButtom的Command事件无法执行???
动态控件LinkButtom无法执行Command事件,就是点击该控件无法执行Command的函数!该LinkButtom控件为动态生成,生成事件为DataGrid的preRender事件触发的!
本人想做自由控制分页样式! --------------------编程问答-------------------- 设置LinkButton的CommandName属性就可以了。
如果需要,可以同时设置CommandArgument属性用作Command的命令参数。 --------------------编程问答-------------------- 本人是使用代码分离方式写的,LinkButtom控件CommandName、CommandArgument都已设置!但还是不能触发事件啊!
代码(VB.NET):
LinkButtom控件生成:
private sub createPageList(byval sender as object,byval e as eventArgs)
pageNum.commandName="pageNumClick"
pageNum.commandArgument=1
addHandler pageNum.command,AddressOf pageNumClick
end sub
事件触发:
addHandler dsGrid.preRender,addressof createPageList'注:该事件触发LinkButtom生成。
dsGrid都是动态生成的!
--------------------编程问答-------------------- '代码为:
public sub pageIndex(source as object,e as dataGridPageChangedEventArgs)
source.currentPageIndex=e.newPageIndex
source.dataBind
end sub
private sub createPageList(byval sender as object,byval e as eventArgs)
dim dsGrid as datagrid=findControl("dsGrid")
dim totalPage,indexPage,i,ii,numList as integer
numList=10
totalPage=dsGrid.pagecount'总页码
indexPage=dsGrid.currentPageIndex'当前页码
i=indexPage\numList*numList+1'开始页码
ii=indexPage\numList*numList+numList
if ii>totalPage then
ii=totalPage
end if
dim pageNum as new linkButton()
pageBar.controls.add(new literalControl(dsGrid.pagecount))
for n as integer=i to ii
if n=indexPage+1 then
dim pageText as new label()
pageText.text=n
pageBar.controls.add(pageText)
else
pageNum = new linkButton()
pageNum.text=n
'pageNum.command=pageNumClick
pageNum.commandName="pageNumClick"
pageNum.commandArgument=n-1
addHandler pageNum.command,AddressOf pageNumClick'添加事件
'addHandler pageNum.click,addressOf pageIndex
pageBar.controls.add(pageNum)
end if
next
end sub
public sub pageNumClick(byval sender as object,byval e as commandEventArgs)
response.write ("触发 ")
dim dsGrid as dataGrid=findControl("dsGrid")
dsGrid.currentPageIndex=e.commandArgument
dsGrid.dataBind
end sub
private function createDataGrid() as dataGrid
dim dsGrid as new dataGrid()
dsGrid.id="dsGrid"
'======================设置DataGrid的样式================================
dsGrid.autoGenerateColumns=false
dsGrid.AllowSorting=true
dsGrid.pageSize=2
dsGrid.allowPaging =true '启用分页
dsGrid.pagerStyle.mode = pagerMode.numericPages'定义分页以数字方式分页
'dsGrid.CssClass="dataList"
dsGrid.GridLines = 0
dsGrid.CellSpacing = 1
dsGrid.headerStyle.backColor = system.drawing.colorTranslator.fromHtml("#404040")
dsGrid.headerStyle.horizontalAlign = 2 '1为向左对齐,2居中对齐,3向右对齐
dsGrid.width = new unit("100%")
dsGrid.headerStyle.height = new unit(20)
dsGrid.itemStyle.height = new unit(20)
'======================动态生成列样项=================================
dim column as new boundcolumn()
column.headertext="序号"
column.datafield="id"
column.itemStyle.width = new unit(50)
column.itemStyle.horizontalAlign = 2
dsGrid.columns.add(column)
dim linkColumn as new hyperLinkColumn()
linkColumn.headerText="资讯标题"
'linkColumn.dataTextField=ds.tables("dataList").columns("title").toString()
linkColumn.dataTextField="title"
linkColumn.dataNavigateUrlField ="id"
linkColumn.dataNavigateUrlFormatString="new.aspx?id={0}"
linkColumn.Target = "_blank"
dsGrid.columns.add(linkColumn)
column = new boundcolumn()
column.headertext="点击数"
column.datafield="hit"
column.ItemStyle.width = new unit(50)
column.ItemStyle.horizontalAlign = 2
dsGrid.columns.add(column)
dim newTemp as new templateColumn()
newTemp.headertext="添加时间"
newTemp.ItemStyle.width = new unit(90)
newTemp.ItemStyle.horizontalAlign = 2
newTemp.itemTemplate=new timeTemplateColumn()
dsGrid.columns.add(newTemp)
dataList.controls.add(dsGrid)
addHandler dsGrid.pageIndexChanged,AddressOf pageIndex'添加事件
addHandler dsGrid.preRender,addressof createPageList'在dataGrid显示之前触发该事件。
return dsGrid
end function --------------------编程问答-------------------- 产生问题的毛病在这里:该LinkButtom控件为动态生
对于动态生成的控件,要执行事件,只有在一种情况下:每次pageload都得生成控件
简单说,对于创建LinkButton的代码的地方,不能用IsPostBack这个语句出现 --------------------编程问答-------------------- 问题是:第一次点击LinkButton的页码后,页面虽刷新,但是LinkButton的页码可用状态没有变。点击第二次,页面不刷新,但是LinkButton的页码可用状态正常了。奇怪得很。
代码如下:
protected void Page_Load(object sender, EventArgs e)
{
this.ToltalCount = 20;
this.PageCount = this.ToltalCount / 9 + 1;
ViewState["PageCount"] = this.PageCount;
if (!IsPostBack)
{
this.CurrentPageIndex = 0;
ViewState["CurrentPage"] = 0;
this.BindData();
}
this.BindFooter();
}
private void BindFooter()
{
this.ddlpageindex.Items.Clear();
this.tablepageindex.Rows[0].Cells[0].Controls.Clear();
for (int i = 1; i <= Convert.ToInt32(ViewState["PageCount"]); i++)
{
this.ddlpageindex.Items.Add(i.ToString());
LinkButton indexbtn = new LinkButton();
indexbtn.Text = i.ToString();
indexbtn.CommandName = i.ToString();
indexbtn.Command += new CommandEventHandler(LinkButton_Command);
if (i == Convert.ToInt32(ViewState["CurrentPage"]) + 1)
{
indexbtn.Enabled = false;
this.ddlpageindex.SelectedIndex = Convert.ToInt32(ViewState["CurrentPage"]);
}
else
{
indexbtn.Enabled = true;
}
this.tablepageindex.Rows[0].Cells[0].Controls.Add(indexbtn);
this.tablepageindex.Rows[0].Cells[0].Controls.Add(new LiteralControl(" "));
}
} --------------------编程问答-------------------- 没有人出个主意???? --------------------编程问答-------------------- 把代码传上来,让别人帮你改改吧
补充:.NET技术 , ASP.NET